From 7bda03bbfe50331305d5008e659435704eefc5c1 Mon Sep 17 00:00:00 2001 From: Yuri Kanivetsky Date: Wed, 22 Jul 2020 14:15:14 +0300 Subject: [PATCH] Make it return empty collection when no parent --- lib/jira/base.rb | 12 +----------- spec/jira/base_spec.rb | 5 +++++ 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/jira/base.rb b/lib/jira/base.rb index 8f98cf05..e63b204d 100644 --- a/lib/jira/base.rb +++ b/lib/jira/base.rb @@ -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) diff --git a/spec/jira/base_spec.rb b/spec/jira/base_spec.rb index 26749bd5..83f38ced 100644 --- a/spec/jira/base_spec.rb +++ b/spec/jira/base_spec.rb @@ -494,6 +494,11 @@ class JIRA::Resource::HasManyExample < JIRA::Base # :nodoc: end end + it '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' }] }