annotate .svn/pristine/e2/e2ef5bc95f9f13036764408192b2e2ad22d7c3aa.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 module CollectiveIdea #:nodoc:
Chris@1295 2 module Acts #:nodoc:
Chris@1295 3 module NestedSet #:nodoc:
Chris@1295 4 # This module provides some helpers for the model classes using acts_as_nested_set.
Chris@1295 5 # It is included by default in all views.
Chris@1295 6 #
Chris@1295 7 module Helper
Chris@1295 8 # Returns options for select.
Chris@1295 9 # You can exclude some items from the tree.
Chris@1295 10 # You can pass a block receiving an item and returning the string displayed in the select.
Chris@1295 11 #
Chris@1295 12 # == Params
Chris@1295 13 # * +class_or_item+ - Class name or top level times
Chris@1295 14 # * +mover+ - The item that is being move, used to exlude impossible moves
Chris@1295 15 # * +&block+ - a block that will be used to display: { |item| ... item.name }
Chris@1295 16 #
Chris@1295 17 # == Usage
Chris@1295 18 #
Chris@1295 19 # <%= f.select :parent_id, nested_set_options(Category, @category) {|i|
Chris@1295 20 # "#{'–' * i.level} #{i.name}"
Chris@1295 21 # }) %>
Chris@1295 22 #
Chris@1295 23 def nested_set_options(class_or_item, mover = nil)
Chris@1295 24 if class_or_item.is_a? Array
Chris@1295 25 items = class_or_item.reject { |e| !e.root? }
Chris@1295 26 else
Chris@1295 27 class_or_item = class_or_item.roots if class_or_item.is_a?(Class)
Chris@1295 28 items = Array(class_or_item)
Chris@1295 29 end
Chris@1295 30 result = []
Chris@1295 31 items.each do |root|
Chris@1295 32 result += root.self_and_descendants.map do |i|
Chris@1295 33 if mover.nil? || mover.new_record? || mover.move_possible?(i)
Chris@1295 34 [yield(i), i.id]
Chris@1295 35 end
Chris@1295 36 end.compact
Chris@1295 37 end
Chris@1295 38 result
Chris@1295 39 end
Chris@1295 40
Chris@1295 41 end
Chris@1295 42 end
Chris@1295 43 end
Chris@1295 44 end