diff --git a/lib/closure_tree/finders.rb b/lib/closure_tree/finders.rb index ea157746..86626ebb 100644 --- a/lib/closure_tree/finders.rb +++ b/lib/closure_tree/finders.rb @@ -112,6 +112,7 @@ def find_all_by_generation(generation_level) # Find the node whose +ancestry_path+ is +path+ def find_by_path(path, attributes = {}, parent_id = nil) + return nil if path.blank? path = _ct.build_ancestry_attr_path(path, attributes) if path.size > _ct.max_join_tables return _ct.find_by_large_path(path, attributes, parent_id) diff --git a/spec/tag_examples.rb b/spec/tag_examples.rb index c1193807..8f8ec7e2 100644 --- a/spec/tag_examples.rb +++ b/spec/tag_examples.rb @@ -72,6 +72,42 @@ expect(tag_class.leaves).to eq([@tag]) end + it 'should not be found by passing find_by_path an array of blank strings' do + expect(tag_class.find_by_path([''])).to be_nil + end + + it 'should not be found by passing find_by_path an empty array' do + expect(tag_class.find_by_path([])).to be_nil + end + + it 'should not be found by passing find_by_path nil' do + expect(tag_class.find_by_path(nil)).to be_nil + end + + it 'should not be found by passing find_by_path an empty string' do + expect(tag_class.find_by_path('')).to be_nil + end + + it 'should not be found by passing find_by_path an array of nils' do + expect(tag_class.find_by_path([nil])).to be_nil + end + + it 'should not be found by passing find_by_path an array with an additional blank string' do + expect(tag_class.find_by_path([@tag.name, ''])).to be_nil + end + + it 'should not be found by passing find_by_path an array with an additional nil' do + expect(tag_class.find_by_path([@tag.name, nil])).to be_nil + end + + it 'should be found by passing find_by_path an array with its name' do + expect(tag_class.find_by_path([@tag.name])).to eq @tag + end + + it 'should be found by passing find_by_path its name' do + expect(tag_class.find_by_path(@tag.name)).to eq @tag + end + context 'with child' do before do @child = tag_class.create!(name: 'tag 2')