Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore belongs_to relations when parsing params #284

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 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
30 changes: 30 additions & 0 deletions spec/model/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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