To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / cc / ccad1072a6b0c20c818238d015e69db735382ae9.svn-base @ 1297:0a574315af3e
History | View | Annotate | Download (1.39 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 |
class_or_item = class_or_item.roots if class_or_item.is_a?(Class) |
| 25 |
items = Array(class_or_item) |
| 26 |
result = [] |
| 27 |
items.each do |root| |
| 28 |
result += root.self_and_descendants.map do |i| |
| 29 |
if mover.nil? || mover.new_record? || mover.move_possible?(i) |
| 30 |
[yield(i), i.id] |
| 31 |
end |
| 32 |
end.compact |
| 33 |
end |
| 34 |
result |
| 35 |
end |
| 36 |
|
| 37 |
end |
| 38 |
end |
| 39 |
end |
| 40 |
end |