Overview
It's sammarized information throughout working with Active Admin on Rails
(Ruby on rails 4.3)
Target to read this content:
- Basic knowledge of Ruby on Rails
- High standard ActiveRecord knowledge
Scope:
Covering to basic integration active admin
Active Admin is a gem to create web page that supports CRUD feature via several kind of parameters configured in Model.
Integrating Device and Cancan, these helps to realize to inject authorization to each pages created.
Formastic is used too generate forms internally.
Validation in pages shall be done by Model's validation settings.
ActiveAdmin
http://activeadmin.info/docs/documentation.html
http://www.rubydoc.info/github/activeadmin/activeadmin
Formastic
http://www.rubydoc.info/github/justinfrench/formtastic/Formtastic/Helpers/InputHelper
CanCan
http://www.rubydoc.info/github/CanCanCommunity/cancancan
1. Overriding each pages
- List of model's content #index
- Displaying a Model's row information #show
- Modification of a Model's row #edit
- Export & Download csv formatted model #csv
Above actions are created by default and files are stored under app/admin/.(File name mustb be $ModelName.rb)
**Model and created file must always one‐to‐one **
Create subclass of original model when you need to create different page from a model.
Below example is basic code to add model and block to ActiveAdmin's register method.
ActiveAdmin.register $ModelName do
# All configuration and process must be threre
end
1.1. Event Handling Controller
Method that is able to override in above block is written in [resource_dsl.rb].
(https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_dsl.rb)
You can customize pages by filling up block throughout the method provided active admin inside above block '# All configuration and process must be threre'
index
index do
# check this page
column : current model's field
actions # show,edit,remove link will be displayed according to permit_params, it will be explained later on this page.
end
show do
show do
attributes_table do
row : model's field name
end
end
active_admin_comments # comment form
render 'some_partial' # can be rendered from view
end
form
Note that this 'f' is ActionView
form do |f|
inputs 'Title' do
input :model's field name
end
end
csv do
column :model's field name
end
When you need to create original action for adding new pages, you can use [action_item]
(http://activeadmin.info/docs/10-custom-pages.html#add-an-action-item).
1.2. Model's handler
Overridable methods in 'controller do' are written in data_access.rb
You can utilize this feature when you need to add condition to retrieving model or override logic.
For example, write something in scoped_collection when you need to customized list in index page.
You will probably face to situation to join table to optimize list.
ActiveRecord's loading featureRemember active record preload or includes to optimize DB access.
controller do
def scoped_collection
super # model call
super.where(filter: condition) # Adding condition to model
$ModelName.includes(:RelatedModelName)
end
def find_resource
# override to find a resouce from model (edit,show)
end
def create
# accept POST request of creating record
end
def update
# accept POST request of modifying record
end
end
end
2.Authentication
2.1. configuration
following configuration must be done in config/initializers/active_admin.rb.
# the method controllers used to force authentication
config.authentication_method = :authenticate_admin_user!
# the method used to access the current user
# It's possible to call this methos in ActiveAdmin.register .By calling this method it will return AdminUserModel currently logged in.
config.current_user_method = :current_admin_user
2.2. Cancan
After installing ActiveAdmin,following 3 models are created AdminUser,AdminUserAssign,AdminRole
Put CRUD settings for each models inside app/models/ability.rb
class Ability
include CanCan::Ability
def initialize(user)
can :manage, $ModelName
end
def $ロール名
can :manage, $ModelName
end
end
2.3. Limitation of resource access
Putting permit_params inside ActiveAdmin.register shall provide access control about each fields of target model.
Writing actions will provide entire CRUD limitation control.
permit_params
permit_params :$FieldName, :$FieldName
actions :all, except: [:update, :destroy]
3. SearchBox and Scope customization
3.1 Search Box
It's posssible to cutomize search box right side on screen.
Adding 'filter' in ActiveAdmin.register will realize customization.
sidebar
filter :モデルのフィールド名
filter :モデルのフィールド名, collection: proc{ #something }.call
3.2 Scope
If you set scope, Scope button shall be displayed upper side of screen.
scope
scope :all, default: true do |user|
# do something
end
scope :$フィルタ名 do |user|
# do something
end
4. Other things
4.1 Customizing menus
Keep this in your mind 'it's possible to customize menu always displayed top of pages. You will need this design consideration.
menu
4.2 Creating new pages
If you need to create pages not related specific model,you can use 'ActiveAdmin.register_page'
register_page
It is useful such as normal static page and view.
But mind about scope issue, @instance variable can't be used.
'controller do' or 'show','index' , these are instance of different classes and you can't pass instance variable directory.If you need to extend logic inside view pass it as render's argument.
content do
render partial: '$page_path'
render $page_path' , { data: @data }
end