Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make has_many relationships return an empty collection when no parent #367

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

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' }] }
Expand Down