Created
August 7, 2013 10:10
-
-
Save astery/6172796 to your computer and use it in GitHub Desktop.
Trick to achieve hierarchical view in 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
# 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 |
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
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