Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extend active admin to prettier boolean values
# 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
end
class AttributesTable
def bool_row(attribute)
row(attribute){ |model| model[attribute] ? '✔'.html_safe : '✗'.html_safe }
end
end
end
end
# example
# app/admin/user.rb
ActiveAdmin.register User do
index do
column :name
column :email
bool_column :admin
end
show do
attributes_table do
row :name
row :email
bool_row :admin
end
end
end
@rafez

This comment has been minimized.

Copy link

commented Jan 10, 2013

Very handy, and easy to appreciate. Thanks.

@rafez

This comment has been minimized.

Copy link

commented Jan 10, 2013

Forgot to mention, also added an IndexAsTable class to the Views module to work with ActiveAdmin 0.5.1

class IndexAsTable
  def bool_column(attribute)
    column(attribute){ |model| model[attribute] ? '✔'.html_safe : '✗'.html_safe }
  end
end
@shamsulsham89

This comment has been minimized.

Copy link

commented Mar 13, 2013

Thanks very helpful. I was looking for it.

@ryanwjackson

This comment has been minimized.

Copy link

commented Feb 13, 2015

I would suggest the following edit (though honestly not 100% that args.last always returns the attribute:

module ActiveAdmin
  module Views
    class TableFor
      def bool_column(*args)
        column(*args){ |model| model[args.last] ? '✔'.html_safe : '✗'.html_safe }
      end
    end
    class AttributesTable
      def bool_row(*args)
        row(*args){ |model| model[args.last] ? '✔'.html_safe : '✗'.html_safe }
      end
    end
  end
end

I ran into the need for this with renaming columns like bool_column 'Paid?', :is_paid.

@jtomaszewski

This comment has been minimized.

Copy link

commented Jan 15, 2016

And use this code if you just want to show the booleans with status_tag in AttributesTable view (no need of changing column methods to bool_column:

ActiveAdmin::Views::AttributesTable.class_eval do
  def content_for(record, attr)
    value = find_attr_value(record, attr)
    if is_boolean?(attr, record)
      value = status_tag(value)
    else
      value = pretty_format(value)
    end
    value.blank? && current_arbre_element.children.to_s.empty? ? empty_value : value
  end

  def is_boolean?(data, item)
    if item.respond_to? :has_attribute?
      item.has_attribute?(data) &&
        item.column_for_attribute(data) &&
        item.column_for_attribute(data).type == :boolean
    end
  end
end
@Suwarna

This comment has been minimized.

Copy link

commented Mar 3, 2016

its really helpful....i am looking for it from last two days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.