Administrate review

MagmaLabs
MagmaLabs Blog

--

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.

A Rails engine that helps you put together a super-flexible admin dashboard.
https://github.com/thoughtbot/administrate
497 forks.
3,381 stars.
141 open issues.
Recent commits:

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

ActiveRecord::Schema.define(version: 20160930181923) do # These are extensions that must be enabled in order to support this database enable_extension “plpgsql” create_table “categories”, force: :cascade do |t| t.string “name” end create_table “categories_products”, id: false, force: :cascade do |t| t.integer “product_id”, null: false t.integer “category_id”, null: false end add_index “categories_products”, [“category_id”], name: “index_categories_products_on_category_id”, using: :btree add_index “categories_products”, [“product_id”], name: “index_categories_products_on_product_id”, using: :btree create_table “line_items”, force: :cascade do |t| t.integer “order_id” t.integer “product_id” t.integer “quantity”, null: false t.datetime “created_at” t.datetime “updated_at” end add_index “line_items”, [“order_id”], name: “index_line_items_on_order_id”, using: :btree add_index “line_items”, [“product_id”], name: “index_line_items_on_product_id”, using: :btree create_table “orders”, force: :cascade do |t| t.string “number”, limit: 15 t.decimal “item_total”, precision: 8, scale: 2, default: 0.0, null: false t.decimal “total”, precision: 8, scale: 2, default: 0.0, null: false t.datetime “created_at” t.datetime “updated_at” t.string “state” t.datetime “completed_at” t.integer “bill_address_id” t.decimal “payment_total”, precision: 8, scale: 2, default: 0.0 t.string “payment_state” t.string “email” t.integer “user_id” end create_table “products”, force: :cascade do |t| t.string “name”, default: “”, null: false t.text “description” t.datetime “available_on” t.datetime “deleated_at” t.datetime “created_at” t.datetime “updated_at” t.string “sku”, default: “”, null: false t.decimal “price”, precision: 8, scale: 2, null: false end create_table “roles”, force: :cascade do |t| t.string “name” t.integer “resource_id” t.string “resource_type” t.datetime “created_at” t.datetime “updated_at” end add_index “roles”, [“name”, “resource_type”, “resource_id”], name: “index_roles_on_name_and_resource_type_and_resource_id”, using: :btree add_index “roles”, [“name”], name: “index_roles_on_name”, using: :btree create_table “users”, force: :cascade do |t| t.string “email”, default: “”, null: false t.string “encrypted_password”, default: “”, null: false t.string “reset_password_token” t.datetime “reset_password_sent_at” t.datetime “remember_created_at” t.integer “sign_in_count”, default: 0, null: false t.datetime “current_sign_in_at” t.datetime “last_sign_in_at” t.inet “current_sign_in_ip” t.inet “last_sign_in_ip” t.datetime “created_at”, null: false t.datetime “updated_at”, null: false end add_index “users”, [“email”], name: “index_users_on_email”, unique: true, using: :btree add_index “users”, [“reset_password_token”], name: “index_users_on_reset_password_token”, unique: true, using: :btree create_table “users_roles”, id: false, force: :cascade do |t| t.integer “user_id” t.integer “role_id” end add_index “users_roles”, [“user_id”, “role_id”], name: “index_users_roles_on_user_id_and_role_id”, using: :btree end

view rawschema.rb hosted with

by GitHub

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
0 forks.
0 stars.
0 open issues.
Recent commits:

Verdict

ProsConsNo DSLs (domain-specific languages)Outdated repolet the user override defaults with standard toolsLast realese May 21, 2016Break up the library into core components and pluginsSupport 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 ***

Magmalabs.io is an expert UX/UI Design, Ecommerce, and Software Development consultancy. We build beautiful ecommerce, web, and mobile software solutions for startups, scale ups and corporate companies using innovative technologies, top talent, and exceptional quality assurance.

--

--

MagmaLabs
MagmaLabs Blog

MagmaLabs builds tailored software solutions to help companies execute their vision, with loyal teams achieving quick turn-around Have a project? We can help!