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

Revert "Dirty model integration" - give it time to finish baking first #140

Merged
merged 1 commit into from Jul 21, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions CHANGELOG.md
Expand Up @@ -9,8 +9,6 @@
* Railtie enables configuration and dev reloading of observers just like
ActiveRecord when using ARes with Rails. ARes also now runs ActiveSupport
load hooks for `:active_resource`. ([#109], [Ches Martin])
* Integrates ActiveModel::Dirty so that only changed attributes are persisted
on saves.

### Fixes ###

Expand Down
35 changes: 6 additions & 29 deletions lib/active_resource/base.rb
Expand Up @@ -601,8 +601,8 @@ def connection(refresh = false)
end

def headers
Thread.current["active.resource.headers.#{self.object_id}"] ||= {}

Thread.current["active.resource.headers.#{self.object_id}"] ||= {}
if superclass != Object && superclass.headers
Thread.current["active.resource.headers.#{self.object_id}"] = superclass.headers.merge(Thread.current["active.resource.headers.#{self.object_id}"])
else
Expand Down Expand Up @@ -1221,10 +1221,7 @@ def dup
# my_company.save # sends PUT /companies/1 (update)
def save
run_callbacks :save do
(new? ? create : update).tap do |result|
@previously_changed = changes
@changed_attributes.clear
end
new? ? create : update
end
end

Expand Down Expand Up @@ -1426,10 +1423,8 @@ def connection(refresh = false)
# Update the resource on the remote service.
def update
run_callbacks :update do
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
connection.put(element_path(prefix_options), encode, self.class.headers).tap do |response|
load_attributes_from_response(response)
end
end
end
Expand Down Expand Up @@ -1542,12 +1537,7 @@ def method_missing(method_symbol, *arguments) #:nodoc:
if method_name =~ /(=|\?)$/
case $1
when "="
field = $`
value = arguments.first

self.class.define_attribute_methods field
send("#{field}_will_change!") unless attributes[field] == value
attributes[field] = value
attributes[$`] = arguments.first
when "?"
attributes[$`]
end
Expand All @@ -1558,18 +1548,6 @@ def method_missing(method_symbol, *arguments) #:nodoc:
super
end
end

# ActiveModel::Dirty expects an attribute method that returns the value of a named attribute.
# Raise NoMethodError if the named attribute does not exist in order to preserve behavior expected by #clone.
def attribute(name)
key = name.to_s

if attributes.has_key?(key)
attributes[key]
else
raise NoMethodError
end
end
end

class Base
Expand All @@ -1581,7 +1559,6 @@ class Base
include ActiveModel::Serializers::JSON
include ActiveModel::Serializers::Xml
include ActiveResource::Reflection
include ActiveModel::Dirty
end

ActiveSupport.run_load_hooks(:active_resource, Base)
Expand Down
111 changes: 0 additions & 111 deletions test/cases/base/dirty_test.rb

This file was deleted.

19 changes: 1 addition & 18 deletions test/cases/base_test.rb
Expand Up @@ -909,26 +909,9 @@ 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) 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
assert_raise(ActiveResource::ResourceConflict) { Person.find(2).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: 0 additions & 1 deletion test/cases/observing_test.rb
Expand Up @@ -43,7 +43,6 @@ 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