From c863388039ff6e97447be86184d41fbf30f0f389 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 11 Jul 2009 16:59:11 -0500 Subject: Add observing hooks to ARes --- activeresource/lib/active_resource/base.rb | 11 +++++-- activeresource/test/observing_test.rb | 53 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 activeresource/test/observing_test.rb (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index f919f911e4..88a431a6d9 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -804,7 +804,8 @@ module ActiveResource # my_company.size = 10 # my_company.save # sends PUT /companies/1 (update) def save - new? ? create : update + notify(:before_save) + (new? ? create : update).tap { notify(:after_save) } end # Deletes the resource from the remote service. @@ -820,7 +821,8 @@ module ActiveResource # new_person.destroy # Person.find(new_id) # 404 (Resource Not Found) def destroy - connection.delete(element_path, self.class.headers) + notify(:before_destroy) + connection.delete(element_path, self.class.headers).tap { notify(:after_destroy) } end # Evaluates to true if this resource is not new? and is @@ -995,16 +997,20 @@ module ActiveResource # Update the resource on the remote service. def update + notify(:before_update) connection.put(element_path(prefix_options), encode, self.class.headers).tap do |response| load_attributes_from_response(response) + notify(:after_update) end end # Create (i.e., \save to the remote service) the \new resource. def create + notify(:before_create) connection.post(collection_path, encode, self.class.headers).tap do |response| self.id = id_from_response(response) load_attributes_from_response(response) + notify(:after_create) end end @@ -1088,5 +1094,6 @@ module ActiveResource class Base extend ActiveModel::Naming include CustomMethods, Validations + include ActiveModel::Observing end end diff --git a/activeresource/test/observing_test.rb b/activeresource/test/observing_test.rb new file mode 100644 index 0000000000..334b256772 --- /dev/null +++ b/activeresource/test/observing_test.rb @@ -0,0 +1,53 @@ +require 'abstract_unit' + +class ObservingTest < Test::Unit::TestCase + cattr_accessor :history + + class PersonObserver < ActiveModel::Observer + observe :person + + %w( after_create after_destroy after_save after_update + before_create before_destroy before_save before_update).each do |method| + define_method(method) { log method } + end + + private + def log(method) + (ObservingTest.history ||= []) << method.to_sym + end + end + + def setup + @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') + + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/people/1.xml", {}, @matz + mock.post "/people.xml", {}, @matz, 201, 'Location' => '/people/1.xml' + mock.put "/people/1.xml", {}, nil, 204 + mock.delete "/people/1.xml", {}, nil, 200 + end + + PersonObserver.instance + end + + def teardown + self.history = nil + end + + def test_create_fires_save_and_create_notifications + rick = Person.create(:name => 'Rick') + assert_equal [:before_save, :before_create, :after_create, :after_save], self.history + end + + def test_update_fires_save_and_update_notifications + person = Person.find(1) + person.save + assert_equal [:before_save, :before_update, :after_update, :after_save], self.history + end + + def test_destroy_fires_destroy_notifications + person = Person.find(1) + person.destroy + assert_equal [:before_destroy, :after_destroy], self.history + end +end -- cgit v1.2.3 From 7c84bbf1607bf4059de04cc4c8ec84df2334574b Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 20 Jul 2009 23:57:01 -0500 Subject: Add wrap_with_notifications helper to AMo observing --- activeresource/lib/active_resource.rb | 1 + activeresource/lib/active_resource/base.rb | 13 +++---------- activeresource/lib/active_resource/observing.rb | 10 ++++++++++ 3 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 activeresource/lib/active_resource/observing.rb (limited to 'activeresource') diff --git a/activeresource/lib/active_resource.rb b/activeresource/lib/active_resource.rb index 1dcb795a7d..fd4c199b48 100644 --- a/activeresource/lib/active_resource.rb +++ b/activeresource/lib/active_resource.rb @@ -34,6 +34,7 @@ module ActiveResource autoload :Connection, 'active_resource/connection' autoload :CustomMethods, 'active_resource/custom_methods' autoload :Formats, 'active_resource/formats' + autoload :Observing, 'active_resource/observing' autoload :Validations, 'active_resource/validations' autoload :HttpMock, 'active_resource/http_mock' end diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 88a431a6d9..bc82139dac 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -804,8 +804,7 @@ module ActiveResource # my_company.size = 10 # my_company.save # sends PUT /companies/1 (update) def save - notify(:before_save) - (new? ? create : update).tap { notify(:after_save) } + new? ? create : update end # Deletes the resource from the remote service. @@ -821,8 +820,7 @@ module ActiveResource # new_person.destroy # Person.find(new_id) # 404 (Resource Not Found) def destroy - notify(:before_destroy) - connection.delete(element_path, self.class.headers).tap { notify(:after_destroy) } + connection.delete(element_path, self.class.headers) end # Evaluates to true if this resource is not new? and is @@ -997,20 +995,16 @@ module ActiveResource # Update the resource on the remote service. def update - notify(:before_update) connection.put(element_path(prefix_options), encode, self.class.headers).tap do |response| load_attributes_from_response(response) - notify(:after_update) end end # Create (i.e., \save to the remote service) the \new resource. def create - notify(:before_create) connection.post(collection_path, encode, self.class.headers).tap do |response| self.id = id_from_response(response) load_attributes_from_response(response) - notify(:after_create) end end @@ -1093,7 +1087,6 @@ module ActiveResource class Base extend ActiveModel::Naming - include CustomMethods, Validations - include ActiveModel::Observing + include CustomMethods, Observing, Validations end end diff --git a/activeresource/lib/active_resource/observing.rb b/activeresource/lib/active_resource/observing.rb new file mode 100644 index 0000000000..94836f4bb1 --- /dev/null +++ b/activeresource/lib/active_resource/observing.rb @@ -0,0 +1,10 @@ +module ActiveResource + module Observing + extend ActiveSupport::Concern + include ActiveModel::Observing + + included do + wrap_with_notifications :create, :save, :update, :destroy + end + end +end -- cgit v1.2.3