ruby-on-railsactiverecordactiveadminacts-as-tree

Displaing closure_tree in ActiveAdmin. How to create hierarhical view?


At minimal I want to achieve some indentation in index table. Like this:

+Parent
+--Child
   +--Child of Child
+--Child

So I create the following:

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
      Section.some_method_to_get_things_in_right_order
    end
  end
end

It's need to some_method return active record relation, but I haven't succeed. And ended up with this hacky way.


Solution

  • The sortable_tree activeadmin plugin works for me well with closure tree.

    https://github.com/zorab47/active_admin-sortable_tree

    It creates a simple hierarchical and sortable view.

    Just put the following in your tree model resource (app/admin/TreeModel): (assuming Rails >4, replace <..> with your values)

    ActiveAdmin.register TreeModel do
    
    config.filters = false # the default filters don't work unfortunately
    permit_params <YOUR_TREE_MODEL_ATTRIBUTES>
    
    sortable tree: true,
                   sorting_attribute: :<YOUR_SORT_ATTRIBUTE>,
                   parent_method: :parent,
                   children_method: :children,
                   roots_method: :roots
    
    index :as => :sortable do
        label :name # item content
            actions
      end
    
    end