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
2 changes: 1 addition & 1 deletion lib/her/model/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +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?
next {} if params.empty?
if association[:class_name].constantize.include_root_in_json?
root = association[:class_name].constantize.root_element
hash[association[:data_key]] = params.map { |n| n[root] }
Expand Down
21 changes: 21 additions & 0 deletions spec/model/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,27 @@ def present?
end
end

context "with #save and association having an empty array" do
before do
Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.get("/users/10") { |env| [200, {}, { :id => 10, comments: [] }.to_json] }
stub.put("/users/10") { |env| [200, {}] }
end
end

Foo::User.use_api Her::API.default_api
end

it "successfuly saves the record" do
@user = Foo::User.find(10)
@user.save
@user.comments.should == []
end
end

context "with #new" do
it "creates nested models from hash attibutes" do
user = Foo::User.new(:name => "vic", :comments => [{:text => "hello"}])
Expand Down