Chris@909: module CollectiveIdea #:nodoc: Chris@909: module Acts #:nodoc: Chris@909: module NestedSet #:nodoc: Chris@909: # This module provides some helpers for the model classes using acts_as_nested_set. Chris@909: # It is included by default in all views. Chris@909: # Chris@909: module Helper Chris@909: # Returns options for select. Chris@909: # You can exclude some items from the tree. Chris@909: # You can pass a block receiving an item and returning the string displayed in the select. Chris@909: # Chris@909: # == Params Chris@909: # * +class_or_item+ - Class name or top level times Chris@909: # * +mover+ - The item that is being move, used to exlude impossible moves Chris@909: # * +&block+ - a block that will be used to display: { |item| ... item.name } Chris@909: # Chris@909: # == Usage Chris@909: # Chris@909: # <%= f.select :parent_id, nested_set_options(Category, @category) {|i| Chris@909: # "#{'–' * i.level} #{i.name}" Chris@909: # }) %> Chris@909: # Chris@909: def nested_set_options(class_or_item, mover = nil) Chris@909: class_or_item = class_or_item.roots if class_or_item.is_a?(Class) Chris@909: items = Array(class_or_item) Chris@909: result = [] Chris@909: items.each do |root| Chris@909: result += root.self_and_descendants.map do |i| Chris@909: if mover.nil? || mover.new_record? || mover.move_possible?(i) Chris@909: [yield(i), i.id] Chris@909: end Chris@909: end.compact Chris@909: end Chris@909: result Chris@909: end Chris@909: Chris@909: end Chris@909: end Chris@909: end Chris@909: end