Class: ActiveAdmin::Views::IndexAsTable

Inherits:
Component
  • Object
show all
Defined in:
lib/active_admin/views/index_as_table.rb

Overview

Index as a Table

By default, the index page is a table with each of the models content columns and links to show, edit and delete the object. There are many ways to customize what gets displayed.

Defining Columns

To display an attribute or a method on a resource, simply pass a symbol into the column method:

index do
  selectable_column
  column :title
end

For association columns we make an educated guess on what to display by calling the following methods in the following order:

:display_name, :full_name, :name, :username, :login, :title, :email, :to_s

This can be customized in config/initializers/active_admin.rb.

If the default title does not work for you, pass it as the first argument:

index do
  selectable_column
  column "My Custom Title", :title
end

Sometimes that just isn't enough and you need to write some view-specific code. For example, say we wanted a "Title" column that links to the posts admin screen.

column accepts a block that will be rendered for each of the objects in the collection. The block is called once for each resource, which is passed as an argument to the block.

index do
  selectable_column
  column "Title" do |post|
    link_to post.title, admin_post_path(post)
  end
end

To setup links to View, Edit and Delete a resource, use the actions method:

index do
  selectable_column
  column :title
  actions
end

You can also append custom links to the default links:

index do
  selectable_column
  column :title
  actions do |post|
    link_to "Preview", admin_preview_post_path(post), class: "member_link"
  end
end

Or forego the default links entirely:

index do
  column :title
  actions defaults: false do |post|
    link_to "View", admin_post_path(post)
  end
end

Sorting

When a column is generated from an Active Record attribute, the table is sortable by default. If you are creating a custom column, you may need to give Active Admin a hint for how to sort the table.

If a column is defined using a block, you must pass the key to turn on sorting. The key is the attribute which gets used to sort objects using Active Record.

By default, this is the column on the resource's table that the attribute corresponds to. Otherwise, any attribute that the resource collection responds to can be used.

index do
  column :title, sortable: :title do |post|
    link_to post.title, admin_post_path(post)
  end
end

You can turn off sorting on any column by passing false:

index do
  column :title, sortable: false
end

It's also possible to sort by PostgreSQL's hstore column key. You should set sortable option to a column->'key' value:

index do
  column :keywords, sortable: "meta->'keywords'"
end

Associated Sorting

You're normally able to sort columns alphabetically, but by default you can't sort by associated objects. Though with a few simple changes, you can.

Assuming you're on the Books index page, and Book has_one Publisher:

controller do
  def scoped_collection
    super.includes :publisher # prevents N+1 queries to your database
  end
end

Then it's simple to sort by any Publisher attribute from within the index table:

index do
  column :publisher, sortable: 'publishers.name'
end

Showing and Hiding Columns

The entire index block is rendered within the context of the view, so you can easily do things that show or hide columns based on the current context.

For example, if you were using CanCan:

index do
  column :title, sortable: false
  column :secret_data if can? :manage, Post
end

Defined Under Namespace

Classes: IndexTableFor

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.index_name ⇒ Object



194
195
196
# File 'lib/active_admin/views/index_as_table.rb', line 194

def self.index_name
  "table"
end

Instance Method Details

#build(page_presenter, collection) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/active_admin/views/index_as_table.rb', line 164

def build(page_presenter, collection)
  table_options = {
    id: "index_table_#{active_admin_config.resource_name.plural}",
    sortable: true,
    class: "index_table index",
    i18n: active_admin_config.resource_class,
    paginator: page_presenter[:paginator] != false
  }

  table_for collection, table_options do |t|
    table_config_block = page_presenter.block || default_table
    instance_exec(t, &table_config_block)
  end
end

#default_table ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/active_admin/views/index_as_table.rb', line 183

def default_table
  proc do
    selectable_column
    id_column
    resource_class.content_columns.each do |col|
      column col.name.to_sym
    end
    default_actions
  end
end

#table_for(*args, &block) ⇒ Object



179
180
181
# File 'lib/active_admin/views/index_as_table.rb', line 179

def table_for(*args, &block)
  insert_tag IndexTableFor, *args, &block
end