annotate .svn/pristine/e2/e2ef5bc95f9f13036764408192b2e2ad22d7c3aa.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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