Customizing ActiveAdmin

Has it really been 3 months since I last wrote a blog post? Embarrassingly: yes. Things have been pretty busy and there’s been lots learned over that time.

I thought I would write a quick post about customizing the ActiveAdmin gem. I first saw ActiveAdmin at RailsConf and thought that it was a pretty cool idea to simplify the tedious task of creating CRUD interfaces for administrative tasks. It’s something you have to do in virtually every project, but is a pretty mundane exercise of repeating virtually the same code for each model you need to manage.

I’m not going to go into detail on setting up the gem for initial use because I think they’ve done an excellent job of it, but I will share some code samples that go above and beyond what appears in the documentation. One of the pitfalls of tools like these is that often the easy stuff is easy, but anything that requires customization is not. Happily, I’ve found this to be untrue with ActiveAdmin.

Adding a Custom Action

ActiveAdmin is DSL-driven and they’ve anticipated that users will want to add to the controllers that are automatically generated under the Admin namespace. In my example, I am using ActiveAdmin to list all of my application users. I wanted to add the ability for the administrator to be able to impersonate or sign in as a particular user.

My basic registration of the User model in admin/users.rb looks like this:

ActiveAdmin.register User do
  filter :username
  filter :email

  index do
    column "Username", :sortable => :username do |user|
      link_to user.username, schedules_path(user)
    column :email
    column :user_type
    column :current_sign_in_at

To add the impersonate action, there is a single DSL block that needs to be added:

  member_action :impersonate do
    sign_in(:user, User.find_by_username(params[:id]))
    redirect_to root_path

Adding the member_action call to the registration definition will add both the appropriate controller action and route. Adding the new link to support the impersonate action is as simple as referencing the new named route:

  index do
    column "Username", :sortable => :username do |user|
      link_to user.username, schedules_path(user)
    column :email
    column :user_type
    column :current_sign_in_at
    column "Action" do |user|
      link_to 'Impersonate', impersonate_admin_user_path(user)

Of course, the corresponding collection_action call is available as well.

Removing the New Button

Out of the box, one of the default actions is to provide a ‘New :model’ button. In my example, managing Devise users manually was not necessary and I wanted to remove this button from the layout. It’s easily handled by adding the actions keyword to the registration block.

I specifically only wanted to show an index and added this to the block:

  actions :index

Huh. Really? That simple.

Default Sort Order for Resource

If you’d like to set a default sort order for your resource index, it’s quite simple as well. On registration, add an options hash:

ActiveAdmin.register User, {:sort_order => :username} do

All in all, my first experience in using ActiveAdmin in a new project has been pretty worthwhile. As I tackle more sophisticated customizations, I’ll publish a few more posts about it. As well, there is a Google Group for addressing any technical questions about the gem.


3 thoughts on “Customizing ActiveAdmin

  1. Thanks, this is good info. I’m learning Rails and have played with activeAdmin, can you tell which file generates the html output?

    I’d like to modify the table formatting (length of text, etc), but can’t seem to find the file in which to do this.

    Any help would be greatly appreciated.

    Thank you,


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s