Skip to content
Merged
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
1 change: 1 addition & 0 deletions her.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions lib/her/model/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
34 changes: 33 additions & 1 deletion spec/model/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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