From a1dc01ed551ee095f6c365440febd884c459b323 Mon Sep 17 00:00:00 2001 From: Tom Dunlap Date: Mon, 15 Sep 2014 16:15:34 -0400 Subject: [PATCH] Remove belongs_to relations from param if present --- lib/her/model/parse.rb | 1 + spec/model/parse_spec.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/her/model/parse.rb b/lib/her/model/parse.rb index db43d98f..7928cbb6 100644 --- a/lib/her/model/parse.rb +++ b/lib/her/model/parse.rb @@ -34,6 +34,7 @@ def parse(data) def to_params(attributes, changes={}) filtered_attributes = attributes.dup.symbolize_keys filtered_attributes.merge!(embeded_params(attributes)) + filtered_attributes.except!(*associations[:belongs_to].map{ |a| a[:data_key] }) if her_api.options[:send_only_modified_attributes] filtered_attributes = changes.symbolize_keys.keys.inject({}) do |hash, attribute| hash[attribute] = filtered_attributes[attribute] diff --git a/spec/model/parse_spec.rb b/spec/model/parse_spec.rb index 4ef1bc32..3a18b2d9 100644 --- a/spec/model/parse_spec.rb +++ b/spec/model/parse_spec.rb @@ -342,4 +342,34 @@ def to_params expect(user.to_params).to eql(:user => {:first_name => 'Someone'}) end end + + describe "removing belongs_to associations" do + before do + Her::API.setup :url => "https://api.example.com" do |builder| + builder.use Her::Middleware::DefaultParseJSON + builder.use Faraday::Request::UrlEncoded + end + + Her::API.default_api.connection.adapter :test do |stub| + stub.get("/users/1") { |env| [200, {}, { :id => 1, :first_name => "Tobias", :last_name => "Fünke", :family_id => 1 }.to_json] } + stub.get("/families/1") { |env| [200, {}, { :id => 1, :name => "Fünke" }.to_json ] } + stub.get("/families/1/users") { |env| [200, {}, [{ :id => 1, :first_name => "Tobias", :last_name => "Fünke", :family_id => 1 }].to_json] } + end + + spawn_model "User" do + belongs_to :family + end + + spawn_model "Family" do + has_many :users + end + end + + it "should not include belongs_to relations" do + family = Family.find(1) + family_user = family.users.first + expect(family_user.to_params[:family]).to be_blank + end + end + end