Module: Spider::Model::Tree::ClassMethods

Defined in:
lib/spiderfw/model/mixins/tree.rb

Instance Method Summary collapse

Instance Method Details

#extend_queryset(qs) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/spiderfw/model/mixins/tree.rb', line 105

def extend_queryset(qs)
		   super
    @elements.each do |name, el|
        qs_module = el.attributes[:queryset_module]
        qs.extend(qs_module) if qs_module
    end
end

#remove_element(el) ⇒ Object



203
204
205
206
207
208
209
210
211
212
# File 'lib/spiderfw/model/mixins/tree.rb', line 203

def remove_element(el)
    el = el.name if el.is_a?(Spider::Model::Element)
    element = @elements[el] if @elements
    return super if !element || element.attributes[:association] != :tree
    remove_element(element.attributes[:reverse])
    remove_element(element.attributes[:tree_left])
    remove_element(element.attributes[:tree_right])
    remove_element(element.attributes[:tree_depth])
    return super
end

#tree(name, attributes = {}) ⇒ Object



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/spiderfw/model/mixins/tree.rb', line 65

def tree(name, attributes={})
    attributes[:association] = :tree
    attributes[:multiple] = true
    attributes[:reverse] ||= :"#{name}_parent"
    attributes[:reverse_attributes] = {
        :association => :tree_parent,
        :tree_element => name
    }.merge(attributes[:reverse_attributes] || {})
    attributes[:tree_left] ||= :"#{name}_left"
    attributes[:tree_right] ||= :"#{name}_right"
    attributes[:tree_depth] ||= :"#{name}_depth"
    attributes[:tree_position] ||= :"#{name}_position"
    choice(attributes[:reverse], self, attributes[:reverse_attributes])
    element(name, self, attributes)
    order = attributes[:order] == false ? false : true
    element(attributes[:tree_left], Fixnum, :hidden => true, :tree_element => name, :order => order)
    element(attributes[:tree_right], Fixnum, :hidden => true, :tree_element => name)
    element(attributes[:tree_depth], Fixnum, :unmapped => true, :hidden => true, :tree_element => name)
    element(attributes[:tree_position], Fixnum, :unmapped => true, :hidden => true, :tree_element => name)
#               sequence(name)
    qs_module ||= Module.new
    
    qs_module.module_eval do
        
        define_method("#{name}_roots") do
            qs = self.clone
            qs.condition[attributes[:reverse]] = nil
            qs
        end

        define_method("#{name}_leafs") do
            qs = self.clone
            qs.condition[attributes[:tree_left]] = QueryFuncs::Expression.new(":#{attributes[:tree_right]} - 1")
            qs
        end
        
        
    end
    @elements[name].attributes[:queryset_module] = qs_module
    
    def extend_queryset(qs)
		   super
        @elements.each do |name, el|
            qs_module = el.attributes[:queryset_module]
            qs.extend(qs_module) if qs_module
        end
    end
    
    (class << self; self; end).instance_eval do
        define_method("#{name}_roots") do
            QuerySet.autoloading(self).send("#{name}_roots")
        end
        
        define_method("#{name}_leafs") do
            QuerySet.autoloading(self).send("#{name}_leafs")
           
        end
        #TODO:la condizione non viene gestita, tolta per problemi in ruby 1.9.3 nel rebuild_menu
        define_method("#{name}_all") do #|condition|
         qs = QuerySet.static(self)
            self.send("#{name}_roots").each do |root|
                ta = root.tree_all(name)
                qs += ta if ta
            end
            return qs
        end
        
    end
    
    define_method("#{name}_leaf?") do
        element = self.class.elements[name]
        left_el = element.attributes[:tree_left]
        right_el = element.attributes[:tree_right]
        left = get(left_el)
        right = get(right_el)
        return left == (right - 1)
    end

    
    define_method("#{name}_all") do
        tree_all(name)
    end
    
    define_method(attributes[:tree_depth]) do
        ivar = :"@#{ attributes[:tree_depth] }"
        return instance_variable_get(ivar)
    end
    
    define_method("#{name}_path") do
        element = self.class.elements[name]
        left_el = element.attributes[:tree_left]
        right_el = element.attributes[:tree_right]
        left = get(left_el)
        right = get(right_el)
        return nil unless (left && right)
        c = Condition.and
        c.set(tree_left, '<', left)
        c.set(tree_right, '>', right)
        q = Query.new(c)
        q.order_by(left_el)
        return element.model.find(q)
    end
    
    define_method("#{name}_append_first") do |new_child|
        
    end
    
    define_method("#{name}_append_after") do |new_child, child|
        element = self.class.elements[name]
        left_el = element.attributes[:tree_left]
        right_el = element.attributes[:tree_right]
        parent_el = element.attributes[:reverse]
        if (child.get(left_el))
            
        end
    end
    
    define_method(attributes[:tree_position]) do
        i = instance_variable_get("@#{attributes[:tree_position]}")
        return i if i
        element = self.class.elements[name]
        left_el = element.attributes[:tree_left]
        right_el = element.attributes[:tree_right]
        parent_el = element.attributes[:reverse]
        parent = self.get(parent_el)
        return nil unless parent
        cnt = 0
        parent.get(name).each do |sub|
            cnt += 1
            break if sub == self
        end
        cnt = nil if cnt == 0
        cnt
    end

end