Skip to content

Instantly share code, notes, and snippets.

@shafi-shaikat
Forked from glaszig/application.rb
Created September 19, 2016 09:43
Show Gist options
  • Save shafi-shaikat/6ffc4c76097dd17000ddc6a9771d8a1f to your computer and use it in GitHub Desktop.
Save shafi-shaikat/6ffc4c76097dd17000ddc6a9771d8a1f to your computer and use it in GitHub Desktop.
Grouped table index for ActiveAdmin
# config/application.rb
module MyApp
class Application < Rails::Application
config.autoload_paths << config.root.join('lib')
end
end
# models/book.rb
class Book < ActiveRecord::Base
belongs_to :shelf
def shelf_name
shelf.name
end
end
# admin/books.rb
ActiveAdmin.register Book do
index as: :grouped_table, group_by_attribute: :shelf_name do
# columns
end
end
# lib/active_admin/views/index_as_grouped_table.rb
require 'active_admin/views/index_as_table'
module ActiveAdmin
module Views
class IndexAsGroupedTable < IndexAsTable
def build(page_presenter, collection)
if group_by_attribute = page_presenter[:group_by_attribute]
collection.group_by(&group_by_attribute).sort.each do |group_name, group_collection|
h3 group_name
super page_presenter, group_collection
end
else
super
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment