Sha256: c68272ef7886911ae2cd4d58298718790094dc85f822598fb949baaf47ecc7e7

Contents?: true

Size: 1.15 KB

Versions: 11

Compression:

Stored size: 1.15 KB

Contents

module ActiveTools
  module ActionPack
    module ActionView
      module PerformAsTree
        def self.line(item, children_method, depth = 0, &block)
          yield item, depth
          item.send(children_method).each do |child|
            line(child, children_method, depth+1, &block)
          end
        end
      end
    end
  end
  
  module OnLoadActionView
    
    def perform_as_tree(scope, options = {}, &block)
      options = options.with_indifferent_access
      children_method = options[:children_method]||:children
      parent_method = options[:parent_method]||:parent
      id_key = options[:id]||nil
      scope = case scope
      when ::ActiveRecord::Relation then
        parent_key = scope.klass.reflections[children_method].foreign_key
        scope.where(parent_key => id_key)
      when ::Array, ::Set then
        scope.select {|item| item.send(parent_method) == id_key}
      else
        raise(TypeError, "ActiveRecord::Relation, Array or Set expected, #{scope.class.name} passed!")
      end
      scope.each do |item|
        ActionPack::ActionView::PerformAsTree.line(item, children_method, 0, &block)
      end
    end
    
  end
  
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
active_tools-0.0.19 lib/active_tools/action_pack/action_view/perform_as_tree.rb
active_tools-0.0.18 lib/active_tools/action_pack/action_view/perform_as_tree.rb
active_tools-0.0.17 lib/active_tools/action_pack/action_view/perform_as_tree.rb
active_tools-0.0.16 lib/active_tools/action_pack/action_view/perform_as_tree.rb
active_tools-0.0.15 lib/active_tools/action_pack/action_view/perform_as_tree.rb
active_tools-0.0.14 lib/active_tools/action_pack/action_view/perform_as_tree.rb
active_tools-0.0.13 lib/active_tools/action_pack/action_view/perform_as_tree.rb
active_tools-0.0.12 lib/active_tools/action_pack/action_view/perform_as_tree.rb
active_tools-0.0.11 lib/active_tools/action_pack/action_view/perform_as_tree.rb
active_tools-0.0.10 lib/active_tools/action_pack/action_view/perform_as_tree.rb
active_tools-0.0.6 lib/active_tools/action_pack/action_view/perform_as_tree.rb