julik-make_like_a_tree 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +5 -1
- data/lib/make_like_a_tree.rb +21 -10
- data/test/test_ordered_tree.rb +24 -0
- metadata +1 -1
data/README.txt
CHANGED
@@ -59,7 +59,11 @@ Add a bare init file to your app and there:
|
|
59
59
|
require 'make_like_tree'
|
60
60
|
Julik::MakeLikeTree.bootstrap!
|
61
61
|
|
62
|
-
|
62
|
+
Or just vendorize it, it has a built-in init.rb. You can also use the
|
63
|
+
plugin without unpacking it, to do so put the following in the config:
|
64
|
+
|
65
|
+
config.gem "make_like_a_tree"
|
66
|
+
config.after_initialize { Julik::MakeLikeTree.bootstrap! }
|
63
67
|
|
64
68
|
== LICENSE:
|
65
69
|
|
data/lib/make_like_a_tree.rb
CHANGED
@@ -3,7 +3,7 @@ module Julik
|
|
3
3
|
class ImpossibleReparent < RuntimeError
|
4
4
|
end
|
5
5
|
|
6
|
-
VERSION = '1.0.
|
6
|
+
VERSION = '1.0.3'
|
7
7
|
def self.included(base) #:nodoc:
|
8
8
|
super
|
9
9
|
base.extend(ClassMethods)
|
@@ -44,6 +44,7 @@ module Julik
|
|
44
44
|
|
45
45
|
after_create :apply_parenting_after_create
|
46
46
|
|
47
|
+
|
47
48
|
# before_update :register_parent_id_before_update, :unless => :new_record?
|
48
49
|
# after_update :replant_after_update
|
49
50
|
|
@@ -300,21 +301,34 @@ module Julik
|
|
300
301
|
end
|
301
302
|
alias_method :children_count, :child_count
|
302
303
|
|
303
|
-
# Shortcut to determine if our left and right values allow for possible children
|
304
|
+
# Shortcut to determine if our left and right values allow for possible children.
|
305
|
+
# Note the difference in wording between might_have and has - if this method returns false,
|
306
|
+
# it means you should look no further. If it returns true, you should really examine
|
307
|
+
# the children to be sure
|
304
308
|
def might_have_children?
|
305
309
|
(self[right_col_name] - self[left_col_name]) > 1
|
306
310
|
end
|
307
311
|
|
308
312
|
# Returns a set of itself and all of its nested children
|
309
|
-
def full_set
|
313
|
+
def full_set(extras = {})
|
310
314
|
[self] + all_children
|
311
315
|
end
|
312
316
|
alias_method :all_children_and_self, :full_set
|
313
317
|
|
314
318
|
# Returns a set of all of its children and nested children
|
315
|
-
def all_children
|
319
|
+
def all_children(extras = {})
|
316
320
|
return [] unless might_have_children? # optimization shortcut
|
317
|
-
self.class.find(:all,
|
321
|
+
self.class.scoped(scope_hash_for_branch).find(:all, extras)
|
322
|
+
end
|
323
|
+
|
324
|
+
# Returns scoping options suitable for fetching all children
|
325
|
+
def scope_hash_for_branch
|
326
|
+
{:conditions => conditions_for_all_children, :order => "#{left_col_name} ASC" }
|
327
|
+
end
|
328
|
+
|
329
|
+
# Returns scopint options suitable for fetching direct children
|
330
|
+
def scope_hash_for_direct_children
|
331
|
+
{:conditions => "#{scope_condition} AND #{parent_column} = #{self.id}", :order => "#{left_col_name} ASC"}
|
318
332
|
end
|
319
333
|
|
320
334
|
# Get conditions for direct and indirect children of this record
|
@@ -340,12 +354,9 @@ module Julik
|
|
340
354
|
end
|
341
355
|
|
342
356
|
# Returns a set of only this entry's immediate children, also ordered by position
|
343
|
-
def direct_children
|
357
|
+
def direct_children(extras = {})
|
344
358
|
return [] unless might_have_children? # optimize!
|
345
|
-
self.class.find(:all,
|
346
|
-
:conditions => "#{scope_condition} AND #{parent_column} = #{self.id}",
|
347
|
-
:order => 'lft ASC'
|
348
|
-
)
|
359
|
+
self.class.scoped(scope_hash_for_direct_children).find(:all, extras)
|
349
360
|
end
|
350
361
|
|
351
362
|
# Make this item a root node
|
data/test/test_ordered_tree.rb
CHANGED
@@ -321,6 +321,30 @@ context "A Node used with OrderedTree should" do
|
|
321
321
|
child.add_child(root).should.equal false
|
322
322
|
end
|
323
323
|
|
324
|
+
specify "support additional find options via scoped finds on all_children" do
|
325
|
+
root = emit :name => "foo"
|
326
|
+
child = emit :name => "bar", :parent_id => root.id
|
327
|
+
another_child = emit :name => "another", :parent_id => root.id
|
328
|
+
|
329
|
+
reload(root)
|
330
|
+
|
331
|
+
root.all_children.should.equal [child, another_child]
|
332
|
+
root.all_children(:conditions => {:name => "another"}).should.equal [another_child]
|
333
|
+
end
|
334
|
+
|
335
|
+
specify "support additional find options via scoped finds on direct_children" do
|
336
|
+
root = emit :name => "foo"
|
337
|
+
anoter_root = emit :name => "another"
|
338
|
+
|
339
|
+
child = emit :name => "bar", :parent_id => root.id
|
340
|
+
another_child = emit :name => "another", :parent_id => root.id
|
341
|
+
|
342
|
+
reload(root)
|
343
|
+
|
344
|
+
root.direct_children.should.equal [child, another_child]
|
345
|
+
root.direct_children(:conditions => {:name => "another"}).should.equal [another_child]
|
346
|
+
end
|
347
|
+
|
324
348
|
specify "support promote_to_root" do
|
325
349
|
a, b = emit_many(2)
|
326
350
|
c = emit(:name => "Subtree", :parent_id => a.id)
|