Category Archives: Active Admin

Add custom style for active admin boolean fileds

Sometimes you need to add custom styles in your active admin as per project requirement.

You can do that just adding few method in your initializer file active_admin.rb

Code:

# It extends activeadmin to show pretty boolean values
# 
# config/initializers/active_admin.rb

 module ActiveAdmin
  module Views
    class TableFor
      def bool_column(attribute)
        column(attribute) { |model| model[attribute] ? '✔'.html_safe : '✗'.html_safe }
      end

      def seller_column(attribute)
        column(attribute) { |model| model[attribute] ? 'Yes' : 'No' }
      end
    end

    class AttributesTable
      def bool_row(attribute)
        row(attribute) { |model| model[attribute] ? '✔'.html_safe : '✗'.html_safe }
      end

      def seller_row(attribute)
        row(attribute) { |model| model[attribute] ? 'Yes' : 'No' }
      end
    end
  end
end

Use in active admin:

# example
# app/admin/user.rb
 
ActiveAdmin.register User do
  index do
    column :name
    column :email
    bool_column :admin
    seller_column :seller
  end
  show do
    attributes_table do
      row :name
      row :email
      bool_row :admin
      seller_row :seller
    end
  end
end

That’s it. Happy coding 🙂