Permalink
Browse files

Merge remote-tracking branch 'rahmat-budiharso/master'

  • Loading branch information...
2 parents fc6bcdd + 4f7c7b3 commit fe6de6ca8d4c62b66eccd67b4ea553cd0a0d3764 @parndt parndt committed Sep 6, 2011
Showing with 37 additions and 5 deletions.
  1. +7 −3 lib/awesome_nested_set/helper.rb
  2. +30 −2 spec/awesome_nested_set/helper_spec.rb
View
10 lib/awesome_nested_set/helper.rb
@@ -21,8 +21,12 @@ module Helper
# }) %>
#
def nested_set_options(class_or_item, mover = nil)
- class_or_item = class_or_item.roots if class_or_item.is_a?(Class)
- items = Array(class_or_item)
+ if class_or_item.is_a? Array
+ items = class_or_item.reject { |e| !e.root? }
+ else
+ class_or_item = class_or_item.roots if class_or_item.is_a?(Class)
+ items = Array(class_or_item)
+ end
result = []
items.each do |root|
result += root.self_and_descendants.map do |i|
@@ -37,4 +41,4 @@ def nested_set_options(class_or_item, mover = nil)
end
end
end
-end
+end
View
32 spec/awesome_nested_set/helper_spec.rb
@@ -8,7 +8,7 @@
end
describe "nested_set_options" do
- def test_nested_set_options
+ it "test_nested_set_options" do
expected = [
[" Top Level", 1],
["- Child 1", 2],
@@ -23,7 +23,7 @@ def test_nested_set_options
actual.should == expected
end
- def test_nested_set_options_with_mover
+ it "test_nested_set_options_with_mover" do
expected = [
[" Top Level", 1],
["- Child 1", 2],
@@ -35,5 +35,33 @@ def test_nested_set_options_with_mover
end
actual.should == expected
end
+
+ it "test_nested_set_options_with_array_as_argument_without_mover" do
+ expected = [
+ [" Top Level", 1],
+ ["- Child 1", 2],
+ ['- Child 2', 3],
+ ['-- Child 2.1', 4],
+ ['- Child 3', 5],
+ [" Top Level 2", 6]
+ ]
+ actual = nested_set_options(Category.all) do |c|
+ "#{'-' * c.level} #{c.name}"
+ end
+ actual.should == expected
+ end
+
+ it "test_nested_set_options_with_array_as_argument_with_mover" do
+ expected = [
+ [" Top Level", 1],
+ ["- Child 1", 2],
+ ['- Child 3', 5],
+ [" Top Level 2", 6]
+ ]
+ actual = nested_set_options(Category.all, categories(:child_2)) do |c|
+ "#{'-' * c.level} #{c.name}"
+ end
+ actual.should == expected
+ end
end
end

0 comments on commit fe6de6c

Please sign in to comment.