Module: Glue::NestedSets

Defined in:
lib/glue/hierarchical.rb

Overview

Implements the Nested Sets pattern for hierarchical SQL queries. – TODO: use active collections. ++

Class Method Summary collapse

Class Method Details

.included_with_parameters(base, options) ⇒ Object



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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/glue/hierarchical.rb', line 14

def self.included_with_parameters base, options
  c = {
    :left => 'lft',
    :right => 'rgt',
    :type => Fixnum,
    :parent => base.to_s.demodulize.underscore.downcase,
    :children => base.to_s.demodulize.underscore.downcase.plural
  }
  c.update(options) if options

  parent = "#{c[:parent]}_oid"
  left = c[:left]
  right = c[:right]
  children = c[:children]
  child = children.singular

   if c[:scope].is_a?(Symbol) && c[:scope].to_s !~ /_oid$/
    c[:scope] = "#{c[:scope]}_oid".intern
  end

  scope = c[:scope]

  if scope
    if scope.is_a?(Symbol)
      scope = %{(#{scope} ? "#{scope} = \#{@#{scope}}" : "#{scope} IS NULL")}
    end

    cond = 'condition => ' + scope
    cond_and = ':condition => ' + scope + ' + " AND " +'
  else
    cond = ':condition => nil'
    cond_and = ':condition => '
  end

  base.module_eval <<-EOE, __FILE__, __LINE__    
    property :#{parent}, Fixnum, :sql_index => true
    property :#{left}, :#{right}, #{c[:type]}

    def root?
      (@#{parent}.nil? || @#{parent} == 0) && (@#{left} == 1) && (@#{right} > @#{left})
    end

    def child?
      (@#{parent} &&  @#{parent} != 0) && (@#{left} > 1) && (@#{right} > @#{left})
    end

    def parent
      if root?
        nil
      else
        #{base}[@#{parent}]
      end
    end

    def #{children}_count
      return (@#{right} - @#{left} - 1)/2
    end

    def full_#{children}(options = {})
      options.update(#{cond_and}"(#{left} BETWEEN \#\{@#{left}\} AND \#{@#{right}})")
      #{base}.all(options)
    end

    def #{children}(options = {})
      options.update(#{cond_and}"(#{left} > \#\{@#{left}\}) AND (#{right} < \#{@#{right}})")
      #{base}.all(options)
    end

    def direct_#{children}(options = {})
      options.update(#{cond_and}"#{parent} = \#{pk}")
      #{base}.all(options)
    end

    def add_#{child}(child)
      self.reload if pk
      child.reload if child.pk

      if @#{left}.nil? || @#{left} == 0 || @#{right}.nil? || @#{right} == 0
        @#{left} = 1
        @#{right} = 2 
      end

      child.#{parent} = pk
      child.#{left} = pivot = @#{right}
      child.#{right} = pivot + 1
      @#{right} = pivot + 2

      #{base}.transaction do
        #{base}.update("#{left} = (#{left} + 2)",  #{cond_and}"#{left} >= \#{pivot}")
        #{base}.update("#{right} = (#{right} + 2)", #{cond_and}"#{right} >= \#{pivot}")
      end

      self.save
      child.save
    end
  EOE
end