Skip to content

Commit 698b4bb

Browse files
committed
Move Active Resource observer feature to this gem
This will make observers be an opt-in feature for users of activeresource like it is for the other frameworks.
1 parent ba1c906 commit 698b4bb

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ source 'https://rubygems.org'
33
# Specify your gem's dependencies in active_record-observers.gemspec
44
gemspec
55
gem 'rails', github: 'rails/rails', branch: '5-0-stable'
6+
gem 'activeresource', github: 'rails/activeresource'
67

78
gem 'mocha', require: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'rails/observers/active_model/observing'
2+
3+
module ActiveResource
4+
module Observing
5+
def self.prepended(context)
6+
context.include ActiveModel::Observing
7+
end
8+
9+
def create(*)
10+
notify_observers(:before_create)
11+
if result = super
12+
notify_observers(:after_create)
13+
end
14+
result
15+
end
16+
17+
def save(*)
18+
notify_observers(:before_save)
19+
if result = super
20+
notify_observers(:after_save)
21+
end
22+
result
23+
end
24+
25+
def update(*)
26+
notify_observers(:before_update)
27+
if result = super
28+
notify_observers(:after_update)
29+
end
30+
result
31+
end
32+
33+
def destroy(*)
34+
notify_observers(:before_destroy)
35+
if result = super
36+
notify_observers(:after_destroy)
37+
end
38+
result
39+
end
40+
end
41+
end

lib/rails/observers/railtie.rb

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ class Railtie < ::Rails::Railtie
1717
end
1818
end
1919

20+
initializer "active_resource.observer" do |app|
21+
ActiveSupport.on_load(:active_resourse) do
22+
require 'rails/observers/active_resource/observing'
23+
24+
prepend ActiveResource::Observing
25+
end
26+
end
27+
2028
config.after_initialize do |app|
2129
ActiveSupport.on_load(:active_record) do
2230
# Rails 5.1 forward-compat. AD::R is deprecated to AS::R in Rails 5.

rails-observers.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ Gem::Specification.new do |s|
2323
s.add_development_dependency 'activerecord', '>= 4.0', '< 5.1'
2424
s.add_development_dependency 'actionmailer', '>= 4.0', '< 5.1'
2525
s.add_development_dependency 'actionpack', '>= 4.0', '< 5.1'
26+
s.add_development_dependency 'activeresource', '>= 4.0', '< 5.1'
2627
s.add_development_dependency 'sqlite3', '~> 1.3'
2728
end

test/active_resource_observer_test.rb

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require 'minitest/autorun'
2+
require 'active_resource'
3+
require 'rails/observers/active_resource/observing'
4+
5+
ActiveResource::Base.prepend ActiveResource::Observing
6+
7+
require 'active_support/core_ext/hash/conversions'
8+
9+
class Person < ActiveResource::Base
10+
self.site = "http://37s.sunrise.i:3000"
11+
end
12+
13+
class ActiveResourceObservingTest < Minitest::Test
14+
cattr_accessor :history
15+
16+
class PersonObserver < ActiveModel::Observer
17+
observe :person
18+
19+
%w( after_create after_destroy after_save after_update
20+
before_create before_destroy before_save before_update).each do |method|
21+
define_method(method) { |*| log method }
22+
end
23+
24+
private
25+
def log(method)
26+
(ActiveResourceObservingTest.history ||= []) << method.to_sym
27+
end
28+
end
29+
30+
def setup
31+
@matz = { 'person' => { :id => 1, :name => 'Matz' } }.to_json
32+
33+
ActiveResource::HttpMock.respond_to do |mock|
34+
mock.get "/people/1.json", {}, @matz
35+
mock.post "/people.json", {}, @matz, 201, 'Location' => '/people/1.json'
36+
mock.put "/people/1.json", {}, nil, 204
37+
mock.delete "/people/1.json", {}, nil, 200
38+
end
39+
40+
PersonObserver.instance
41+
end
42+
43+
def teardown
44+
self.history = nil
45+
end
46+
47+
def test_create_fires_save_and_create_notifications
48+
Person.create(:name => 'Rick')
49+
assert_equal [:before_save, :before_create, :after_create, :after_save], self.history
50+
end
51+
52+
def test_update_fires_save_and_update_notifications
53+
person = Person.find(1)
54+
person.save
55+
assert_equal [:before_save, :before_update, :after_update, :after_save], self.history
56+
end
57+
58+
def test_destroy_fires_destroy_notifications
59+
person = Person.find(1)
60+
person.destroy
61+
assert_equal [:before_destroy, :after_destroy], self.history
62+
end
63+
end

0 commit comments

Comments
 (0)