Skip to content

Commit

Permalink
Add roots_collection option
Browse files Browse the repository at this point in the history
Allow users to define custom logic to find the roots of the tree to be
rendered, while staying backward compatible. Users may now do:

ActiveAdmin.register TreeNode do
  sortable tree: true,
    roots_collection: proc { current_user.tree_nodes.roots }
end
  • Loading branch information
zorab47 committed Nov 18, 2013
1 parent e9e5cd4 commit 6e5a2b6
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions lib/active_admin/views/index_as_sortable.rb
Expand Up @@ -6,7 +6,7 @@ class IndexAsSortable < ActiveAdmin::Component
def build(page_presenter, collection)
@page_presenter = page_presenter
@collection = if tree?
resource_class.send(options[:roots_method])
roots
else
collection
end
Expand All @@ -29,6 +29,34 @@ def options
active_admin_config.dsl.sortable_options
end

def roots
roots_collection || default_roots_collection
end

# Find the roots by calling the roots method directly on the resource.
# This effectively performs:
#
# TreeNode.roots # => [#<TreeNode id:1>, ... ]
#
# Returns collection of roots.
def default_roots_collection
resource_class.send(options[:roots_method])
end

# Use user-defined logic to find the root nodes. This executes a callable
# object within the context of the resource's controller.
#
# Example
#
# options[:roots_collection] = proc { current_user.tree_nodes.roots }
#
# Returns collection of roots.
def roots_collection
if (callable = options[:roots_collection])
controller.instance_exec(&callable)
end
end

def tree?
!!options[:tree]
end
Expand Down Expand Up @@ -80,7 +108,7 @@ def build_nested_item(item)
div :class => "cell left" do
resource_selection_cell(item) if active_admin_config.batch_actions.any?
end

span :class => :disclose do
span
end if options[:collapsible]
Expand Down

0 comments on commit 6e5a2b6

Please sign in to comment.