File: rebuildable.rb

package info (click to toggle)
ruby-awesome-nested-set 3.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 172 kB
  • sloc: ruby: 1,044; makefile: 2
file content (46 lines) | stat: -rw-r--r-- 1,282 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
require 'awesome_nested_set/tree'

module CollectiveIdea
  module Acts
    module NestedSet
      module Model
        module Rebuildable


          # Rebuilds the left & rights if unset or invalid.
          # Also very useful for converting from acts_as_tree.
          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

          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

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

      end
    end
  end
end