Skip to content

Commit dd0afcd

Browse files
committed
Make it return empty collection when no parent
1 parent 342755e commit dd0afcd

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

lib/jira/base.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -475,17 +475,7 @@ def maybe_nested_attribute(attribute_name, nested_under = nil)
475475
end
476476

477477
def self.maybe_nested_attribute(attributes, attribute_name, nested_under = nil)
478-
return attributes[attribute_name] if nested_under.nil?
479-
if nested_under.instance_of? Array
480-
final = nested_under.inject(attributes) do |parent, key|
481-
break if parent.nil?
482-
parent[key]
483-
end
484-
return nil if final.nil?
485-
final[attribute_name]
486-
else
487-
return attributes[nested_under][attribute_name]
488-
end
478+
return attributes.dig(*[*nested_under, attribute_name])
489479
end
490480

491481
def url_with_query_params(url, query_params)

spec/jira/base_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,11 @@ class JIRA::Resource::HasManyExample < JIRA::Base # :nodoc:
494494
end
495495
end
496496

497+
fit 'returns an empty collection for empty nested has_many relationships' do
498+
subject = JIRA::Resource::HasManyExample.new(client)
499+
expect(subject.brunchmuffins.length).to eq(0)
500+
end
501+
497502
it 'allows it to be deeply nested' do
498503
subject = JIRA::Resource::HasManyExample.new(client, attrs: { 'nested' => {
499504
'breakfastscone' => { 'breakfastscones' => [{ 'id' => '123' }, { 'id' => '456' }] }

0 commit comments

Comments
 (0)