Sha256: 7aa6cc3005b819ba4c5e548db6172316782727154f8640af2a5e4077be396263

Contents?: true

Size: 900 Bytes

Versions: 3

Compression:

Stored size: 900 Bytes

Contents

module Parentry
  module ClassMethods
    def parentry(options = {})
      self.parentry_column = options.fetch(:parentry_column, 'parentry')
      self.depth_offset = options.fetch(:depth_offset, 0)
      self.cache_depth = options.fetch(:cache_depth, false)
      self.touch_ancestors = options.fetch(:touch, false)
    end

    def arrange(options = {})
      scope =
        if (order = options.delete(:order))
          self.base_class.order_by_parentry.order(order)
        else
          self.base_class.order_by_parentry
        end

      scope.where(options).each_with_object(Hash.new { |h, k| h[k] = {} }) do |node, memo|
        insert_node = node.ancestor_ids.reduce(memo) do |subtree, ancestor_id|
          match = subtree.find { |parent, _children| parent.id == ancestor_id }
          match ? match[1] : subtree
        end
        insert_node[node] = {}
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
parentry-0.4.0 lib/parentry/class_methods.rb
parentry-0.3.1 lib/parentry/class_methods.rb
parentry-0.3.0 lib/parentry/class_methods.rb