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 "    property :\#{parent}, Fixnum, :sql_index => true\n    property :\#{left}, :\#{right}, \#{c[:type]}\n\n    def root?\n      (@\#{parent}.nil? || @\#{parent} == 0) && (@\#{left} == 1) && (@\#{right} > @\#{left})\n    end\n\n    def child?\n      (@\#{parent} &&  @\#{parent} != 0) && (@\#{left} > 1) && (@\#{right} > @\#{left})\n    end\n\n    def parent\n      if root?\n        nil\n      else\n        \#{base}[@\#{parent}]\n      end\n    end\n\n    def \#{children}_count\n      return (@\#{right} - @\#{left} - 1)/2\n    end\n\n    def full_\#{children}(options = {})\n      options.update(\#{cond_and}\"(\#{left} BETWEEN \\#\\{@\#{left}\\} AND \\\#{@\#{right}})\")\n      \#{base}.all(options)\n    end\n\n    def \#{children}(options = {})\n      options.update(\#{cond_and}\"(\#{left} > \\#\\{@\#{left}\\}) AND (\#{right} < \\\#{@\#{right}})\")\n      \#{base}.all(options)\n    end\n\n    def direct_\#{children}(options = {})\n      options.update(\#{cond_and}\"\#{parent} = \\\#{pk}\")\n      \#{base}.all(options)\n    end\n\n    def add_\#{child}(child)\n      self.reload if pk\n      child.reload if child.pk\n\n      if @\#{left}.nil? || @\#{left} == 0 || @\#{right}.nil? || @\#{right} == 0\n        @\#{left} = 1\n        @\#{right} = 2 \n      end\n\n      child.\#{parent} = pk\n      child.\#{left} = pivot = @\#{right}\n      child.\#{right} = pivot + 1\n      @\#{right} = pivot + 2\n\n      \#{base}.transaction do\n        \#{base}.update(\"\#{left} = (\#{left} + 2)\",  \#{cond_and}\"\#{left} >= \\\#{pivot}\")\n        \#{base}.update(\"\#{right} = (\#{right} + 2)\", \#{cond_and}\"\#{right} >= \\\#{pivot}\")\n      end\n\n      self.save\n      child.save\n    end\n  EOE\nend\n", __FILE__, __LINE__