Admini is a minimal administration framework for Ruby on Rails application.
The core feature is just provides CRUD actions as Active Support's Concern module. So you can implement administration page as usual.
Table of contents
- Basic usage
- Customize attributes
- Customize rendering text
- Search items
- Enum as select tag
- Override CRUD actions
- Authorize user
- Use default theme
- Edit header menu
- Override specify view
- Change namespace of form object
- Change paginates per
- Need help?
You can try an administration page built with Admini at following link. The code of the demo can be found here.
Add this line to your application's Gemfile:
And then execute:
If the namespace of your administration page is
:admin, you probably create
Admin::ApplicationController like this:
class Admin::ApplicationController < ActionController::Base protect_from_forgery with: :exception # If you use Devise: # before_action :authenticate_user! end
There's no code related to Admini. You can implement as you like, such as an authentication.
Now everything is ready to create administration page. For example, to create the page manages Post model, you have to do the following steps:
- Add routing
The example codes is below:
class Admin::PostsController < Admin::ApplicationController include Admini::Resources end
namespace :admin do resources :posts end
That's it, and now you can take action
destroy the posts at
The items rendering on
edit can customize using
If you define following methods to controller, the items have changed:
class Admin::PostsController < Admin::ApplicationController include Admini::Resources private def index_attributes %i(id title status created_at) end def show_attributes %i(id title status created_at content) end def new_attributes %i(title status content) end end
Customize rendering text
The items rendering text on
show are also customizable as you like.
If you define
#render_xxx on your controller, Admini renders text according to the method.
Here is an example that renders a title with link to post instead of just title.
class Admin::PostsController < Admin::ApplicationController include Admini::Resources private def render_title(resource) path = case resource.status when 'draft' preview_post_path(resource, token: resource.preview_token) else post_path(resource) end view_context.link_to resource.title, path end end
In the same way, a method name to customize the
If you want to enable the search form, you should just define
#search_attributes on your controller.
Following examples enable the search form searched by
class Admin::PostsController < Admin::ApplicationController include Admini::Resources private def search_attributes %i(title content) end end
Enum as select tag
Enum is treated as Integer by database, so enum form has created as text field by default.
If you want to show the form as select box, you should define
class Admin::PostsController < Admin::ApplicationController include Admini::Resources private def enum_attributes %i(status) end end
Override CRUD actions
Often we want to override CRUD actions, especially
To do this, just define
#update on your controller.
If you want to delegate to
super defined by Admini, you should call
#super on the action.
class Admin::PostsController < Admin::ApplicationController include Admini::Resources def create @resource.user = current_user super end end
When you define the following methods on your controller, Admini authorizes user with it, and raise
Admini::AuthorizationError if user has not authorized.
Examples using CanCanCan:
class Admin::PostsController < Admin::ApplicationController include Admini::Resources private def can_create? can? :create, Post end end
Also you can define custom error handler.
This is realized by to define
#authorization_error on your
class Admin::ApplicationController < ActionController::Base private def authorization_error puts 'Authorization error' end end
Use default theme
Admini doesn't apply any styles to administration pages by default. Because Admini should be minimal. If you want to apply basic style created by Admini, you should require the stylesheet.
/* *= require admini/default */
Needless to say, you can write your own styles here as you like.
Edit header menu
Admini is minimal, so the links to pages will not added automatically. The default view generated by Admini doesn't have any links like this.
However, you can override header menu by editing
<div class="header"> <div class="container"> <%= link_to 'Admin', admin_root_path, class: 'header-title' %> <%= link_to 'Posts', admin_posts_path %> <%= link_to 'Users', admin_users_path %> <div class="right"> <%= link_to 'Logout', destroy_user_session_path, method: :delete %> </div> </div> </div>
Override specify view
The view has rendered with a common views. If you want to implement original views, you should place your own views according to Rails convention.
For example, to customize the view of
admin/posts#show, you should create
In the same way, you can override all views, including
Change namespace of form object
[:admin, @resource] as the namespace of form object by default.
This is because generally we adopt
:admin as administration page's namespace.
If your administration page has a different namespace like
:editor, you should define
#resource_object on your
class Editor::ApplicationController < ActionController::Base private def resource_object [:editor, resource] end end
Change paginates per
Admini depends on Kaminari as paginater, and it paginates per
You can override this number by
class Admin::ApplicationController < ActionController::Base private def paginates_per 10 end end
- Fork it ( https://github.com/kami-zh/admini/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
The gem is available as open source under the terms of the MIT License.