Skip to content

Commit

Permalink
Merge pull request ClosureTree#288 from robotdana/dana/find_by_path_e…
Browse files Browse the repository at this point in the history
…mpty_array

Fix `tree.find_by_tree([])`
  • Loading branch information
mceachen authored Nov 1, 2017
2 parents 93f8ac4 + 8481ad7 commit 48c3aa9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/closure_tree/finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions spec/tag_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 48c3aa9

Please sign in to comment.