aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-07-25 12:55:01 +0100
committerPratik Naik <pratiknaik@gmail.com>2009-07-25 12:55:01 +0100
commitc6b16fc2aada37aab1949cd9a1e46a2d9ccc8381 (patch)
treeea13a4e420a94ed7b552529ca9dc4b02680203d2 /activeresource
parentf2d65a456fd93fd3a220f85c1001f0180bfdd6be (diff)
parent0c68d23f19010379a9320690ca17a26743c8f071 (diff)
downloadrails-c6b16fc2aada37aab1949cd9a1e46a2d9ccc8381.tar.gz
rails-c6b16fc2aada37aab1949cd9a1e46a2d9ccc8381.tar.bz2
rails-c6b16fc2aada37aab1949cd9a1e46a2d9ccc8381.zip
Merge commit 'mainstream/master'
Diffstat (limited to 'activeresource')
-rw-r--r--activeresource/lib/active_resource.rb1
-rw-r--r--activeresource/lib/active_resource/base.rb2
-rw-r--r--activeresource/lib/active_resource/observing.rb10
-rw-r--r--activeresource/test/observing_test.rb53
4 files changed, 65 insertions, 1 deletions
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 f919f911e4..bc82139dac 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -1087,6 +1087,6 @@ module ActiveResource
class Base
extend ActiveModel::Naming
- include CustomMethods, Validations
+ 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
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