Module: RecursiveTreeScopes::ModelMixin

Defined in:
lib/activerecord-recursive_tree_scopes.rb

Instance Method Summary collapse

Instance Method Details

#has_ancestors(ancestors_name = :ancestors, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/activerecord-recursive_tree_scopes.rb', line 3

def has_ancestors(ancestors_name = :ancestors, options = {})
  options[:key] ||= :parent_id
  
  scope ancestors_name, lambda{ |record|
    RecursiveTreeScopes::Scopes.ancestors_for(record, options[:key])
  }

  class_eval "    def \#{ancestors_name}\n      self.class.\#{ancestors_name}(self)\n    end\n  EOF\nend\n"

#has_descendants(descendants_name = :descendants, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/activerecord-recursive_tree_scopes.rb', line 17

def has_descendants(descendants_name = :descendants, options = {})
  options[:key] ||= :parent_id

  scope descendants_name, lambda{ |record|
    RecursiveTreeScopes::Scopes.descendants_for(record, options[:key])
  }

  class_eval "    def \#{descendants_name}\n      self.class.\#{descendants_name}(self)\n    end\n  EOF\nend\n"