Last Updated: February 25, 2016
·
651
· Julio Bêtta

RailsAdmin and ActiveSupport::Concern

Instead of throwing all your models' configuration at one giant rails_admin.rb initializer, it would be better to organize them by context. We could just include a rails_admin macro in our models. But IMHO, I don't think that the model is a good place to do that, because it breaks a design principle called Separation of Concerns (SoC).

Therefore, we'll use the ActiveSupport::Concern to... separate concerns!

For instance, we have a Category model.

Creating an ActiveSupport:Concern module

# app/models/concerns/rails_admin/category.rb
module RailsAdmin::Category
  extend ActiveSupport::Concern

  included do
    rails_admin do
      edit do
        # edit fields
      end

      show do
        # show fields
      end

      list do
        # list fields
      end
    end
  end
end

Including the Concern

# app/models/category.rb
class Category < ActiveRecord::Base
  include RailsAdmin::Category
  # ...
end

Read more about ActiveSupport::Concern here.