Sha256: e8673dda39e1c61802a9edf2c647522a5e78762a2d0fb63067137855c7b2f267

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

# -*- coding: utf-8 -*-
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 exclude 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)
          if class_or_item.is_a? Array
            items = class_or_item.reject { |e| !e.root? }
          else
            class_or_item = class_or_item.roots if class_or_item.respond_to?(:scope)
            items = Array(class_or_item)
          end
          result = []
          items.each do |root|
            result += root.class.associate_parents(root.self_and_descendants).map do |i|
              if mover.nil? || mover.new_record? || mover.move_possible?(i)
                [yield(i), i.primary_id]
              end
            end.compact
          end
          result
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
awesome_nested_set-3.6.0 lib/awesome_nested_set/helper.rb
awesome_nested_set-3.5.0 lib/awesome_nested_set/helper.rb