Skip to content

Instantly share code, notes, and snippets.

@astery
Created August 7, 2013 10:10
Show Gist options
  • Save astery/6172796 to your computer and use it in GitHub Desktop.
Save astery/6172796 to your computer and use it in GitHub Desktop.
Trick to achieve hierarchical view in activeadmin.
# encoding: utf-8
ActiveAdmin.register Section do
config.filters = false
index do
column :name do |s|
" #{ "――" * s.depth } #{s.name}"
end
default_actions
end
controller do
def collection
c = Section.hash_tree_flat
c = Kaminari.paginate_array(c).page(params[:page])
class << c
def reorder(a=''); self end
alias :count :size
end
c
end
end
end
class Section < ActiveRecord::Base
has_and_belongs_to_many :posts
has_many :menu_items, as: :menuable
acts_as_tree order: 'sort_order'
def hash_tree_flat
_ct_hash_tree_flat(hash_tree)
end
def _ct_hash_tree_flat(hash)
hash.each_pair.map do |h_arr|
[h_arr[0], _ct_hash_tree_flat(h_arr[1])] unless h_arr[1].nil?
end
.flatten
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment