diff .svn/pristine/cc/ccad1072a6b0c20c818238d015e69db735382ae9.svn-base @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.svn/pristine/cc/ccad1072a6b0c20c818238d015e69db735382ae9.svn-base	Fri Feb 24 19:09:32 2012 +0000
@@ -0,0 +1,40 @@
+module CollectiveIdea #:nodoc:
+  module Acts #:nodoc:
+    module NestedSet #:nodoc:
+      # This module provides some helpers for the model classes using acts_as_nested_set.
+      # It is included by default in all views.
+      #
+      module Helper
+        # Returns options for select.
+        # You can exclude some items from the tree.
+        # You can pass a block receiving an item and returning the string displayed in the select.
+        #
+        # == Params
+        #  * +class_or_item+ - Class name or top level times
+        #  * +mover+ - The item that is being move, used to exlude impossible moves
+        #  * +&block+ - a block that will be used to display: { |item| ... item.name }
+        #
+        # == Usage
+        #
+        #   <%= f.select :parent_id, nested_set_options(Category, @category) {|i|
+        #       "#{'–' * i.level} #{i.name}"
+        #     }) %>
+        #
+        def nested_set_options(class_or_item, mover = nil)
+          class_or_item = class_or_item.roots if class_or_item.is_a?(Class)
+          items = Array(class_or_item)
+          result = []
+          items.each do |root|
+            result += root.self_and_descendants.map do |i|
+              if mover.nil? || mover.new_record? || mover.move_possible?(i)
+                [yield(i), i.id]
+              end
+            end.compact
+          end
+          result
+        end  
+        
+      end
+    end  
+  end
+end
\ No newline at end of file