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