Recently I’ve been playing with Administrate a gem from Thoughtbot, for a new e-commerce project built from scratch. I needed an Admin dashboard to manage users, orders, products, etc.
I heard about Administrate from a colleague at the company. I’ve used similar tools as RailsAdmin and ActiveAdmin, but I wanted to give it a try.
https://github.com/thoughtbot/administrate
My first impression:
The good part I enjoyed the most was that you don’t require to learn a DSL to hook up this gem with your project. That gives you som sort of freedom to customize your application using just ruby.
Getting started – Simple configuration:
# Gemfile
gem "administrate", "~> 0.2.2"
Then run bundle:
$ bundle install
After that, run the generator install:
$ rails generate administrate:install
And there you go, up and running.
You don’t need to run $ rails g administrate:install
every time you have made a change or created a new model, you can use administrate generators for that purpose.
You can see all generators Administrate provide doing $ rails g | ag Administrate
(I’m using Silver Searcher for those who are wondering what does mean ag
)
administrate:assets
administrate:assets:images
administrate:assets:javascripts
administrate:assets:stylesheets
administrate:dashboard
administrate:field
administrate:install
administrate:routes
administrate:view
administrate:views
administrate:views:edit
administrate:views:field
administrate:views:form
administrate:views:index
administrate:views:layout
administrate:views:new
administrate:views:show
administrate:views:sidebar
Looks like for a simple project it works pretty smooth.
My Case of study:
I’m using Postgress, Devise, Rails 4.2, Rolify and CanCan:
My Gemfile looks like:
source 'https://rubygems.org'
ruby '2.3.1'
### Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2'
### Services
gem 'pg'
### Utils
gem 'administrate', '~> 0.2.2'
gem 'cancancan', '~> 1.10'
gem 'devise', '~> 3.5.10'
gem 'rolify', '~> 5.1.0'
My Database looks like:
This is the Database Schema
https://gist.github.com/vicmaster/13832fe122ce067d883677c691c5a78d
Those are my models and relations between them:
class User < ActiveRecord::Base
rolify
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :orders
end
class Order < ActiveRecord::Base
has_many :products, through: :line_items
has_many :line_items
belongs_to :user
end
class LineItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
has_one :line_item
end
Then after I have created all the models I’m going to use the install generator:
$ rails generate administrate:install
It’s generated an admin/
route
I have my config/routes.rb
file with something like this:
AdministrateTest::Application.routes.draw do
namespace :admin do
resources :users
resources :categories
resources :line_items
resources :orders
resources :products
resources :roles
root to: "users#index"
end
devise_for :users
end
This generator it’s going to create a ‘dashboard/’ folder inside your ‘app/’ directory:
app/
▸ assets/
▸ controllers/
▾ dashboards/
category_dashboard.rb
line_item_dashboard.rb
order_dashboard.rb
product_dashboard.rb
role_dashboard.rb
user_dashboard.rb
▸ helpers/
▸ mailers/
▸ models/
▸ views/layouts
There you can customize as many you want maybe remove inputs you don’t want it to modify through the admin, and things like that.
If you want to customize views as well, you need to run this generator:
$ rails g administrate:views
this is going to create a folder admin/
inside app/views/
directory
▾ views/
▾ admin/applications/
_collection.html.erb
_form.html.erb
edit.html.erb
index.html.erb
new.html.erb
show.html.erb
▸ layouts/
And maybe you are wondering what can I do?, if I made modifications in a model or I have created a new one model, instead of use aministrate:install
you can use another of his generators:
$ rails g administrate:dashboard User
Now this regenerates the dashboard with the new model inside your admin panel.
Using Devise and Rolify
How can I give access to admin dashboard only for admin users?
Go to app/controllers/admin/application_controller.rb
and add something like:
module Admin
class ApplicationController < Administrate::ApplicationController
before_action :authenticate_user!
before_action :authenticate_admin
def authenticate_admin
redirect_to '/', alert: 'Not authorized.' unless current_user && access?
end
private
def access?
current_user.has_role? :admin
end
end
end
Just be sure you have already added role admin to the user you are testing:
$ user = User.find(1)
$ user.add_role :admin
And there you go you have an admin dashboard to manage your app.
i18n: How can I add internationalization?
If you want to support more than one language for example Mexican Spanish:
By default administrate gem has it’s own locale files: It’s support espanish, english and others but Mexican Spanish.
How can I add es-MX?
Go to your config/locales/
directory and add a translation file es-MX.yml
Then if you go to the github repo of administrate if you go to that same directory:
Copy the same content from administrate.es.yml
and add it to your es-MX.yml
you will see something like:
administrate:
actions:
confirm: ¿Estás seguro?
destroy: Destruir
edit: Editar
show: Mostrar
......
Once you have your file you can add a link wherever you want, I added on sidebar menu:
<% if I18n.locale == I18n.default_locale %>
<ul>
<li><%= link_to I18n.t('administrate.sidebar.language'), params.merge(locale: 'es-MX'), class: "sidebar__link" %></li>
</ul>
<% else %>
<ul>
<li><%= link_to I18n.t('administrate.sidebar.language'),params.merge(locale: 'en'), class: "sidebar__link" %></li>
</ul>
<% end %>
Add an initializer file to define my default language in my case english it was my default:
‘config/initializers’ add locale.rb file
I18n.default_locale = :en
Then you can go to controllers/admin/application_controller.rb
and add a set_locale method to change your language based on your params:
module Admin
class ApplicationController < Administrate::ApplicationController
before_action :set_locale
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
end
end
So, now you can jump from english to spanish with the default translations for administrate:
But, this doesn’t stop here, maybe you want to add more translations than the default by administrate:
You can add to your file es-MX.yml
under activerecord if you want to translate models and attributes:
activerecord:
errors:
messages:
record_invalid: "La validación falló: %{errors}"
restrict_dependent_destroy:
has_one: El registro no puede ser eliminado pues existe un %{record} dependiente
has_many: El registro no puede ser eliminado pues existen %{record} dependientes
models:
order:
one: "Orden"
other: "Ordenes"
product:
one: "Producto"
other: "Productos"
user:
one: "Usuario"
other: "Usuarios"
category:
one: "Categoria"
other: "Categorias"
attributes:
order:
number: "No. de Orden"
item_total: "Total por Item"
state: "Estado"
completed_at: "Completada el"
What if you want to support routes in spanish or other language as well?
Go to you Gemfile and add:
gem 'route_translator'
Then bundle again:
$ bundle install
Go to config/routes.rb
and add localized
namespace :admin do
localized do
resources :orders
resources :users
resources :categories
resources :line_items
resources :products
resources :roles
end
root to: "users#index"
end
After that go config/locales/
:
Add a new attribute routes
es-MX:
routes:
orders: "ordenes"
en: #if you don't want something custom for english don't do anything
routes:
orders: "orders"
Then re-start rails server
Then try in the browser your new route http://localhost:3000/es-mx/admin/ordenes
And there you go!
I leave you the github repo I’m using for testing purposes, so you can have it as a reference:
https://github.com/vicmaster/administrate-dashboard-test
Verdict
Pros | Cons |
---|---|
No DSLs (domain-specific languages) | Outdated repo |
let the user override defaults with standard tools | Last realese May 21, 2016 |
Break up the library into core components and plugins | |
Support Rails 4 and Rails 5 |
Administrate is a good gem. Thoughtbot vision of creating another alternative for dashboard admin has no doubt been achieved, and will certainly provide everyone who use it with something great.
However, looks like the person in charge move on so there is a lot of open Pull Requests but enough for me to stick around and explore many of the features the gem has to offer. With such easy configuration management, it quickly wears thin and you’ll feel like you’re dragging yourself from ruby to ruby on rails instead of using a DSL.
Does it live up to the hype? For anyone who’s been following this gem for a while, probably not. But if you appreciate this gem for what it is – “solves the same problem as Rails Admin and ActiveAdmin” – then there is certainly plenty of improvements to do, Hopefully soon they keep working on this amazing project.
*** Note: I just giving my personal insight about this gem ***