Module: CollectiveIdea::Acts::NestedSet::Model::Rebuildable

Defined in:
lib/awesome_nested_set/model/rebuildable.rb

Instance Method Summary collapse

Instance Method Details

#order_for_rebuildObject



34
35
36
37
38
39
40
# File 'lib/awesome_nested_set/model/rebuildable.rb', line 34

def order_for_rebuild
  {
    left_column_name => :asc,
    right_column_name => :asc,
    primary_key => :asc
  }
end

#rebuild!(validate_nodes = true) ⇒ Object

Rebuilds the left & rights if unset or invalid. Also very useful for converting from acts_as_tree.



12
13
14
15
16
17
# File 'lib/awesome_nested_set/model/rebuildable.rb', line 12

def rebuild!(validate_nodes = true)
  # default_scope with order may break database queries so we do all operation without scope
  unscoped do
    Tree.new(self, validate_nodes).rebuild!
  end
end

#scope_for_rebuildObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/awesome_nested_set/model/rebuildable.rb', line 19

def scope_for_rebuild
  scope = proc {}

  if acts_as_nested_set_options[:scope]
    scope = proc {|node|
      scope_column_names.inject("") {|str, column_name|
        column_value = node.send(column_name)
        cond = column_value.nil? ? "IS NULL" : "= #{connection.quote(column_value)}"
        str << "AND #{connection.quote_column_name(column_name)} #{cond} "
      }
    }
  end
  scope
end