Active Admin

Admin section is a common module for most of the applications. For Rails applications we have a gem called Active Admin. That can be used to create application from scratch to admin interfaces with little effort.

Introduction
Its a framework where all the admin tasks can be managed. It creates the beautiful admin interface that gives you access to manage the data and it can be customized easily.

How to install and setup?

First I will be creating three modules for my Rails 5 application.

Artist, Producer and Movie

Relations:
artist.rb
has_many :movies

producer.rb
has_many :movies

movie.rb
belongs_to :artist
belongs_to :producer

Lets add activeadmin to the gemfile and do bundle install.

gem 'devise'
gem 'activeadmin', github: 'activeadmin'
bundle install

There are some dependencies you need to add for Rails 5. Add below gem to the gemfile and do bundle install again.

gem 'inherited_resources', github: 'activeadmin/inherited_resources'

Run the generator to install active admin. It creates all the administrative files under app/admin directory. Device gem used by active admin for authentication.

rails g active_admin:install

Your terminal prompts you some settings need to be configured manually once you ran above command.

ActiveAdmin Authentication

Requires two settings to authenticate and use current user in the application.

To force controller methods to authenticate

config.authentication_method = :authenticate_admin_user!

To access current user from the methods

config.current_user_method = :current_admin_user

Both above settings can be set to false to turn off authentication.

Start the rails server and navigate to the http://localhost:3000/admin. You can login by using following credentials.

Username: [email protected]
Password: password

active-admin

The admin dashboard will be displayed once you login successfully. Top menu is showing the list of models registered with active admin. As of now we have only one model AdminUser. Here you can create or edit admin information.

Customizing Views
Here we need to register our models with active admin.

rails generate active_admin:resource Artist
rails generate active_admin:resource Producer
rails generate active_admin:resource Movie

Before that lets add some data to your db.

Seeds.rb

a1 = Artist.create!(name: 'artist-1')
a2 = Artist.create!(name: 'artist-2')

p1 = Producer.create!(name: 'producer-1')
p2 = Producer.create!(name: 'producer-2')

Movie.create!(name: 'movie-1', released_year: 2014, director: 'director-1', description: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.', artist: a1, producer: p1)

Run the application, you can see the seeded on the interface and you can edit and delete the data.

Customization

By default resource will be displayed in the global navigation. It can be disabled if it is not required in the menu.

ActiveAdmin.register Movie do
  menu false
end

Rename the menu label with the required label.

ActiveAdmin.register Movie do
  menu label: 'List of movies'
end

Menu priority can be set by

ActiveAdmin.register Movie do
  menu priority: 4
end

By default all the columns used to diaplay. You can list the required columns in index method that used to display. Here I removed timestamp columns from the admin/movie.rb

index do
  column :name
  column :description
  column :released_year
  column :director
  column :producer
  column :artist
  actions
end

You can customize column name by specifying the column name before field in the index method.

column 'First Name', :name

On right side of the page we can find filter section. By default active admin detects belongs_to relations. We can customize filter section.

filter :producer, :as => :check_boxes
filter :artist, :as => :check_boxes


CSV Customization

By dafult active admin will download all the displayed columns to csv. Here we can customize to the required columns with the CSV settings.

ActiveAdmin.register Movie do
  csv force_quotes: true, col_sep: ';', column_names: false do
    column :name
    column(:artist) { |movie| movie.artist }
    column(:producer) { |movie| movie.producer }
  end
end

Even globally we can configure settings for the CSV

# config/initializers/active_admin.rb

# Set the CSV builder separator

config.csv_options = { col_sep: ';' }

# Force the use of quotes

config.csv_options = { force_quotes: true }