Abhi On Rails

Ruby On Rails - What I learned recently

Active Admin tips and tricks

For my current project I had to setup Admin console. I used Active Admin for that purpose. It is very easy to setup an Admin console. And they have a very good documentation. But if you want make some customization you have to spent some time digging for solution. Following are some customization I made to improve the Admin console:

1.member_label

If you are using select box for showing association in form page or filters, it will show the object string if there is no attribute 'name' for that model. By default the select box will take the name attribute of the model as the values. So you have to use member_label to specify the attribute you want to show in the select box.
f.input :user, :as => :select, :member_label => :email
  

2.Custom Menu

I had to modify the menus by grouping some menus to sub-menus. I have added the following code to initializers/active_admin.rb:
 admin.build_menu do |menu|
   menu.add :label => "Product", :priority => 2
 end
  

This will create a main menu 'Product'. To add sub-menus you have to mention this menu as the parent.
 menu :parent => "Product", :priority => 3
  

Here priority is the order in which we need to show the sub-menu.

3.Custom Actions

For one model Admin doesn't have the privilege to create new record. So I have to remove the new action from the Admin console. So I added the following code:
actions :all, :except => [:new]
  

4.Columns in show page

For one of my model, in the show page I felt there are lots of space available in the right side. But to show something in the right side I had to customize the show page. So I used columns to show multiple associated data in the show page:
show do |product_request|
  columns do
    column do
      attributes_table do
        row :id
        row :user
      end
    end

    column do
      active_admin_comments
    end
  end
    
  columns do
    column do
      h3 "Businesses"
      table_for resource.businesses do 
        column :id
        column :business
      end
    end

    column do
      h3 "Products"
      table_for resource.products do 
        column :id
        column :product
      end
    end
  end
end
  

5.Link to filter of another model

For one of the models (For Eg. User) I had to show a link in the index page to show list of associated data (For Eg. Business) ie., I have to show the index page of business with a filter of user.
column "Businesses" do |user|
  link_to "Click", :controller => "businesses", 
                   :action => "index", 'q[user_id_eq]' => "#{user.id}".html_safe
end
  

6.Implementing Authorization

In one of my project I had implemented both User model (for consumers) and Admin User model (for Active Admin). But later more and more roles got added to the Consumer user and we had to implement same set of features in both Active Admin and the user side. Some of the User roles like "Managers" need some of the reports which were already present for Admin.
So I decided to merge User and AdminUser models. Keep only User model as it has more entries than AdminUser model. To implement this we need to make only a small change in the Active Admin initializer.
    # config/initializers/active_admin.rb
    ActiveAdmin.setup do |config|
    ...
    config.current_user_method = :current_user
    config.logout_link_path = :destroy_user_session_path
    # config.skip_before_filter :authenticate_user!
    ...
    end
  

The next step was to show/hide the Active Admin menus based on the roles. It was pretty much simple, just need to implement the role checking functions in the User model and use that in the corresponding Active Admin resource pages.
    # app/admin/post.rb
    ActiveAdmin.register Post do
    menu :if => proc{ current_user.manager? }
    ...
    end
  

blog comments powered by Disqus
Integrating FriendlyID, Inh... →
← Deploying a Rails app to a ...