Skip to content

Commit

Permalink
Make it return empty collection when no parent
Browse files Browse the repository at this point in the history
  • Loading branch information
x-yuri committed Jul 22, 2020
1 parent 342755e commit dd0afcd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
12 changes: 1 addition & 11 deletions lib/jira/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,17 +475,7 @@ def maybe_nested_attribute(attribute_name, nested_under = nil)
end

def self.maybe_nested_attribute(attributes, attribute_name, nested_under = nil)
return attributes[attribute_name] if nested_under.nil?
if nested_under.instance_of? Array
final = nested_under.inject(attributes) do |parent, key|
break if parent.nil?
parent[key]
end
return nil if final.nil?
final[attribute_name]
else
return attributes[nested_under][attribute_name]
end
return attributes.dig(*[*nested_under, attribute_name])
end

def url_with_query_params(url, query_params)
Expand Down
5 changes: 5 additions & 0 deletions spec/jira/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,11 @@ class JIRA::Resource::HasManyExample < JIRA::Base # :nodoc:
end
end

fit 'returns an empty collection for empty nested has_many relationships' do
subject = JIRA::Resource::HasManyExample.new(client)
expect(subject.brunchmuffins.length).to eq(0)
end

it 'allows it to be deeply nested' do
subject = JIRA::Resource::HasManyExample.new(client, attrs: { 'nested' => {
'breakfastscone' => { 'breakfastscones' => [{ 'id' => '123' }, { 'id' => '456' }] }
Expand Down

0 comments on commit dd0afcd

Please sign in to comment.