Module: HasHierarchy

Defined in:
lib/has_hierarchy.rb,
lib/has_hierarchy/path.rb,
lib/has_hierarchy/order.rb,
lib/has_hierarchy/version.rb,
lib/has_hierarchy/depth_cache.rb,
lib/has_hierarchy/orm_adapter.rb,
lib/has_hierarchy/counter_cache.rb,
lib/has_hierarchy/orm_adapter/mongoid.rb,
lib/has_hierarchy/orm_adapter/active_record.rb

Defined Under Namespace

Modules: ClassMethods, CounterCache, DepthCache, InstanceMethods, Order, OrmAdapter, Path

Constant Summary collapse

DEFAULT_OPTIONS =
{
  scope: nil,
  order: :position,
  path_cache: :path,
  path_part: :id,
  path_separator: '/',
  depth_cache: :depth,
  counter_cache: :children_count,
  dependent: nil
}
VERSION =
'0.4.2'

Instance Method Summary collapse

Instance Method Details

#has_hierarchy(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/has_hierarchy.rb', line 21

def has_hierarchy(options = {})
  extend ClassMethods
  include InstanceMethods

  setup_has_hierarchy_options(options)

  include Order        if options[:order]
  include Path         if options[:path_cache]
  include DepthCache   if options[:depth_cache]
  include CounterCache if options[:counter_cache]

  belongs_to :parent, class_name: self.name,
                      inverse_of: :children

  has_many :children, class_name: self.name,
                      foreign_key: :parent_id,
                      inverse_of: :parent,
                      dependent: options[:dependent]

  define_tree_scope(options[:scope])

  include HasHierarchy::OrmAdapter
end