Skip to content

Commit 2d72b11

Browse files
committed
Merge pull request #3 from aauthor/td-has_one_dont_parse_belongs_to
Td has one dont parse belongs to
2 parents 614beff + aec4609 commit 2d72b11

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

her.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
2222
s.add_development_dependency "rspec-its", "~> 1.0"
2323
s.add_development_dependency "fivemat", "~> 1.2"
2424
s.add_development_dependency "json", "~> 1.8"
25+
s.add_development_dependency "pry-byebug"
2526

2627
s.add_runtime_dependency "activemodel", ">= 3.0.0", "< 4.2"
2728
s.add_runtime_dependency "activesupport", ">= 3.0.0", "< 4.2"

lib/her/model/parse.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def parse(data)
3434
def to_params(attributes, changes={})
3535
filtered_attributes = attributes.dup.symbolize_keys
3636
filtered_attributes.merge!(embeded_params(attributes))
37+
filtered_attributes.except!(*associations[:belongs_to].map{ |a| a[:data_key] })
3738
if her_api.options[:send_only_modified_attributes]
3839
filtered_attributes = changes.symbolize_keys.keys.inject({}) do |hash, attribute|
3940
hash[attribute] = filtered_attributes[attribute]

spec/model/parse_spec.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def to_params
364364
end
365365
end
366366

367-
context 'parsing an association of a class in a module'
367+
context 'parsing an association of a class in a module' do
368368
before do
369369
Her::API.setup :url => "https://api.example.com", :send_only_modified_attributes => true do |builder|
370370
builder.use Her::Middleware::FirstLevelParseJSON
@@ -382,4 +382,36 @@ def to_params
382382
it 'should not raise an error while trying to parse to params' do
383383
expect{ subject.to_params }.to_not raise_error
384384
end
385+
end
386+
387+
describe "removing belongs_to associations" do
388+
before do
389+
Her::API.setup :url => "https://api.example.com" do |builder|
390+
builder.use Her::Middleware::DefaultParseJSON
391+
builder.use Faraday::Request::UrlEncoded
392+
end
393+
394+
Her::API.default_api.connection.adapter :test do |stub|
395+
stub.get("/users/1") { |env| [200, {}, { :id => 1, :first_name => "Tobias", :last_name => "Fünke", :family_id => 1 }.to_json] }
396+
stub.get("/families/1") { |env| [200, {}, { :id => 1, :name => "Fünke" }.to_json ] }
397+
stub.get("/families/1/users") { |env| [200, {}, [{ :id => 1, :first_name => "Tobias", :last_name => "Fünke", :family_id => 1 }].to_json] }
398+
end
399+
400+
spawn_model "User" do
401+
belongs_to :family
402+
end
403+
404+
spawn_model "Family" do
405+
has_many :users
406+
end
407+
end
408+
409+
it "should not include belongs_to relations" do
410+
family = Family.find(1)
411+
family_user = family.users.first
412+
expect(family_user.to_params[:family]).to be_blank
413+
end
414+
415+
end
416+
385417
end

0 commit comments

Comments
 (0)