Skip to content
Closed
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
3 changes: 1 addition & 2 deletions lib/her/model/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def to_params(attributes, changes={})
def embeded_params(attributes)
associations[:has_many].select { |a| attributes.include?(a[:data_key])}.compact.inject({}) do |hash, association|
params = attributes[association[:data_key]].map(&:to_params)
next if params.empty?
if association[:class_name].constantize.include_root_in_json?
if her_nearby_class(association[:class_name]).include_root_in_json?
root = association[:class_name].constantize.root_element
hash[association[:data_key]] = params.map { |n| n[root] }
else
Expand Down
40 changes: 40 additions & 0 deletions spec/model/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,44 @@ def to_params
expect(user.to_params).to eql(:user => {:first_name => 'Someone'})
end
end

context 'objects or attributes empty for an association' do
before do
Her::API.setup :url => "https://api.example.com", :send_only_modified_attributes => true do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
end

spawn_model "User" do
has_many :pets
end
spawn_model "Pet"

end

subject { User.build(fullname: "Tobias Fünke", pets: []) }

it 'should not raise an error while trying to parse to params' do
expect{ subject.to_params }.to_not raise_error
end
end

context 'parsing an association of a class in a module'
before do
Her::API.setup :url => "https://api.example.com", :send_only_modified_attributes => true do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
end

spawn_model "Foo::User" do
has_many :pets
end
spawn_model "Foo::Pet"
end

subject { Foo::User.build(fullname: "Tobias Fünke", pets: []) }

it 'should not raise an error while trying to parse to params' do
expect{ subject.to_params }.to_not raise_error
end
end