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 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s