From 5b4935fae4af866aa090e90fba0288918d461f7a Mon Sep 17 00:00:00 2001 From: Tom Dunlap Date: Mon, 15 Sep 2014 16:04:54 -0400 Subject: [PATCH 1/2] Do not parse belongs_to attirubtes into params --- lib/her/model/parse.rb | 1 + spec/model/parse_spec.rb | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/her/model/parse.rb b/lib/her/model/parse.rb index c7c6b07e..693477f1 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 f7e246b7..e6d1a445 100644 --- a/spec/model/parse_spec.rb +++ b/spec/model/parse_spec.rb @@ -364,7 +364,7 @@ def to_params end end - context 'parsing an association of a class in a module' + context 'parsing an association of a class in a module' do before do Her::API.setup :url => "https://api.example.com", :send_only_modified_attributes => true do |builder| builder.use Her::Middleware::FirstLevelParseJSON @@ -382,4 +382,36 @@ def to_params it 'should not raise an error while trying to parse to params' do expect{ subject.to_params }.to_not raise_error 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 From aec46092b242880fe1fd11d120edd7924c36b293 Mon Sep 17 00:00:00 2001 From: Tom Dunlap Date: Mon, 15 Sep 2014 16:05:40 -0400 Subject: [PATCH 2/2] Add pry as development dependency --- her.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/her.gemspec b/her.gemspec index 159b1aa1..a95ef1c1 100644 --- a/her.gemspec +++ b/her.gemspec @@ -22,6 +22,7 @@ Gem::Specification.new do |s| s.add_development_dependency "rspec-its", "~> 1.0" s.add_development_dependency "fivemat", "~> 1.2" s.add_development_dependency "json", "~> 1.8" + s.add_development_dependency "pry-byebug" s.add_runtime_dependency "activemodel", ">= 3.0.0", "< 4.2" s.add_runtime_dependency "activesupport", ">= 3.0.0", "< 4.2"