annotate .svn/pristine/cc/ccad1072a6b0c20c818238d015e69db735382ae9.svn-base @ 1478:5ca1f4a47171 bibplugin_db_migrations

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