Skip to content

Commit

Permalink
ActiveResource::Base#update only puts changed attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Kovacs committed Feb 25, 2014
1 parent e5f9e6a commit b907f84
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/active_resource/base.rb
Expand Up @@ -1426,8 +1426,10 @@ def connection(refresh = false)
# Update the resource on the remote service.
def update
run_callbacks :update do
connection.put(element_path(prefix_options), encode, self.class.headers).tap do |response|
load_attributes_from_response(response)
if changed_attributes.present?
connection.put(element_path(prefix_options), encode(only: changed_attributes.keys), self.class.headers).tap do |response|
load_attributes_from_response(response)
end
end
end
end
Expand Down
19 changes: 18 additions & 1 deletion test/cases/base_test.rb
Expand Up @@ -907,9 +907,26 @@ def test_update_conflict
mock.get "/people/2.json", {}, @david
mock.put "/people/2.json", @default_request_headers, nil, 409
end
assert_raise(ActiveResource::ResourceConflict) { Person.find(2).save }
assert_raise(ActiveResource::ResourceConflict) do
person = Person.find(2)
person.name = 'new name'
person.save
end
end

def test_update_with_no_changes_does_nothing
matz = Person.find(:first)
matz.class.connection.expects(:put).never
matz.save
end

def test_update_with_changes_only_puts_changed_attributes
matz = Person.find(:first)
matz.name = 'Rick'
resp = ActiveResource::Response.new(@matz, 200)
matz.class.connection.expects(:put).with("/people/#{matz.id}.json", matz.encode(:only => :name), {}).returns(resp)
matz.save
end

######
# update_attribute(s)(!)
Expand Down
1 change: 1 addition & 0 deletions test/cases/observing_test.rb
Expand Up @@ -43,6 +43,7 @@ def test_create_fires_save_and_create_notifications

def test_update_fires_save_and_update_notifications
person = Person.find(1)
person.name = 'new name'
person.save
assert_equal [:before_save, :before_update, :after_update, :after_save], self.history
end
Expand Down

0 comments on commit b907f84

Please sign in to comment.