rails-observers 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/Gemfile +1 -4
- data/README.md +12 -0
- data/lib/rails-observers.rb +1 -33
- data/lib/rails/observers/action_controller/caching.rb +1 -1
- data/lib/rails/observers/action_controller/caching/sweeping.rb +0 -2
- data/lib/rails/observers/active_model.rb +1 -0
- data/lib/rails/observers/railtie.rb +33 -0
- data/lib/rails/observers/version.rb +1 -1
- data/rails-observers.gemspec +6 -6
- data/rails-observers.gemspec.erb +5 -5
- data/test/transaction_callbacks_test.rb +4 -36
- metadata +24 -38
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4ebf1dea2a2c82e54123428320e6685c5765da02
|
4
|
+
data.tar.gz: 52ef78d1ccb52ed81cd43e91d582b18976f18123
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c2465a6f154cd1de2b3439333d2abc2719db0dd0a3294779e09df4caf31f37b66fe61a4ae0622a558011bd6e96a383a243814ee7afee059649aac335cbd24f7f
|
7
|
+
data.tar.gz: ee0dd0aca9d35994d059e92341861319ca4aab3aa00ae104b19e28d94a646798163fb926f00fbe5e5eb177bf1598d331e4e3ceb19eda161323fc0b151c1a4a02
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
@@ -3,9 +3,6 @@ source 'https://rubygems.org'
|
|
3
3
|
# Specify your gem's dependencies in active_record-observers.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
-
gem 'activerecord-deprecated_finders'
|
7
|
-
gem 'journey', github: 'rails/journey'
|
8
|
-
|
9
|
-
gem 'rails', github: 'rails/rails', branch: 'master'
|
6
|
+
gem 'activerecord-deprecated_finders'
|
10
7
|
|
11
8
|
gem 'mocha', require: false
|
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
[![Build Status](https://secure.travis-ci.org/rails/rails-observers.png)](https://travis-ci.org/rails/rails-observers)
|
1
2
|
# Rails::Observers
|
2
3
|
|
3
4
|
Rails Observers (removed from core in Rails 4.0)
|
@@ -55,6 +56,17 @@ end
|
|
55
56
|
|
56
57
|
This Observer uses logger to log when specific callbacks are triggered.
|
57
58
|
|
59
|
+
Please note that observers are called in the order that they are defined. That means that callbacks in an observer
|
60
|
+
will always be called *after* callbacks defined in the model itself. Likewise, `has_one` and `has_many`
|
61
|
+
use callbacks to enforce `:dependent => :destroy`. Therefore, associated records will be destroyed before
|
62
|
+
the observer's `before_destory` is called.
|
63
|
+
|
64
|
+
For an observer to be active, it must be registered first. This can be done by adding the following line into the `application.rb`:
|
65
|
+
|
66
|
+
config.active_record.observers = :contact_observer
|
67
|
+
|
68
|
+
Obeservers can also be registered on an environment-specific basis by simply using the corresponding environment's configuration file instead of `application.rb`.
|
69
|
+
|
58
70
|
### Action Controller Sweeper
|
59
71
|
|
60
72
|
Sweepers are the terminators of the caching world and responsible for expiring caches when model objects change.
|
data/lib/rails-observers.rb
CHANGED
@@ -1,34 +1,2 @@
|
|
1
|
-
require 'rails'
|
1
|
+
require 'rails/observers/railtie' if defined? Rails
|
2
2
|
require 'rails/observers/version'
|
3
|
-
|
4
|
-
module Rails
|
5
|
-
module Observers
|
6
|
-
class Railtie < ::Rails::Railtie
|
7
|
-
initializer "active_record.observer", :before => "active_record.set_configs" do |app|
|
8
|
-
ActiveSupport.on_load(:active_record) do
|
9
|
-
require "rails/observers/activerecord/active_record"
|
10
|
-
|
11
|
-
if observers = app.config.respond_to?(:active_record) && app.config.active_record.delete(:observers)
|
12
|
-
send :observers=, observers
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
initializer "action_controller.caching.sweppers" do
|
18
|
-
ActiveSupport.on_load(:action_controller) do
|
19
|
-
require "rails/observers/action_controller/caching"
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
config.after_initialize do |app|
|
24
|
-
ActiveSupport.on_load(:active_record) do
|
25
|
-
ActiveRecord::Base.instantiate_observers
|
26
|
-
|
27
|
-
ActionDispatch::Reloader.to_prepare do
|
28
|
-
ActiveRecord::Base.instantiate_observers
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -30,8 +30,6 @@ module ActionController #:nodoc:
|
|
30
30
|
# cache_sweeper OpenBar::Sweeper, :only => [ :edit, :destroy, :share ]
|
31
31
|
# end
|
32
32
|
module Sweeping
|
33
|
-
extend ActiveSupport::Concern
|
34
|
-
|
35
33
|
module ClassMethods #:nodoc:
|
36
34
|
def cache_sweeper(*sweepers)
|
37
35
|
configuration = sweepers.extract_options!
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rails/observers/active_model/active_model'
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rails/railtie'
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Observers
|
5
|
+
class Railtie < ::Rails::Railtie
|
6
|
+
initializer "active_record.observer", :before => "active_record.set_configs" do |app|
|
7
|
+
ActiveSupport.on_load(:active_record) do
|
8
|
+
require "rails/observers/activerecord/active_record"
|
9
|
+
|
10
|
+
if observers = app.config.respond_to?(:active_record) && app.config.active_record.delete(:observers)
|
11
|
+
send :observers=, observers
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
initializer "action_controller.caching.sweepers" do
|
17
|
+
ActiveSupport.on_load(:action_controller) do
|
18
|
+
require "rails/observers/action_controller/caching"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
config.after_initialize do |app|
|
23
|
+
ActiveSupport.on_load(:active_record) do
|
24
|
+
ActiveRecord::Base.instantiate_observers
|
25
|
+
|
26
|
+
ActionDispatch::Reloader.to_prepare do
|
27
|
+
ActiveRecord::Base.instantiate_observers
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/rails-observers.gemspec
CHANGED
@@ -10,17 +10,17 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.homepage = "https://github.com/rails/rails-observers"
|
11
11
|
s.version = Rails::Observers::VERSION
|
12
12
|
|
13
|
-
s.files = [".gitignore",".travis.yml","Gemfile","LICENSE","README.md","Rakefile","lib/generators/active_record/observer/observer_generator.rb","lib/generators/active_record/observer/templates/observer.rb","lib/generators/rails/observer/USAGE","lib/generators/rails/observer/observer_generator.rb","lib/generators/test_unit/observer/observer_generator.rb","lib/generators/test_unit/observer/templates/unit_test.rb","lib/rails-observers.rb","lib/rails/observers/action_controller/caching.rb","lib/rails/observers/action_controller/caching/sweeping.rb","lib/rails/observers/active_model/active_model.rb","lib/rails/observers/active_model/observer_array.rb","lib/rails/observers/active_model/observing.rb","lib/rails/observers/activerecord/active_record.rb","lib/rails/observers/activerecord/base.rb","lib/rails/observers/activerecord/observer.rb","lib/rails/observers/version.rb","rails-observers.gemspec","rails-observers.gemspec.erb","test/configuration_test.rb","test/console_test.rb","test/fixtures/developers.yml","test/fixtures/minimalistics.yml","test/fixtures/topics.yml","test/generators/generators_test_helper.rb","test/generators/namespaced_generators_test.rb","test/generators/observer_generator_test.rb","test/helper.rb","test/isolation/abstract_unit.rb","test/lifecycle_test.rb","test/models/observers.rb","test/observer_array_test.rb","test/observing_test.rb","test/rake_test.rb","test/sweeper_test.rb","test/transaction_callbacks_test.rb"]
|
13
|
+
s.files = [".gitignore",".travis.yml","Gemfile","LICENSE","README.md","Rakefile","lib/generators/active_record/observer/observer_generator.rb","lib/generators/active_record/observer/templates/observer.rb","lib/generators/rails/observer/USAGE","lib/generators/rails/observer/observer_generator.rb","lib/generators/test_unit/observer/observer_generator.rb","lib/generators/test_unit/observer/templates/unit_test.rb","lib/rails-observers.rb","lib/rails/observers/action_controller/caching.rb","lib/rails/observers/action_controller/caching/sweeping.rb","lib/rails/observers/active_model.rb","lib/rails/observers/active_model/active_model.rb","lib/rails/observers/active_model/observer_array.rb","lib/rails/observers/active_model/observing.rb","lib/rails/observers/activerecord/active_record.rb","lib/rails/observers/activerecord/base.rb","lib/rails/observers/activerecord/observer.rb","lib/rails/observers/railtie.rb","lib/rails/observers/version.rb","rails-observers.gemspec","rails-observers.gemspec.erb","test/configuration_test.rb","test/console_test.rb","test/fixtures/developers.yml","test/fixtures/minimalistics.yml","test/fixtures/topics.yml","test/generators/generators_test_helper.rb","test/generators/namespaced_generators_test.rb","test/generators/observer_generator_test.rb","test/helper.rb","test/isolation/abstract_unit.rb","test/lifecycle_test.rb","test/models/observers.rb","test/observer_array_test.rb","test/observing_test.rb","test/rake_test.rb","test/sweeper_test.rb","test/transaction_callbacks_test.rb"]
|
14
14
|
s.test_files = ["test/configuration_test.rb","test/console_test.rb","test/fixtures/developers.yml","test/fixtures/minimalistics.yml","test/fixtures/topics.yml","test/generators/generators_test_helper.rb","test/generators/namespaced_generators_test.rb","test/generators/observer_generator_test.rb","test/helper.rb","test/isolation/abstract_unit.rb","test/lifecycle_test.rb","test/models/observers.rb","test/observer_array_test.rb","test/observing_test.rb","test/rake_test.rb","test/sweeper_test.rb","test/transaction_callbacks_test.rb"]
|
15
15
|
s.executables = []
|
16
16
|
s.require_paths = ["lib"]
|
17
17
|
|
18
|
-
s.add_dependency '
|
18
|
+
s.add_dependency 'activemodel', '~> 4.0'
|
19
19
|
|
20
20
|
s.add_development_dependency 'minitest', '>= 3'
|
21
|
-
s.add_development_dependency '
|
22
|
-
s.add_development_dependency '
|
23
|
-
s.add_development_dependency 'actionmailer', '~> 4.0
|
24
|
-
s.add_development_dependency 'actionpack', '~> 4.0
|
21
|
+
s.add_development_dependency 'railties', '~> 4.0'
|
22
|
+
s.add_development_dependency 'activerecord', '~> 4.0'
|
23
|
+
s.add_development_dependency 'actionmailer', '~> 4.0'
|
24
|
+
s.add_development_dependency 'actionpack', '~> 4.0'
|
25
25
|
s.add_development_dependency 'sqlite3', '~> 1.3'
|
26
26
|
end
|
data/rails-observers.gemspec.erb
CHANGED
@@ -15,12 +15,12 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.executables = [<%= executables.map(&:inspect).join ',' %>]
|
16
16
|
s.require_paths = ["lib"]
|
17
17
|
|
18
|
-
s.add_dependency '
|
18
|
+
s.add_dependency 'activemodel', '~> 4.0'
|
19
19
|
|
20
20
|
s.add_development_dependency 'minitest', '>= 3'
|
21
|
-
s.add_development_dependency '
|
22
|
-
s.add_development_dependency '
|
23
|
-
s.add_development_dependency 'actionmailer', '~> 4.0
|
24
|
-
s.add_development_dependency 'actionpack', '~> 4.0
|
21
|
+
s.add_development_dependency 'railties', '~> 4.0'
|
22
|
+
s.add_development_dependency 'activerecord', '~> 4.0'
|
23
|
+
s.add_development_dependency 'actionmailer', '~> 4.0'
|
24
|
+
s.add_development_dependency 'actionpack', '~> 4.0'
|
25
25
|
s.add_development_dependency 'sqlite3', '~> 1.3'
|
26
26
|
end
|
@@ -154,9 +154,9 @@ class TransactionCallbacksTest < ActiveRecord::TestCase
|
|
154
154
|
end
|
155
155
|
|
156
156
|
def test_call_after_rollback_when_commit_fails
|
157
|
-
|
157
|
+
TopicWithCallbacks.connection.class.send(:alias_method, :real_method_commit_db_transaction, :commit_db_transaction)
|
158
158
|
begin
|
159
|
-
|
159
|
+
TopicWithCallbacks.connection.class.class_eval do
|
160
160
|
def commit_db_transaction; raise "boom!"; end
|
161
161
|
end
|
162
162
|
|
@@ -166,8 +166,8 @@ class TransactionCallbacksTest < ActiveRecord::TestCase
|
|
166
166
|
assert !@first.save rescue nil
|
167
167
|
assert_equal [:after_rollback], @first.history
|
168
168
|
ensure
|
169
|
-
|
170
|
-
|
169
|
+
TopicWithCallbacks.connection.class.send(:remove_method, :commit_db_transaction)
|
170
|
+
TopicWithCallbacks.connection.class.send(:alias_method, :commit_db_transaction, :real_method_commit_db_transaction)
|
171
171
|
end
|
172
172
|
end
|
173
173
|
|
@@ -244,35 +244,3 @@ class TransactionCallbacksTest < ActiveRecord::TestCase
|
|
244
244
|
assert_equal [:after_rollback], @second.history
|
245
245
|
end
|
246
246
|
end
|
247
|
-
|
248
|
-
|
249
|
-
class SaveFromAfterCommitBlockTest < ActiveRecord::TestCase
|
250
|
-
self.use_transactional_fixtures = false
|
251
|
-
|
252
|
-
class TopicWithSaveInCallback < ActiveRecord::Base
|
253
|
-
self.table_name = :topics
|
254
|
-
after_commit :cache_topic, :on => :create
|
255
|
-
after_commit :call_update, :on => :update
|
256
|
-
attr_accessor :cached, :record_updated
|
257
|
-
|
258
|
-
def call_update
|
259
|
-
self.record_updated = true
|
260
|
-
end
|
261
|
-
|
262
|
-
def cache_topic
|
263
|
-
unless cached
|
264
|
-
self.cached = true
|
265
|
-
self.save
|
266
|
-
else
|
267
|
-
self.cached = false
|
268
|
-
end
|
269
|
-
end
|
270
|
-
end
|
271
|
-
|
272
|
-
def test_after_commit_in_save
|
273
|
-
topic = TopicWithSaveInCallback.new()
|
274
|
-
topic.save
|
275
|
-
assert_equal true, topic.cached
|
276
|
-
assert_equal true, topic.record_updated
|
277
|
-
end
|
278
|
-
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-observers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Rafael Mendonça França
|
@@ -10,108 +9,95 @@ authors:
|
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date:
|
12
|
+
date: 2013-07-19 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
15
|
+
name: activemodel
|
17
16
|
requirement: !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
17
|
requirements:
|
20
18
|
- - ~>
|
21
19
|
- !ruby/object:Gem::Version
|
22
|
-
version: 4.0
|
20
|
+
version: '4.0'
|
23
21
|
type: :runtime
|
24
22
|
prerelease: false
|
25
23
|
version_requirements: !ruby/object:Gem::Requirement
|
26
|
-
none: false
|
27
24
|
requirements:
|
28
25
|
- - ~>
|
29
26
|
- !ruby/object:Gem::Version
|
30
|
-
version: 4.0
|
27
|
+
version: '4.0'
|
31
28
|
- !ruby/object:Gem::Dependency
|
32
29
|
name: minitest
|
33
30
|
requirement: !ruby/object:Gem::Requirement
|
34
|
-
none: false
|
35
31
|
requirements:
|
36
|
-
- -
|
32
|
+
- - '>='
|
37
33
|
- !ruby/object:Gem::Version
|
38
34
|
version: '3'
|
39
35
|
type: :development
|
40
36
|
prerelease: false
|
41
37
|
version_requirements: !ruby/object:Gem::Requirement
|
42
|
-
none: false
|
43
38
|
requirements:
|
44
|
-
- -
|
39
|
+
- - '>='
|
45
40
|
- !ruby/object:Gem::Version
|
46
41
|
version: '3'
|
47
42
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
43
|
+
name: railties
|
49
44
|
requirement: !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
45
|
requirements:
|
52
46
|
- - ~>
|
53
47
|
- !ruby/object:Gem::Version
|
54
|
-
version: 4.0
|
48
|
+
version: '4.0'
|
55
49
|
type: :development
|
56
50
|
prerelease: false
|
57
51
|
version_requirements: !ruby/object:Gem::Requirement
|
58
|
-
none: false
|
59
52
|
requirements:
|
60
53
|
- - ~>
|
61
54
|
- !ruby/object:Gem::Version
|
62
|
-
version: 4.0
|
55
|
+
version: '4.0'
|
63
56
|
- !ruby/object:Gem::Dependency
|
64
|
-
name:
|
57
|
+
name: activerecord
|
65
58
|
requirement: !ruby/object:Gem::Requirement
|
66
|
-
none: false
|
67
59
|
requirements:
|
68
60
|
- - ~>
|
69
61
|
- !ruby/object:Gem::Version
|
70
|
-
version: 4.0
|
62
|
+
version: '4.0'
|
71
63
|
type: :development
|
72
64
|
prerelease: false
|
73
65
|
version_requirements: !ruby/object:Gem::Requirement
|
74
|
-
none: false
|
75
66
|
requirements:
|
76
67
|
- - ~>
|
77
68
|
- !ruby/object:Gem::Version
|
78
|
-
version: 4.0
|
69
|
+
version: '4.0'
|
79
70
|
- !ruby/object:Gem::Dependency
|
80
71
|
name: actionmailer
|
81
72
|
requirement: !ruby/object:Gem::Requirement
|
82
|
-
none: false
|
83
73
|
requirements:
|
84
74
|
- - ~>
|
85
75
|
- !ruby/object:Gem::Version
|
86
|
-
version: 4.0
|
76
|
+
version: '4.0'
|
87
77
|
type: :development
|
88
78
|
prerelease: false
|
89
79
|
version_requirements: !ruby/object:Gem::Requirement
|
90
|
-
none: false
|
91
80
|
requirements:
|
92
81
|
- - ~>
|
93
82
|
- !ruby/object:Gem::Version
|
94
|
-
version: 4.0
|
83
|
+
version: '4.0'
|
95
84
|
- !ruby/object:Gem::Dependency
|
96
85
|
name: actionpack
|
97
86
|
requirement: !ruby/object:Gem::Requirement
|
98
|
-
none: false
|
99
87
|
requirements:
|
100
88
|
- - ~>
|
101
89
|
- !ruby/object:Gem::Version
|
102
|
-
version: 4.0
|
90
|
+
version: '4.0'
|
103
91
|
type: :development
|
104
92
|
prerelease: false
|
105
93
|
version_requirements: !ruby/object:Gem::Requirement
|
106
|
-
none: false
|
107
94
|
requirements:
|
108
95
|
- - ~>
|
109
96
|
- !ruby/object:Gem::Version
|
110
|
-
version: 4.0
|
97
|
+
version: '4.0'
|
111
98
|
- !ruby/object:Gem::Dependency
|
112
99
|
name: sqlite3
|
113
100
|
requirement: !ruby/object:Gem::Requirement
|
114
|
-
none: false
|
115
101
|
requirements:
|
116
102
|
- - ~>
|
117
103
|
- !ruby/object:Gem::Version
|
@@ -119,7 +105,6 @@ dependencies:
|
|
119
105
|
type: :development
|
120
106
|
prerelease: false
|
121
107
|
version_requirements: !ruby/object:Gem::Requirement
|
122
|
-
none: false
|
123
108
|
requirements:
|
124
109
|
- - ~>
|
125
110
|
- !ruby/object:Gem::Version
|
@@ -147,12 +132,14 @@ files:
|
|
147
132
|
- lib/rails-observers.rb
|
148
133
|
- lib/rails/observers/action_controller/caching.rb
|
149
134
|
- lib/rails/observers/action_controller/caching/sweeping.rb
|
135
|
+
- lib/rails/observers/active_model.rb
|
150
136
|
- lib/rails/observers/active_model/active_model.rb
|
151
137
|
- lib/rails/observers/active_model/observer_array.rb
|
152
138
|
- lib/rails/observers/active_model/observing.rb
|
153
139
|
- lib/rails/observers/activerecord/active_record.rb
|
154
140
|
- lib/rails/observers/activerecord/base.rb
|
155
141
|
- lib/rails/observers/activerecord/observer.rb
|
142
|
+
- lib/rails/observers/railtie.rb
|
156
143
|
- lib/rails/observers/version.rb
|
157
144
|
- rails-observers.gemspec
|
158
145
|
- rails-observers.gemspec.erb
|
@@ -175,27 +162,26 @@ files:
|
|
175
162
|
- test/transaction_callbacks_test.rb
|
176
163
|
homepage: https://github.com/rails/rails-observers
|
177
164
|
licenses: []
|
165
|
+
metadata: {}
|
178
166
|
post_install_message:
|
179
167
|
rdoc_options: []
|
180
168
|
require_paths:
|
181
169
|
- lib
|
182
170
|
required_ruby_version: !ruby/object:Gem::Requirement
|
183
|
-
none: false
|
184
171
|
requirements:
|
185
|
-
- -
|
172
|
+
- - '>='
|
186
173
|
- !ruby/object:Gem::Version
|
187
174
|
version: '0'
|
188
175
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
189
|
-
none: false
|
190
176
|
requirements:
|
191
|
-
- -
|
177
|
+
- - '>='
|
192
178
|
- !ruby/object:Gem::Version
|
193
179
|
version: '0'
|
194
180
|
requirements: []
|
195
181
|
rubyforge_project:
|
196
|
-
rubygems_version:
|
182
|
+
rubygems_version: 2.0.2
|
197
183
|
signing_key:
|
198
|
-
specification_version:
|
184
|
+
specification_version: 4
|
199
185
|
summary: ActiveModel::Observer, ActiveRecord::Observer and ActionController::Caching::Sweeper
|
200
186
|
extracted from Rails.
|
201
187
|
test_files:
|