Skip to content

Commit 8481ad7

Browse files
committed
Fix tree.find_by_tree([]) returning tree.first
I expect this should return `nil` I've also added a number of similar specs of passing all the blank values I can think of as find_by_tree didn't seem to already be tested with these various blank values.
1 parent 93f8ac4 commit 8481ad7

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/closure_tree/finders.rb

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def find_all_by_generation(generation_level)
112112

113113
# Find the node whose +ancestry_path+ is +path+
114114
def find_by_path(path, attributes = {}, parent_id = nil)
115+
return nil if path.blank?
115116
path = _ct.build_ancestry_attr_path(path, attributes)
116117
if path.size > _ct.max_join_tables
117118
return _ct.find_by_large_path(path, attributes, parent_id)

spec/tag_examples.rb

+36
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,42 @@
7272
expect(tag_class.leaves).to eq([@tag])
7373
end
7474

75+
it 'should not be found by passing find_by_path an array of blank strings' do
76+
expect(tag_class.find_by_path([''])).to be_nil
77+
end
78+
79+
it 'should not be found by passing find_by_path an empty array' do
80+
expect(tag_class.find_by_path([])).to be_nil
81+
end
82+
83+
it 'should not be found by passing find_by_path nil' do
84+
expect(tag_class.find_by_path(nil)).to be_nil
85+
end
86+
87+
it 'should not be found by passing find_by_path an empty string' do
88+
expect(tag_class.find_by_path('')).to be_nil
89+
end
90+
91+
it 'should not be found by passing find_by_path an array of nils' do
92+
expect(tag_class.find_by_path([nil])).to be_nil
93+
end
94+
95+
it 'should not be found by passing find_by_path an array with an additional blank string' do
96+
expect(tag_class.find_by_path([@tag.name, ''])).to be_nil
97+
end
98+
99+
it 'should not be found by passing find_by_path an array with an additional nil' do
100+
expect(tag_class.find_by_path([@tag.name, nil])).to be_nil
101+
end
102+
103+
it 'should be found by passing find_by_path an array with its name' do
104+
expect(tag_class.find_by_path([@tag.name])).to eq @tag
105+
end
106+
107+
it 'should be found by passing find_by_path its name' do
108+
expect(tag_class.find_by_path(@tag.name)).to eq @tag
109+
end
110+
75111
context 'with child' do
76112
before do
77113
@child = tag_class.create!(name: 'tag 2')

0 commit comments

Comments
 (0)