Permalink
Browse files

Merge pull request #20 from zorab47/add-roots-collection-option

Add option to specify :roots_collection proc
  • Loading branch information...
2 parents 4bb2e13 + 4cf6eb9 commit 46bd17031e50a1243a91b07610e7830f35c55335 @nebirhos nebirhos committed Nov 21, 2013
Showing with 56 additions and 18 deletions.
  1. +13 −7 README.md
  2. +1 −0 app/assets/javascripts/active_admin/sortable.js.coffee
  3. +42 −11 lib/active_admin/views/index_as_sortable.rb
View
@@ -48,11 +48,16 @@ ActiveAdmin.register Page do
sorting_attribute: :position,
parent_method: :parent,
children_method: :children,
- roots_method: :roots
- ...
+ roots_method: :roots,
+ roots_collection: proc { current_user.pages.roots }
+ # ...
end
```
+The option `roots_collection` provides full control on how to find the root
+nodes of your sortable tree and is evaluated within the context of the
+controller. Please note that `roots_collection` will override what is specified
+in `roots_method`.
## Usage (List)
@@ -107,14 +112,15 @@ end
```ruby
ActiveAdmin.register Page do
sortable tree: true,
- max_levels: 0, # infinite indent levels
- protect_root: false, # allow root items to be dragged
+ max_levels: 0, # infinite indent levels
+ protect_root: false, # allow root items to be dragged
sorting_attribute: :position,
parent_method: :parent,
children_method: :children,
- roots_method: :roots
- collapsible: false, # show +/- buttons to collapse children
- start_collapsed: false, # when collapsible, start with all roots collapsed
+ roots_method: :roots,
+ roots_collection: nil, # proc to specifiy retrieval of roots
+ collapsible: false, # show +/- buttons to collapse children
+ start_collapsed: false, # when collapsible, start with all roots collapsed
end
```
@@ -1,3 +1,4 @@
+#= require jquery.ui.sortable
#= require jquery.mjs.nestedSortable
window.ActiveAdminSortableEvent = do ->
@@ -5,14 +5,13 @@ module Views
class IndexAsSortable < ActiveAdmin::Component
def build(page_presenter, collection)
@page_presenter = page_presenter
- @options = active_admin_config.dsl.sortable_options
@collection = if tree?
- resource_class.send(@options[:roots_method])
+ roots
else
collection
end
@collection.sort_by! do |a|
- a.send(@options[:sorting_attribute]) || 1
+ a.send(options[:sorting_attribute]) || 1
end
@resource_name = active_admin_config.resource_name.to_s.underscore.parameterize('_')
@@ -26,8 +25,40 @@ def build(page_presenter, collection)
def self.index_name; "sortable"; end
+ 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]
+ !!options[:tree]
end
# Setter method for the configuration of the label
@@ -50,7 +81,7 @@ def actions(options = {}, &block)
def build_list
resource_selection_toggle_panel if active_admin_config.batch_actions.any?
- sort_url = if (( sort_url_block = @options[:sort_url] ))
+ sort_url = if (( sort_url_block = options[:sort_url] ))
sort_url_block.call(self)
else
url_for(:action => :sort)
@@ -59,9 +90,9 @@ def build_list
"data-sortable-type" => (tree? ? "tree" : "list"),
"data-sortable-url" => sort_url,
}
- data_options["data-max-levels"] = @options[:max_levels]
- data_options["data-start-collapsed"] = @options[:start_collapsed]
- data_options["data-protect-root"] = true if @options[:protect_root]
+ data_options["data-max-levels"] = options[:max_levels]
+ data_options["data-start-collapsed"] = options[:start_collapsed]
+ data_options["data-protect-root"] = true if options[:protect_root]
ol data_options do
@collection.each do |item|
@@ -77,10 +108,10 @@ 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]
+ end if options[:collapsible]
h3 :class => "cell left" do
call_method_or_proc_on(item, @label)
@@ -91,7 +122,7 @@ def build_nested_item(item)
end
ol do
- item.send(@options[:children_method]).order(@options[:sorting_attribute]).each do |c|
+ item.send(options[:children_method]).order(options[:sorting_attribute]).each do |c|
build_nested_item(c)
end
end if tree?

0 comments on commit 46bd170

Please sign in to comment.