To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / lib / plugins / awesome_nested_set / lib / awesome_nested_set / helper.rb @ 1298:4f746d8966dd
History | View | Annotate | Download (1.5 KB)
| 1 |
module CollectiveIdea #:nodoc: |
|---|---|
| 2 |
module Acts #:nodoc: |
| 3 |
module NestedSet #:nodoc: |
| 4 |
# This module provides some helpers for the model classes using acts_as_nested_set.
|
| 5 |
# It is included by default in all views.
|
| 6 |
#
|
| 7 |
module Helper |
| 8 |
# Returns options for select.
|
| 9 |
# You can exclude some items from the tree.
|
| 10 |
# You can pass a block receiving an item and returning the string displayed in the select.
|
| 11 |
#
|
| 12 |
# == Params
|
| 13 |
# * +class_or_item+ - Class name or top level times
|
| 14 |
# * +mover+ - The item that is being move, used to exlude impossible moves
|
| 15 |
# * +&block+ - a block that will be used to display: { |item| ... item.name }
|
| 16 |
#
|
| 17 |
# == Usage
|
| 18 |
#
|
| 19 |
# <%= f.select :parent_id, nested_set_options(Category, @category) {|i|
|
| 20 |
# "#{'–' * i.level} #{i.name}"
|
| 21 |
# }) %>
|
| 22 |
#
|
| 23 |
def nested_set_options(class_or_item, mover = nil) |
| 24 |
if class_or_item.is_a? Array |
| 25 |
items = class_or_item.reject { |e| !e.root? }
|
| 26 |
else
|
| 27 |
class_or_item = class_or_item.roots if class_or_item.is_a?(Class) |
| 28 |
items = Array(class_or_item) |
| 29 |
end
|
| 30 |
result = [] |
| 31 |
items.each do |root|
|
| 32 |
result += root.self_and_descendants.map do |i|
|
| 33 |
if mover.nil? || mover.new_record? || mover.move_possible?(i)
|
| 34 |
[yield(i), i.id]
|
| 35 |
end
|
| 36 |
end.compact
|
| 37 |
end
|
| 38 |
result |
| 39 |
end
|
| 40 |
|
| 41 |
end
|
| 42 |
end
|
| 43 |
end
|
| 44 |
end
|