-
-
Save shafi-shaikat/6ffc4c76097dd17000ddc6a9771d8a1f to your computer and use it in GitHub Desktop.
Grouped table index for ActiveAdmin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/application.rb | |
module MyApp | |
class Application < Rails::Application | |
config.autoload_paths << config.root.join('lib') | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# models/book.rb | |
class Book < ActiveRecord::Base | |
belongs_to :shelf | |
def shelf_name | |
shelf.name | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# admin/books.rb | |
ActiveAdmin.register Book do | |
index as: :grouped_table, group_by_attribute: :shelf_name do | |
# columns | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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