Chris@1115
|
1 require 'spec_helper'
|
Chris@1115
|
2
|
Chris@1115
|
3 describe "AwesomeNestedSet" do
|
Chris@1115
|
4 before(:all) do
|
Chris@1115
|
5 self.class.fixtures :categories, :departments, :notes, :things, :brokens
|
Chris@1115
|
6 end
|
Chris@1115
|
7
|
Chris@1115
|
8 describe "defaults" do
|
Chris@1115
|
9 it "should have left_column_default" do
|
Chris@1115
|
10 Default.acts_as_nested_set_options[:left_column].should == 'lft'
|
Chris@1115
|
11 end
|
Chris@1115
|
12
|
Chris@1115
|
13 it "should have right_column_default" do
|
Chris@1115
|
14 Default.acts_as_nested_set_options[:right_column].should == 'rgt'
|
Chris@1115
|
15 end
|
Chris@1115
|
16
|
Chris@1115
|
17 it "should have parent_column_default" do
|
Chris@1115
|
18 Default.acts_as_nested_set_options[:parent_column].should == 'parent_id'
|
Chris@1115
|
19 end
|
Chris@1115
|
20
|
Chris@1115
|
21 it "should have scope_default" do
|
Chris@1115
|
22 Default.acts_as_nested_set_options[:scope].should be_nil
|
Chris@1115
|
23 end
|
Chris@1115
|
24
|
Chris@1115
|
25 it "should have left_column_name" do
|
Chris@1115
|
26 Default.left_column_name.should == 'lft'
|
Chris@1115
|
27 Default.new.left_column_name.should == 'lft'
|
Chris@1115
|
28 RenamedColumns.left_column_name.should == 'red'
|
Chris@1115
|
29 RenamedColumns.new.left_column_name.should == 'red'
|
Chris@1115
|
30 end
|
Chris@1115
|
31
|
Chris@1115
|
32 it "should have right_column_name" do
|
Chris@1115
|
33 Default.right_column_name.should == 'rgt'
|
Chris@1115
|
34 Default.new.right_column_name.should == 'rgt'
|
Chris@1115
|
35 RenamedColumns.right_column_name.should == 'black'
|
Chris@1115
|
36 RenamedColumns.new.right_column_name.should == 'black'
|
Chris@1115
|
37 end
|
Chris@1115
|
38
|
Chris@1115
|
39 it "should have parent_column_name" do
|
Chris@1115
|
40 Default.parent_column_name.should == 'parent_id'
|
Chris@1115
|
41 Default.new.parent_column_name.should == 'parent_id'
|
Chris@1115
|
42 RenamedColumns.parent_column_name.should == 'mother_id'
|
Chris@1115
|
43 RenamedColumns.new.parent_column_name.should == 'mother_id'
|
Chris@1115
|
44 end
|
Chris@1115
|
45 end
|
Chris@1115
|
46
|
Chris@1115
|
47 it "creation_with_altered_column_names" do
|
Chris@1115
|
48 lambda {
|
Chris@1115
|
49 RenamedColumns.create!()
|
Chris@1115
|
50 }.should_not raise_exception
|
Chris@1115
|
51 end
|
Chris@1115
|
52
|
Chris@1115
|
53 it "creation when existing record has nil left column" do
|
Chris@1115
|
54 assert_nothing_raised do
|
Chris@1115
|
55 Broken.create!
|
Chris@1115
|
56 end
|
Chris@1115
|
57 end
|
Chris@1115
|
58
|
Chris@1115
|
59 it "quoted_left_column_name" do
|
Chris@1115
|
60 quoted = Default.connection.quote_column_name('lft')
|
Chris@1115
|
61 Default.quoted_left_column_name.should == quoted
|
Chris@1115
|
62 Default.new.quoted_left_column_name.should == quoted
|
Chris@1115
|
63 end
|
Chris@1115
|
64
|
Chris@1115
|
65 it "quoted_right_column_name" do
|
Chris@1115
|
66 quoted = Default.connection.quote_column_name('rgt')
|
Chris@1115
|
67 Default.quoted_right_column_name.should == quoted
|
Chris@1115
|
68 Default.new.quoted_right_column_name.should == quoted
|
Chris@1115
|
69 end
|
Chris@1115
|
70
|
Chris@1115
|
71 it "left_column_protected_from_assignment" do
|
Chris@1115
|
72 lambda {
|
Chris@1115
|
73 Category.new.lft = 1
|
Chris@1115
|
74 }.should raise_exception(ActiveRecord::ActiveRecordError)
|
Chris@1115
|
75 end
|
Chris@1115
|
76
|
Chris@1115
|
77 it "right_column_protected_from_assignment" do
|
Chris@1115
|
78 lambda {
|
Chris@1115
|
79 Category.new.rgt = 1
|
Chris@1115
|
80 }.should raise_exception(ActiveRecord::ActiveRecordError)
|
Chris@1115
|
81 end
|
Chris@1115
|
82
|
Chris@1115
|
83 it "scoped_appends_id" do
|
Chris@1115
|
84 ScopedCategory.acts_as_nested_set_options[:scope].should == :organization_id
|
Chris@1115
|
85 end
|
Chris@1115
|
86
|
Chris@1115
|
87 it "roots_class_method" do
|
Chris@1115
|
88 Category.roots.should == Category.find_all_by_parent_id(nil)
|
Chris@1115
|
89 end
|
Chris@1115
|
90
|
Chris@1115
|
91 it "root_class_method" do
|
Chris@1115
|
92 Category.root.should == categories(:top_level)
|
Chris@1115
|
93 end
|
Chris@1115
|
94
|
Chris@1115
|
95 it "root" do
|
Chris@1115
|
96 categories(:child_3).root.should == categories(:top_level)
|
Chris@1115
|
97 end
|
Chris@1115
|
98
|
Chris@1115
|
99 it "root?" do
|
Chris@1115
|
100 categories(:top_level).root?.should be_true
|
Chris@1115
|
101 categories(:top_level_2).root?.should be_true
|
Chris@1115
|
102 end
|
Chris@1115
|
103
|
Chris@1115
|
104 it "leaves_class_method" do
|
Chris@1115
|
105 Category.find(:all, :conditions => "#{Category.right_column_name} - #{Category.left_column_name} = 1").should == Category.leaves
|
Chris@1115
|
106 Category.leaves.count.should == 4
|
Chris@1115
|
107 Category.leaves.should include(categories(:child_1))
|
Chris@1115
|
108 Category.leaves.should include(categories(:child_2_1))
|
Chris@1115
|
109 Category.leaves.should include(categories(:child_3))
|
Chris@1115
|
110 Category.leaves.should include(categories(:top_level_2))
|
Chris@1115
|
111 end
|
Chris@1115
|
112
|
Chris@1115
|
113 it "leaf" do
|
Chris@1115
|
114 categories(:child_1).leaf?.should be_true
|
Chris@1115
|
115 categories(:child_2_1).leaf?.should be_true
|
Chris@1115
|
116 categories(:child_3).leaf?.should be_true
|
Chris@1115
|
117 categories(:top_level_2).leaf?.should be_true
|
Chris@1115
|
118
|
Chris@1115
|
119 categories(:top_level).leaf?.should be_false
|
Chris@1115
|
120 categories(:child_2).leaf?.should be_false
|
Chris@1115
|
121 Category.new.leaf?.should be_false
|
Chris@1115
|
122 end
|
Chris@1115
|
123
|
Chris@1115
|
124
|
Chris@1115
|
125 it "parent" do
|
Chris@1115
|
126 categories(:child_2_1).parent.should == categories(:child_2)
|
Chris@1115
|
127 end
|
Chris@1115
|
128
|
Chris@1115
|
129 it "self_and_ancestors" do
|
Chris@1115
|
130 child = categories(:child_2_1)
|
Chris@1115
|
131 self_and_ancestors = [categories(:top_level), categories(:child_2), child]
|
Chris@1115
|
132 self_and_ancestors.should == child.self_and_ancestors
|
Chris@1115
|
133 end
|
Chris@1115
|
134
|
Chris@1115
|
135 it "ancestors" do
|
Chris@1115
|
136 child = categories(:child_2_1)
|
Chris@1115
|
137 ancestors = [categories(:top_level), categories(:child_2)]
|
Chris@1115
|
138 ancestors.should == child.ancestors
|
Chris@1115
|
139 end
|
Chris@1115
|
140
|
Chris@1115
|
141 it "self_and_siblings" do
|
Chris@1115
|
142 child = categories(:child_2)
|
Chris@1115
|
143 self_and_siblings = [categories(:child_1), child, categories(:child_3)]
|
Chris@1115
|
144 self_and_siblings.should == child.self_and_siblings
|
Chris@1115
|
145 lambda do
|
Chris@1115
|
146 tops = [categories(:top_level), categories(:top_level_2)]
|
Chris@1115
|
147 assert_equal tops, categories(:top_level).self_and_siblings
|
Chris@1115
|
148 end.should_not raise_exception
|
Chris@1115
|
149 end
|
Chris@1115
|
150
|
Chris@1115
|
151 it "siblings" do
|
Chris@1115
|
152 child = categories(:child_2)
|
Chris@1115
|
153 siblings = [categories(:child_1), categories(:child_3)]
|
Chris@1115
|
154 siblings.should == child.siblings
|
Chris@1115
|
155 end
|
Chris@1115
|
156
|
Chris@1115
|
157 it "leaves" do
|
Chris@1115
|
158 leaves = [categories(:child_1), categories(:child_2_1), categories(:child_3)]
|
Chris@1115
|
159 categories(:top_level).leaves.should == leaves
|
Chris@1115
|
160 end
|
Chris@1115
|
161
|
Chris@1115
|
162 it "level" do
|
Chris@1115
|
163 categories(:top_level).level.should == 0
|
Chris@1115
|
164 categories(:child_1).level.should == 1
|
Chris@1115
|
165 categories(:child_2_1).level.should == 2
|
Chris@1115
|
166 end
|
Chris@1115
|
167
|
Chris@1115
|
168 it "has_children?" do
|
Chris@1115
|
169 categories(:child_2_1).children.empty?.should be_true
|
Chris@1115
|
170 categories(:child_2).children.empty?.should be_false
|
Chris@1115
|
171 categories(:top_level).children.empty?.should be_false
|
Chris@1115
|
172 end
|
Chris@1115
|
173
|
Chris@1115
|
174 it "self_and_descendents" do
|
Chris@1115
|
175 parent = categories(:top_level)
|
Chris@1115
|
176 self_and_descendants = [parent, categories(:child_1), categories(:child_2),
|
Chris@1115
|
177 categories(:child_2_1), categories(:child_3)]
|
Chris@1115
|
178 self_and_descendants.should == parent.self_and_descendants
|
Chris@1115
|
179 self_and_descendants.count.should == parent.self_and_descendants.count
|
Chris@1115
|
180 end
|
Chris@1115
|
181
|
Chris@1115
|
182 it "descendents" do
|
Chris@1115
|
183 lawyers = Category.create!(:name => "lawyers")
|
Chris@1115
|
184 us = Category.create!(:name => "United States")
|
Chris@1115
|
185 us.move_to_child_of(lawyers)
|
Chris@1115
|
186 patent = Category.create!(:name => "Patent Law")
|
Chris@1115
|
187 patent.move_to_child_of(us)
|
Chris@1115
|
188 lawyers.reload
|
Chris@1115
|
189
|
Chris@1115
|
190 lawyers.children.size.should == 1
|
Chris@1115
|
191 us.children.size.should == 1
|
Chris@1115
|
192 lawyers.descendants.size.should == 2
|
Chris@1115
|
193 end
|
Chris@1115
|
194
|
Chris@1115
|
195 it "self_and_descendents" do
|
Chris@1115
|
196 parent = categories(:top_level)
|
Chris@1115
|
197 descendants = [categories(:child_1), categories(:child_2),
|
Chris@1115
|
198 categories(:child_2_1), categories(:child_3)]
|
Chris@1115
|
199 descendants.should == parent.descendants
|
Chris@1115
|
200 end
|
Chris@1115
|
201
|
Chris@1115
|
202 it "children" do
|
Chris@1115
|
203 category = categories(:top_level)
|
Chris@1115
|
204 category.children.each {|c| category.id.should == c.parent_id }
|
Chris@1115
|
205 end
|
Chris@1115
|
206
|
Chris@1115
|
207 it "order_of_children" do
|
Chris@1115
|
208 categories(:child_2).move_left
|
Chris@1115
|
209 categories(:child_2).should == categories(:top_level).children[0]
|
Chris@1115
|
210 categories(:child_1).should == categories(:top_level).children[1]
|
Chris@1115
|
211 categories(:child_3).should == categories(:top_level).children[2]
|
Chris@1115
|
212 end
|
Chris@1115
|
213
|
Chris@1115
|
214 it "is_or_is_ancestor_of?" do
|
Chris@1115
|
215 categories(:top_level).is_or_is_ancestor_of?(categories(:child_1)).should be_true
|
Chris@1115
|
216 categories(:top_level).is_or_is_ancestor_of?(categories(:child_2_1)).should be_true
|
Chris@1115
|
217 categories(:child_2).is_or_is_ancestor_of?(categories(:child_2_1)).should be_true
|
Chris@1115
|
218 categories(:child_2_1).is_or_is_ancestor_of?(categories(:child_2)).should be_false
|
Chris@1115
|
219 categories(:child_1).is_or_is_ancestor_of?(categories(:child_2)).should be_false
|
Chris@1115
|
220 categories(:child_1).is_or_is_ancestor_of?(categories(:child_1)).should be_true
|
Chris@1115
|
221 end
|
Chris@1115
|
222
|
Chris@1115
|
223 it "is_ancestor_of?" do
|
Chris@1115
|
224 categories(:top_level).is_ancestor_of?(categories(:child_1)).should be_true
|
Chris@1115
|
225 categories(:top_level).is_ancestor_of?(categories(:child_2_1)).should be_true
|
Chris@1115
|
226 categories(:child_2).is_ancestor_of?(categories(:child_2_1)).should be_true
|
Chris@1115
|
227 categories(:child_2_1).is_ancestor_of?(categories(:child_2)).should be_false
|
Chris@1115
|
228 categories(:child_1).is_ancestor_of?(categories(:child_2)).should be_false
|
Chris@1115
|
229 categories(:child_1).is_ancestor_of?(categories(:child_1)).should be_false
|
Chris@1115
|
230 end
|
Chris@1115
|
231
|
Chris@1115
|
232 it "is_or_is_ancestor_of_with_scope" do
|
Chris@1115
|
233 root = ScopedCategory.root
|
Chris@1115
|
234 child = root.children.first
|
Chris@1115
|
235 root.is_or_is_ancestor_of?(child).should be_true
|
Chris@1115
|
236 child.update_attribute :organization_id, 'different'
|
Chris@1115
|
237 root.is_or_is_ancestor_of?(child).should be_false
|
Chris@1115
|
238 end
|
Chris@1115
|
239
|
Chris@1115
|
240 it "is_or_is_descendant_of?" do
|
Chris@1115
|
241 categories(:child_1).is_or_is_descendant_of?(categories(:top_level)).should be_true
|
Chris@1115
|
242 categories(:child_2_1).is_or_is_descendant_of?(categories(:top_level)).should be_true
|
Chris@1115
|
243 categories(:child_2_1).is_or_is_descendant_of?(categories(:child_2)).should be_true
|
Chris@1115
|
244 categories(:child_2).is_or_is_descendant_of?(categories(:child_2_1)).should be_false
|
Chris@1115
|
245 categories(:child_2).is_or_is_descendant_of?(categories(:child_1)).should be_false
|
Chris@1115
|
246 categories(:child_1).is_or_is_descendant_of?(categories(:child_1)).should be_true
|
Chris@1115
|
247 end
|
Chris@1115
|
248
|
Chris@1115
|
249 it "is_descendant_of?" do
|
Chris@1115
|
250 categories(:child_1).is_descendant_of?(categories(:top_level)).should be_true
|
Chris@1115
|
251 categories(:child_2_1).is_descendant_of?(categories(:top_level)).should be_true
|
Chris@1115
|
252 categories(:child_2_1).is_descendant_of?(categories(:child_2)).should be_true
|
Chris@1115
|
253 categories(:child_2).is_descendant_of?(categories(:child_2_1)).should be_false
|
Chris@1115
|
254 categories(:child_2).is_descendant_of?(categories(:child_1)).should be_false
|
Chris@1115
|
255 categories(:child_1).is_descendant_of?(categories(:child_1)).should be_false
|
Chris@1115
|
256 end
|
Chris@1115
|
257
|
Chris@1115
|
258 it "is_or_is_descendant_of_with_scope" do
|
Chris@1115
|
259 root = ScopedCategory.root
|
Chris@1115
|
260 child = root.children.first
|
Chris@1115
|
261 child.is_or_is_descendant_of?(root).should be_true
|
Chris@1115
|
262 child.update_attribute :organization_id, 'different'
|
Chris@1115
|
263 child.is_or_is_descendant_of?(root).should be_false
|
Chris@1115
|
264 end
|
Chris@1115
|
265
|
Chris@1115
|
266 it "same_scope?" do
|
Chris@1115
|
267 root = ScopedCategory.root
|
Chris@1115
|
268 child = root.children.first
|
Chris@1115
|
269 child.same_scope?(root).should be_true
|
Chris@1115
|
270 child.update_attribute :organization_id, 'different'
|
Chris@1115
|
271 child.same_scope?(root).should be_false
|
Chris@1115
|
272 end
|
Chris@1115
|
273
|
Chris@1115
|
274 it "left_sibling" do
|
Chris@1115
|
275 categories(:child_1).should == categories(:child_2).left_sibling
|
Chris@1115
|
276 categories(:child_2).should == categories(:child_3).left_sibling
|
Chris@1115
|
277 end
|
Chris@1115
|
278
|
Chris@1115
|
279 it "left_sibling_of_root" do
|
Chris@1115
|
280 categories(:top_level).left_sibling.should be_nil
|
Chris@1115
|
281 end
|
Chris@1115
|
282
|
Chris@1115
|
283 it "left_sibling_without_siblings" do
|
Chris@1115
|
284 categories(:child_2_1).left_sibling.should be_nil
|
Chris@1115
|
285 end
|
Chris@1115
|
286
|
Chris@1115
|
287 it "left_sibling_of_leftmost_node" do
|
Chris@1115
|
288 categories(:child_1).left_sibling.should be_nil
|
Chris@1115
|
289 end
|
Chris@1115
|
290
|
Chris@1115
|
291 it "right_sibling" do
|
Chris@1115
|
292 categories(:child_3).should == categories(:child_2).right_sibling
|
Chris@1115
|
293 categories(:child_2).should == categories(:child_1).right_sibling
|
Chris@1115
|
294 end
|
Chris@1115
|
295
|
Chris@1115
|
296 it "right_sibling_of_root" do
|
Chris@1115
|
297 categories(:top_level_2).should == categories(:top_level).right_sibling
|
Chris@1115
|
298 categories(:top_level_2).right_sibling.should be_nil
|
Chris@1115
|
299 end
|
Chris@1115
|
300
|
Chris@1115
|
301 it "right_sibling_without_siblings" do
|
Chris@1115
|
302 categories(:child_2_1).right_sibling.should be_nil
|
Chris@1115
|
303 end
|
Chris@1115
|
304
|
Chris@1115
|
305 it "right_sibling_of_rightmost_node" do
|
Chris@1115
|
306 categories(:child_3).right_sibling.should be_nil
|
Chris@1115
|
307 end
|
Chris@1115
|
308
|
Chris@1115
|
309 it "move_left" do
|
Chris@1115
|
310 categories(:child_2).move_left
|
Chris@1115
|
311 categories(:child_2).left_sibling.should be_nil
|
Chris@1115
|
312 categories(:child_1).should == categories(:child_2).right_sibling
|
Chris@1115
|
313 Category.valid?.should be_true
|
Chris@1115
|
314 end
|
Chris@1115
|
315
|
Chris@1115
|
316 it "move_right" do
|
Chris@1115
|
317 categories(:child_2).move_right
|
Chris@1115
|
318 categories(:child_2).right_sibling.should be_nil
|
Chris@1115
|
319 categories(:child_3).should == categories(:child_2).left_sibling
|
Chris@1115
|
320 Category.valid?.should be_true
|
Chris@1115
|
321 end
|
Chris@1115
|
322
|
Chris@1115
|
323 it "move_to_left_of" do
|
Chris@1115
|
324 categories(:child_3).move_to_left_of(categories(:child_1))
|
Chris@1115
|
325 categories(:child_3).left_sibling.should be_nil
|
Chris@1115
|
326 categories(:child_1).should == categories(:child_3).right_sibling
|
Chris@1115
|
327 Category.valid?.should be_true
|
Chris@1115
|
328 end
|
Chris@1115
|
329
|
Chris@1115
|
330 it "move_to_right_of" do
|
Chris@1115
|
331 categories(:child_1).move_to_right_of(categories(:child_3))
|
Chris@1115
|
332 categories(:child_1).right_sibling.should be_nil
|
Chris@1115
|
333 categories(:child_3).should == categories(:child_1).left_sibling
|
Chris@1115
|
334 Category.valid?.should be_true
|
Chris@1115
|
335 end
|
Chris@1115
|
336
|
Chris@1115
|
337 it "move_to_root" do
|
Chris@1115
|
338 categories(:child_2).move_to_root
|
Chris@1115
|
339 categories(:child_2).parent.should be_nil
|
Chris@1115
|
340 categories(:child_2).level.should == 0
|
Chris@1115
|
341 categories(:child_2_1).level.should == 1
|
Chris@1115
|
342 categories(:child_2).left.should == 1
|
Chris@1115
|
343 categories(:child_2).right.should == 4
|
Chris@1115
|
344 Category.valid?.should be_true
|
Chris@1115
|
345 end
|
Chris@1115
|
346
|
Chris@1115
|
347 it "move_to_child_of" do
|
Chris@1115
|
348 categories(:child_1).move_to_child_of(categories(:child_3))
|
Chris@1115
|
349 categories(:child_3).id.should == categories(:child_1).parent_id
|
Chris@1115
|
350 Category.valid?.should be_true
|
Chris@1115
|
351 end
|
Chris@1115
|
352
|
Chris@1115
|
353 it "move_to_child_of_appends_to_end" do
|
Chris@1115
|
354 child = Category.create! :name => 'New Child'
|
Chris@1115
|
355 child.move_to_child_of categories(:top_level)
|
Chris@1115
|
356 child.should == categories(:top_level).children.last
|
Chris@1115
|
357 end
|
Chris@1115
|
358
|
Chris@1115
|
359 it "subtree_move_to_child_of" do
|
Chris@1115
|
360 categories(:child_2).left.should == 4
|
Chris@1115
|
361 categories(:child_2).right.should == 7
|
Chris@1115
|
362
|
Chris@1115
|
363 categories(:child_1).left.should == 2
|
Chris@1115
|
364 categories(:child_1).right.should == 3
|
Chris@1115
|
365
|
Chris@1115
|
366 categories(:child_2).move_to_child_of(categories(:child_1))
|
Chris@1115
|
367 Category.valid?.should be_true
|
Chris@1115
|
368 categories(:child_1).id.should == categories(:child_2).parent_id
|
Chris@1115
|
369
|
Chris@1115
|
370 categories(:child_2).left.should == 3
|
Chris@1115
|
371 categories(:child_2).right.should == 6
|
Chris@1115
|
372 categories(:child_1).left.should == 2
|
Chris@1115
|
373 categories(:child_1).right.should == 7
|
Chris@1115
|
374 end
|
Chris@1115
|
375
|
Chris@1115
|
376 it "slightly_difficult_move_to_child_of" do
|
Chris@1115
|
377 categories(:top_level_2).left.should == 11
|
Chris@1115
|
378 categories(:top_level_2).right.should == 12
|
Chris@1115
|
379
|
Chris@1115
|
380 # create a new top-level node and move single-node top-level tree inside it.
|
Chris@1115
|
381 new_top = Category.create(:name => 'New Top')
|
Chris@1115
|
382 new_top.left.should == 13
|
Chris@1115
|
383 new_top.right.should == 14
|
Chris@1115
|
384
|
Chris@1115
|
385 categories(:top_level_2).move_to_child_of(new_top)
|
Chris@1115
|
386
|
Chris@1115
|
387 Category.valid?.should be_true
|
Chris@1115
|
388 new_top.id.should == categories(:top_level_2).parent_id
|
Chris@1115
|
389
|
Chris@1115
|
390 categories(:top_level_2).left.should == 12
|
Chris@1115
|
391 categories(:top_level_2).right.should == 13
|
Chris@1115
|
392 new_top.left.should == 11
|
Chris@1115
|
393 new_top.right.should == 14
|
Chris@1115
|
394 end
|
Chris@1115
|
395
|
Chris@1115
|
396 it "difficult_move_to_child_of" do
|
Chris@1115
|
397 categories(:top_level).left.should == 1
|
Chris@1115
|
398 categories(:top_level).right.should == 10
|
Chris@1115
|
399 categories(:child_2_1).left.should == 5
|
Chris@1115
|
400 categories(:child_2_1).right.should == 6
|
Chris@1115
|
401
|
Chris@1115
|
402 # create a new top-level node and move an entire top-level tree inside it.
|
Chris@1115
|
403 new_top = Category.create(:name => 'New Top')
|
Chris@1115
|
404 categories(:top_level).move_to_child_of(new_top)
|
Chris@1115
|
405 categories(:child_2_1).reload
|
Chris@1115
|
406 Category.valid?.should be_true
|
Chris@1115
|
407 new_top.id.should == categories(:top_level).parent_id
|
Chris@1115
|
408
|
Chris@1115
|
409 categories(:top_level).left.should == 4
|
Chris@1115
|
410 categories(:top_level).right.should == 13
|
Chris@1115
|
411 categories(:child_2_1).left.should == 8
|
Chris@1115
|
412 categories(:child_2_1).right.should == 9
|
Chris@1115
|
413 end
|
Chris@1115
|
414
|
Chris@1115
|
415 #rebuild swaps the position of the 2 children when added using move_to_child twice onto same parent
|
Chris@1115
|
416 it "move_to_child_more_than_once_per_parent_rebuild" do
|
Chris@1115
|
417 root1 = Category.create(:name => 'Root1')
|
Chris@1115
|
418 root2 = Category.create(:name => 'Root2')
|
Chris@1115
|
419 root3 = Category.create(:name => 'Root3')
|
Chris@1115
|
420
|
Chris@1115
|
421 root2.move_to_child_of root1
|
Chris@1115
|
422 root3.move_to_child_of root1
|
Chris@1115
|
423
|
Chris@1115
|
424 output = Category.roots.last.to_text
|
Chris@1115
|
425 Category.update_all('lft = null, rgt = null')
|
Chris@1115
|
426 Category.rebuild!
|
Chris@1115
|
427
|
Chris@1115
|
428 Category.roots.last.to_text.should == output
|
Chris@1115
|
429 end
|
Chris@1115
|
430
|
Chris@1115
|
431 # doing move_to_child twice onto same parent from the furthest right first
|
Chris@1115
|
432 it "move_to_child_more_than_once_per_parent_outside_in" do
|
Chris@1115
|
433 node1 = Category.create(:name => 'Node-1')
|
Chris@1115
|
434 node2 = Category.create(:name => 'Node-2')
|
Chris@1115
|
435 node3 = Category.create(:name => 'Node-3')
|
Chris@1115
|
436
|
Chris@1115
|
437 node2.move_to_child_of node1
|
Chris@1115
|
438 node3.move_to_child_of node1
|
Chris@1115
|
439
|
Chris@1115
|
440 output = Category.roots.last.to_text
|
Chris@1115
|
441 Category.update_all('lft = null, rgt = null')
|
Chris@1115
|
442 Category.rebuild!
|
Chris@1115
|
443
|
Chris@1115
|
444 Category.roots.last.to_text.should == output
|
Chris@1115
|
445 end
|
Chris@1115
|
446
|
Chris@1115
|
447 it "should be able to rebuild without validating each record" do
|
Chris@1115
|
448 root1 = Category.create(:name => 'Root1')
|
Chris@1115
|
449 root2 = Category.create(:name => 'Root2')
|
Chris@1115
|
450 root3 = Category.create(:name => 'Root3')
|
Chris@1115
|
451
|
Chris@1115
|
452 root2.move_to_child_of root1
|
Chris@1115
|
453 root3.move_to_child_of root1
|
Chris@1115
|
454
|
Chris@1115
|
455 root2.name = nil
|
Chris@1115
|
456 root2.save!(:validate => false)
|
Chris@1115
|
457
|
Chris@1115
|
458 output = Category.roots.last.to_text
|
Chris@1115
|
459 Category.update_all('lft = null, rgt = null')
|
Chris@1115
|
460 Category.rebuild!(false)
|
Chris@1115
|
461
|
Chris@1115
|
462 Category.roots.last.to_text.should == output
|
Chris@1115
|
463 end
|
Chris@1115
|
464
|
Chris@1115
|
465 it "valid_with_null_lefts" do
|
Chris@1115
|
466 Category.valid?.should be_true
|
Chris@1115
|
467 Category.update_all('lft = null')
|
Chris@1115
|
468 Category.valid?.should be_false
|
Chris@1115
|
469 end
|
Chris@1115
|
470
|
Chris@1115
|
471 it "valid_with_null_rights" do
|
Chris@1115
|
472 Category.valid?.should be_true
|
Chris@1115
|
473 Category.update_all('rgt = null')
|
Chris@1115
|
474 Category.valid?.should be_false
|
Chris@1115
|
475 end
|
Chris@1115
|
476
|
Chris@1115
|
477 it "valid_with_missing_intermediate_node" do
|
Chris@1115
|
478 # Even though child_2_1 will still exist, it is a sign of a sloppy delete, not an invalid tree.
|
Chris@1115
|
479 Category.valid?.should be_true
|
Chris@1115
|
480 Category.delete(categories(:child_2).id)
|
Chris@1115
|
481 Category.valid?.should be_true
|
Chris@1115
|
482 end
|
Chris@1115
|
483
|
Chris@1115
|
484 it "valid_with_overlapping_and_rights" do
|
Chris@1115
|
485 Category.valid?.should be_true
|
Chris@1115
|
486 categories(:top_level_2)['lft'] = 0
|
Chris@1115
|
487 categories(:top_level_2).save
|
Chris@1115
|
488 Category.valid?.should be_false
|
Chris@1115
|
489 end
|
Chris@1115
|
490
|
Chris@1115
|
491 it "rebuild" do
|
Chris@1115
|
492 Category.valid?.should be_true
|
Chris@1115
|
493 before_text = Category.root.to_text
|
Chris@1115
|
494 Category.update_all('lft = null, rgt = null')
|
Chris@1115
|
495 Category.rebuild!
|
Chris@1115
|
496 Category.valid?.should be_true
|
Chris@1115
|
497 before_text.should == Category.root.to_text
|
Chris@1115
|
498 end
|
Chris@1115
|
499
|
Chris@1115
|
500 it "move_possible_for_sibling" do
|
Chris@1115
|
501 categories(:child_2).move_possible?(categories(:child_1)).should be_true
|
Chris@1115
|
502 end
|
Chris@1115
|
503
|
Chris@1115
|
504 it "move_not_possible_to_self" do
|
Chris@1115
|
505 categories(:top_level).move_possible?(categories(:top_level)).should be_false
|
Chris@1115
|
506 end
|
Chris@1115
|
507
|
Chris@1115
|
508 it "move_not_possible_to_parent" do
|
Chris@1115
|
509 categories(:top_level).descendants.each do |descendant|
|
Chris@1115
|
510 categories(:top_level).move_possible?(descendant).should be_false
|
Chris@1115
|
511 descendant.move_possible?(categories(:top_level)).should be_true
|
Chris@1115
|
512 end
|
Chris@1115
|
513 end
|
Chris@1115
|
514
|
Chris@1115
|
515 it "is_or_is_ancestor_of?" do
|
Chris@1115
|
516 [:child_1, :child_2, :child_2_1, :child_3].each do |c|
|
Chris@1115
|
517 categories(:top_level).is_or_is_ancestor_of?(categories(c)).should be_true
|
Chris@1115
|
518 end
|
Chris@1115
|
519 categories(:top_level).is_or_is_ancestor_of?(categories(:top_level_2)).should be_false
|
Chris@1115
|
520 end
|
Chris@1115
|
521
|
Chris@1115
|
522 it "left_and_rights_valid_with_blank_left" do
|
Chris@1115
|
523 Category.left_and_rights_valid?.should be_true
|
Chris@1115
|
524 categories(:child_2)[:lft] = nil
|
Chris@1115
|
525 categories(:child_2).save(:validate => false)
|
Chris@1115
|
526 Category.left_and_rights_valid?.should be_false
|
Chris@1115
|
527 end
|
Chris@1115
|
528
|
Chris@1115
|
529 it "left_and_rights_valid_with_blank_right" do
|
Chris@1115
|
530 Category.left_and_rights_valid?.should be_true
|
Chris@1115
|
531 categories(:child_2)[:rgt] = nil
|
Chris@1115
|
532 categories(:child_2).save(:validate => false)
|
Chris@1115
|
533 Category.left_and_rights_valid?.should be_false
|
Chris@1115
|
534 end
|
Chris@1115
|
535
|
Chris@1115
|
536 it "left_and_rights_valid_with_equal" do
|
Chris@1115
|
537 Category.left_and_rights_valid?.should be_true
|
Chris@1115
|
538 categories(:top_level_2)[:lft] = categories(:top_level_2)[:rgt]
|
Chris@1115
|
539 categories(:top_level_2).save(:validate => false)
|
Chris@1115
|
540 Category.left_and_rights_valid?.should be_false
|
Chris@1115
|
541 end
|
Chris@1115
|
542
|
Chris@1115
|
543 it "left_and_rights_valid_with_left_equal_to_parent" do
|
Chris@1115
|
544 Category.left_and_rights_valid?.should be_true
|
Chris@1115
|
545 categories(:child_2)[:lft] = categories(:top_level)[:lft]
|
Chris@1115
|
546 categories(:child_2).save(:validate => false)
|
Chris@1115
|
547 Category.left_and_rights_valid?.should be_false
|
Chris@1115
|
548 end
|
Chris@1115
|
549
|
Chris@1115
|
550 it "left_and_rights_valid_with_right_equal_to_parent" do
|
Chris@1115
|
551 Category.left_and_rights_valid?.should be_true
|
Chris@1115
|
552 categories(:child_2)[:rgt] = categories(:top_level)[:rgt]
|
Chris@1115
|
553 categories(:child_2).save(:validate => false)
|
Chris@1115
|
554 Category.left_and_rights_valid?.should be_false
|
Chris@1115
|
555 end
|
Chris@1115
|
556
|
Chris@1115
|
557 it "moving_dirty_objects_doesnt_invalidate_tree" do
|
Chris@1115
|
558 r1 = Category.create :name => "Test 1"
|
Chris@1115
|
559 r2 = Category.create :name => "Test 2"
|
Chris@1115
|
560 r3 = Category.create :name => "Test 3"
|
Chris@1115
|
561 r4 = Category.create :name => "Test 4"
|
Chris@1115
|
562 nodes = [r1, r2, r3, r4]
|
Chris@1115
|
563
|
Chris@1115
|
564 r2.move_to_child_of(r1)
|
Chris@1115
|
565 Category.valid?.should be_true
|
Chris@1115
|
566
|
Chris@1115
|
567 r3.move_to_child_of(r1)
|
Chris@1115
|
568 Category.valid?.should be_true
|
Chris@1115
|
569
|
Chris@1115
|
570 r4.move_to_child_of(r2)
|
Chris@1115
|
571 Category.valid?.should be_true
|
Chris@1115
|
572 end
|
Chris@1115
|
573
|
Chris@1115
|
574 it "multi_scoped_no_duplicates_for_columns?" do
|
Chris@1115
|
575 lambda {
|
Chris@1115
|
576 Note.no_duplicates_for_columns?
|
Chris@1115
|
577 }.should_not raise_exception
|
Chris@1115
|
578 end
|
Chris@1115
|
579
|
Chris@1115
|
580 it "multi_scoped_all_roots_valid?" do
|
Chris@1115
|
581 lambda {
|
Chris@1115
|
582 Note.all_roots_valid?
|
Chris@1115
|
583 }.should_not raise_exception
|
Chris@1115
|
584 end
|
Chris@1115
|
585
|
Chris@1115
|
586 it "multi_scoped" do
|
Chris@1115
|
587 note1 = Note.create!(:body => "A", :notable_id => 2, :notable_type => 'Category')
|
Chris@1115
|
588 note2 = Note.create!(:body => "B", :notable_id => 2, :notable_type => 'Category')
|
Chris@1115
|
589 note3 = Note.create!(:body => "C", :notable_id => 2, :notable_type => 'Default')
|
Chris@1115
|
590
|
Chris@1115
|
591 [note1, note2].should == note1.self_and_siblings
|
Chris@1115
|
592 [note3].should == note3.self_and_siblings
|
Chris@1115
|
593 end
|
Chris@1115
|
594
|
Chris@1115
|
595 it "multi_scoped_rebuild" do
|
Chris@1115
|
596 root = Note.create!(:body => "A", :notable_id => 3, :notable_type => 'Category')
|
Chris@1115
|
597 child1 = Note.create!(:body => "B", :notable_id => 3, :notable_type => 'Category')
|
Chris@1115
|
598 child2 = Note.create!(:body => "C", :notable_id => 3, :notable_type => 'Category')
|
Chris@1115
|
599
|
Chris@1115
|
600 child1.move_to_child_of root
|
Chris@1115
|
601 child2.move_to_child_of root
|
Chris@1115
|
602
|
Chris@1115
|
603 Note.update_all('lft = null, rgt = null')
|
Chris@1115
|
604 Note.rebuild!
|
Chris@1115
|
605
|
Chris@1115
|
606 Note.roots.find_by_body('A').should == root
|
Chris@1115
|
607 [child1, child2].should == Note.roots.find_by_body('A').children
|
Chris@1115
|
608 end
|
Chris@1115
|
609
|
Chris@1115
|
610 it "same_scope_with_multi_scopes" do
|
Chris@1115
|
611 lambda {
|
Chris@1115
|
612 notes(:scope1).same_scope?(notes(:child_1))
|
Chris@1115
|
613 }.should_not raise_exception
|
Chris@1115
|
614 notes(:scope1).same_scope?(notes(:child_1)).should be_true
|
Chris@1115
|
615 notes(:child_1).same_scope?(notes(:scope1)).should be_true
|
Chris@1115
|
616 notes(:scope1).same_scope?(notes(:scope2)).should be_false
|
Chris@1115
|
617 end
|
Chris@1115
|
618
|
Chris@1115
|
619 it "quoting_of_multi_scope_column_names" do
|
Chris@1115
|
620 ["\"notable_id\"", "\"notable_type\""].should == Note.quoted_scope_column_names
|
Chris@1115
|
621 end
|
Chris@1115
|
622
|
Chris@1115
|
623 it "equal_in_same_scope" do
|
Chris@1115
|
624 notes(:scope1).should == notes(:scope1)
|
Chris@1115
|
625 notes(:scope1).should_not == notes(:child_1)
|
Chris@1115
|
626 end
|
Chris@1115
|
627
|
Chris@1115
|
628 it "equal_in_different_scopes" do
|
Chris@1115
|
629 notes(:scope1).should_not == notes(:scope2)
|
Chris@1115
|
630 end
|
Chris@1115
|
631
|
Chris@1115
|
632 it "delete_does_not_invalidate" do
|
Chris@1115
|
633 Category.acts_as_nested_set_options[:dependent] = :delete
|
Chris@1115
|
634 categories(:child_2).destroy
|
Chris@1115
|
635 Category.valid?.should be_true
|
Chris@1115
|
636 end
|
Chris@1115
|
637
|
Chris@1115
|
638 it "destroy_does_not_invalidate" do
|
Chris@1115
|
639 Category.acts_as_nested_set_options[:dependent] = :destroy
|
Chris@1115
|
640 categories(:child_2).destroy
|
Chris@1115
|
641 Category.valid?.should be_true
|
Chris@1115
|
642 end
|
Chris@1115
|
643
|
Chris@1115
|
644 it "destroy_multiple_times_does_not_invalidate" do
|
Chris@1115
|
645 Category.acts_as_nested_set_options[:dependent] = :destroy
|
Chris@1115
|
646 categories(:child_2).destroy
|
Chris@1115
|
647 categories(:child_2).destroy
|
Chris@1115
|
648 Category.valid?.should be_true
|
Chris@1115
|
649 end
|
Chris@1115
|
650
|
Chris@1115
|
651 it "assigning_parent_id_on_create" do
|
Chris@1115
|
652 category = Category.create!(:name => "Child", :parent_id => categories(:child_2).id)
|
Chris@1115
|
653 categories(:child_2).should == category.parent
|
Chris@1115
|
654 categories(:child_2).id.should == category.parent_id
|
Chris@1115
|
655 category.left.should_not be_nil
|
Chris@1115
|
656 category.right.should_not be_nil
|
Chris@1115
|
657 Category.valid?.should be_true
|
Chris@1115
|
658 end
|
Chris@1115
|
659
|
Chris@1115
|
660 it "assigning_parent_on_create" do
|
Chris@1115
|
661 category = Category.create!(:name => "Child", :parent => categories(:child_2))
|
Chris@1115
|
662 categories(:child_2).should == category.parent
|
Chris@1115
|
663 categories(:child_2).id.should == category.parent_id
|
Chris@1115
|
664 category.left.should_not be_nil
|
Chris@1115
|
665 category.right.should_not be_nil
|
Chris@1115
|
666 Category.valid?.should be_true
|
Chris@1115
|
667 end
|
Chris@1115
|
668
|
Chris@1115
|
669 it "assigning_parent_id_to_nil_on_create" do
|
Chris@1115
|
670 category = Category.create!(:name => "New Root", :parent_id => nil)
|
Chris@1115
|
671 category.parent.should be_nil
|
Chris@1115
|
672 category.parent_id.should be_nil
|
Chris@1115
|
673 category.left.should_not be_nil
|
Chris@1115
|
674 category.right.should_not be_nil
|
Chris@1115
|
675 Category.valid?.should be_true
|
Chris@1115
|
676 end
|
Chris@1115
|
677
|
Chris@1115
|
678 it "assigning_parent_id_on_update" do
|
Chris@1115
|
679 category = categories(:child_2_1)
|
Chris@1115
|
680 category.parent_id = categories(:child_3).id
|
Chris@1115
|
681 category.save
|
Chris@1115
|
682 category.reload
|
Chris@1115
|
683 categories(:child_3).reload
|
Chris@1115
|
684 categories(:child_3).should == category.parent
|
Chris@1115
|
685 categories(:child_3).id.should == category.parent_id
|
Chris@1115
|
686 Category.valid?.should be_true
|
Chris@1115
|
687 end
|
Chris@1115
|
688
|
Chris@1115
|
689 it "assigning_parent_on_update" do
|
Chris@1115
|
690 category = categories(:child_2_1)
|
Chris@1115
|
691 category.parent = categories(:child_3)
|
Chris@1115
|
692 category.save
|
Chris@1115
|
693 category.reload
|
Chris@1115
|
694 categories(:child_3).reload
|
Chris@1115
|
695 categories(:child_3).should == category.parent
|
Chris@1115
|
696 categories(:child_3).id.should == category.parent_id
|
Chris@1115
|
697 Category.valid?.should be_true
|
Chris@1115
|
698 end
|
Chris@1115
|
699
|
Chris@1115
|
700 it "assigning_parent_id_to_nil_on_update" do
|
Chris@1115
|
701 category = categories(:child_2_1)
|
Chris@1115
|
702 category.parent_id = nil
|
Chris@1115
|
703 category.save
|
Chris@1115
|
704 category.parent.should be_nil
|
Chris@1115
|
705 category.parent_id.should be_nil
|
Chris@1115
|
706 Category.valid?.should be_true
|
Chris@1115
|
707 end
|
Chris@1115
|
708
|
Chris@1115
|
709 it "creating_child_from_parent" do
|
Chris@1115
|
710 category = categories(:child_2).children.create!(:name => "Child")
|
Chris@1115
|
711 categories(:child_2).should == category.parent
|
Chris@1115
|
712 categories(:child_2).id.should == category.parent_id
|
Chris@1115
|
713 category.left.should_not be_nil
|
Chris@1115
|
714 category.right.should_not be_nil
|
Chris@1115
|
715 Category.valid?.should be_true
|
Chris@1115
|
716 end
|
Chris@1115
|
717
|
Chris@1115
|
718 def check_structure(entries, structure)
|
Chris@1115
|
719 structure = structure.dup
|
Chris@1115
|
720 Category.each_with_level(entries) do |category, level|
|
Chris@1115
|
721 expected_level, expected_name = structure.shift
|
Chris@1115
|
722 expected_name.should == category.name
|
Chris@1115
|
723 expected_level.should == level
|
Chris@1115
|
724 end
|
Chris@1115
|
725 end
|
Chris@1115
|
726
|
Chris@1115
|
727 it "each_with_level" do
|
Chris@1115
|
728 levels = [
|
Chris@1115
|
729 [0, "Top Level"],
|
Chris@1115
|
730 [1, "Child 1"],
|
Chris@1115
|
731 [1, "Child 2"],
|
Chris@1115
|
732 [2, "Child 2.1"],
|
Chris@1115
|
733 [1, "Child 3" ]]
|
Chris@1115
|
734
|
Chris@1115
|
735 check_structure(Category.root.self_and_descendants, levels)
|
Chris@1115
|
736
|
Chris@1115
|
737 # test some deeper structures
|
Chris@1115
|
738 category = Category.find_by_name("Child 1")
|
Chris@1115
|
739 c1 = Category.new(:name => "Child 1.1")
|
Chris@1115
|
740 c2 = Category.new(:name => "Child 1.1.1")
|
Chris@1115
|
741 c3 = Category.new(:name => "Child 1.1.1.1")
|
Chris@1115
|
742 c4 = Category.new(:name => "Child 1.2")
|
Chris@1115
|
743 [c1, c2, c3, c4].each(&:save!)
|
Chris@1115
|
744
|
Chris@1115
|
745 c1.move_to_child_of(category)
|
Chris@1115
|
746 c2.move_to_child_of(c1)
|
Chris@1115
|
747 c3.move_to_child_of(c2)
|
Chris@1115
|
748 c4.move_to_child_of(category)
|
Chris@1115
|
749
|
Chris@1115
|
750 levels = [
|
Chris@1115
|
751 [0, "Top Level"],
|
Chris@1115
|
752 [1, "Child 1"],
|
Chris@1115
|
753 [2, "Child 1.1"],
|
Chris@1115
|
754 [3, "Child 1.1.1"],
|
Chris@1115
|
755 [4, "Child 1.1.1.1"],
|
Chris@1115
|
756 [2, "Child 1.2"],
|
Chris@1115
|
757 [1, "Child 2"],
|
Chris@1115
|
758 [2, "Child 2.1"],
|
Chris@1115
|
759 [1, "Child 3" ]]
|
Chris@1115
|
760
|
Chris@1115
|
761 check_structure(Category.root.self_and_descendants, levels)
|
Chris@1115
|
762 end
|
Chris@1115
|
763
|
Chris@1115
|
764 it "should not error on a model with attr_accessible" do
|
Chris@1115
|
765 model = Class.new(ActiveRecord::Base)
|
Chris@1115
|
766 model.table_name = 'categories'
|
Chris@1115
|
767 model.attr_accessible :name
|
Chris@1115
|
768 lambda {
|
Chris@1115
|
769 model.acts_as_nested_set
|
Chris@1115
|
770 model.new(:name => 'foo')
|
Chris@1115
|
771 }.should_not raise_exception
|
Chris@1115
|
772 end
|
Chris@1115
|
773
|
Chris@1115
|
774 describe "before_move_callback" do
|
Chris@1115
|
775 it "should fire the callback" do
|
Chris@1115
|
776 categories(:child_2).should_receive(:custom_before_move)
|
Chris@1115
|
777 categories(:child_2).move_to_root
|
Chris@1115
|
778 end
|
Chris@1115
|
779
|
Chris@1115
|
780 it "should stop move when callback returns false" do
|
Chris@1115
|
781 Category.test_allows_move = false
|
Chris@1115
|
782 categories(:child_3).move_to_root.should be_false
|
Chris@1115
|
783 categories(:child_3).root?.should be_false
|
Chris@1115
|
784 end
|
Chris@1115
|
785
|
Chris@1115
|
786 it "should not halt save actions" do
|
Chris@1115
|
787 Category.test_allows_move = false
|
Chris@1115
|
788 categories(:child_3).parent_id = nil
|
Chris@1115
|
789 categories(:child_3).save.should be_true
|
Chris@1115
|
790 end
|
Chris@1115
|
791 end
|
Chris@1115
|
792
|
Chris@1115
|
793 describe "counter_cache" do
|
Chris@1115
|
794
|
Chris@1115
|
795 it "should allow use of a counter cache for children" do
|
Chris@1115
|
796 note1 = things(:parent1)
|
Chris@1115
|
797 note1.children.count.should == 2
|
Chris@1115
|
798 end
|
Chris@1115
|
799
|
Chris@1115
|
800 it "should increment the counter cache on create" do
|
Chris@1115
|
801 note1 = things(:parent1)
|
Chris@1115
|
802 note1.children.count.should == 2
|
Chris@1115
|
803 note1[:children_count].should == 2
|
Chris@1115
|
804 note1.children.create :body => 'Child 3'
|
Chris@1115
|
805 note1.children.count.should == 3
|
Chris@1115
|
806 note1.reload
|
Chris@1115
|
807 note1[:children_count].should == 3
|
Chris@1115
|
808 end
|
Chris@1115
|
809
|
Chris@1115
|
810 it "should decrement the counter cache on destroy" do
|
Chris@1115
|
811 note1 = things(:parent1)
|
Chris@1115
|
812 note1.children.count.should == 2
|
Chris@1115
|
813 note1[:children_count].should == 2
|
Chris@1115
|
814 note1.children.last.destroy
|
Chris@1115
|
815 note1.children.count.should == 1
|
Chris@1115
|
816 note1.reload
|
Chris@1115
|
817 note1[:children_count].should == 1
|
Chris@1115
|
818 end
|
Chris@1115
|
819 end
|
Chris@1115
|
820
|
Chris@1115
|
821 describe "association callbacks on children" do
|
Chris@1115
|
822 it "should call the appropriate callbacks on the children :has_many association " do
|
Chris@1115
|
823 root = DefaultWithCallbacks.create
|
Chris@1115
|
824 root.should_not be_new_record
|
Chris@1115
|
825
|
Chris@1115
|
826 child = root.children.build
|
Chris@1115
|
827
|
Chris@1115
|
828 root.before_add.should == child
|
Chris@1115
|
829 root.after_add.should == child
|
Chris@1115
|
830
|
Chris@1115
|
831 root.before_remove.should_not == child
|
Chris@1115
|
832 root.after_remove.should_not == child
|
Chris@1115
|
833
|
Chris@1115
|
834 child.save.should be_true
|
Chris@1115
|
835 root.children.delete(child).should be_true
|
Chris@1115
|
836
|
Chris@1115
|
837 root.before_remove.should == child
|
Chris@1115
|
838 root.after_remove.should == child
|
Chris@1115
|
839 end
|
Chris@1115
|
840 end
|
Chris@1115
|
841 end
|