Module: ActsAsOrderedTree

Defined in:
lib/acts_as_ordered_tree.rb,
lib/acts_as_ordered_tree/version.rb,
lib/acts_as_ordered_tree/validators.rb,
lib/acts_as_ordered_tree/arrangeable.rb,
lib/acts_as_ordered_tree/class_methods.rb,
lib/acts_as_ordered_tree/relation/base.rb,
lib/acts_as_ordered_tree/instance_methods.rb,
lib/acts_as_ordered_tree/relation/preloaded.rb,
lib/acts_as_ordered_tree/tenacious_transaction.rb,
lib/acts_as_ordered_tree/adapters/postgresql_adapter.rb

Defined Under Namespace

Modules: Adapters, Arrangeable, ClassMethods, Columns, InstanceMethods, Relation, TenaciousTransaction, Validators

Constant Summary collapse

PROTECTED_ATTRIBUTES_SUPPORTED =
ActiveRecord::VERSION::STRING < '4.0.0' ||
defined?(ProtectedAttributes)
PLAIN_ORDER_OPTION_SUPPORTED =

can we use has_many :children, :order => :position

ActiveRecord::VERSION::STRING < '4.0.0'
VERSION =
'1.3.1'

Instance Method Summary collapse

Instance Method Details

#acts_as_ordered_tree(options = {}) ⇒ Object

Usage

class Category < ActiveRecord::Base
  acts_as_ordered_tree :parent_column => :parent_id,
                       :position_column => :position,
                       :depth_column => :depth,
                       :counter_cache => :children_count
end

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/acts_as_ordered_tree.rb', line 21

def acts_as_ordered_tree(options = {})
  options = {
    :parent_column   => :parent_id,
    :position_column => :position,
    :depth_column    => :depth
  }.merge(options)

  class_attribute :acts_as_ordered_tree_options, :instance_writer => false
  self.acts_as_ordered_tree_options = options

  acts_as_ordered_tree_options[:depth_column] = nil unless
      columns_hash.include?(acts_as_ordered_tree_options[:depth_column].to_s)

  extend  Columns
  include Columns

  has_many_children_options = {
    :class_name    => "::#{base_class.name}",
    :foreign_key   => options[:parent_column],
    :inverse_of    => (:parent unless options[:polymorphic]),
    :dependent     => :destroy
  }

  [:before_add, :after_add, :before_remove, :after_remove].each do |callback|
    has_many_children_options[callback] = options[callback] if options.key?(callback)
  end

  if PLAIN_ORDER_OPTION_SUPPORTED
    has_many_children_options[:order] = options[:position_column]

    if scope_column_names.any?
      has_many_children_options[:conditions] = proc do
        [scope_column_names.map { |c| "#{c} = ?" }.join(' AND '),
         scope_column_names.map { |c| self[c] }]
      end
    end

    has_many :children, has_many_children_options
  else
    scope = ->(parent) {
      relation = order(options[:position_column])

      if scope_column_names.any?
        relation = relation.where(
          Hash[scope_column_names.map { |c| [c, parent[c]]}]
        )
      end

      relation
    }

    has_many :children, scope, has_many_children_options
  end

  # create parent association
  belongs_to :parent,
             :class_name => "::#{base_class.name}",
             :foreign_key => options[:parent_column],
             :counter_cache => options[:counter_cache],
             :inverse_of => (:children unless options[:polymorphic])

  include ClassMethods
  include InstanceMethods
  setup_ordered_tree_adapter
  setup_ordered_tree_callbacks
  setup_ordered_tree_validations
end