File tree Expand file tree Collapse file tree 7 files changed +62
-16
lines changed Expand file tree Collapse file tree 7 files changed +62
-16
lines changed Original file line number Diff line number Diff line change 1
1
require "rails/observers/active_model"
2
+
2
3
require "mongoid"
3
4
require "mongoid/observers/config"
4
- require "mongoid/observers/composable "
5
+ require "mongoid/observers/interceptable "
5
6
require "mongoid/observers/railtie" if defined? Rails
6
7
require "mongoid/observer"
7
8
Original file line number Diff line number Diff line change @@ -118,8 +118,6 @@ class Observer < ActiveModel::Observer
118
118
# observer.add_observer!(Document)
119
119
#
120
120
# @param [ Class ] klass The child observer to add.
121
- #
122
- # @since 2.0.0.rc.8
123
121
def add_observer! ( klass )
124
122
super and define_callbacks ( klass )
125
123
end
@@ -130,12 +128,10 @@ def add_observer!(klass)
130
128
# observer.define_callbacks(Document)
131
129
#
132
130
# @param [ Class ] klass The model to define them on.
133
- #
134
- # @since 2.0.0.rc.8
135
131
def define_callbacks ( klass )
136
132
observer = self
137
133
observer_name = observer . class . name . underscore . gsub ( '/' , '__' )
138
- Mongoid ::Interceptable :: CALLBACKS . each do |callback |
134
+ Mongoid ::Interceptable . observables . each do |callback |
139
135
next unless respond_to? ( callback )
140
136
callback_meth = :"_notify_#{ observer_name } _for_#{ callback } "
141
137
unless klass . respond_to? ( callback_meth )
@@ -178,8 +174,6 @@ class << self
178
174
# end
179
175
#
180
176
# @param [ Array<Symbol> ] models The names of the models.
181
- #
182
- # @since 3.0.15
183
177
def observe ( *models )
184
178
models . flatten!
185
179
models . collect! do |model |
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ module Mongoid
2
+ module Interceptable
3
+ include ActiveModel ::Observing
4
+
5
+ class << self
6
+
7
+ # Get all callbacks that can be observed.
8
+ #
9
+ # @example Get the observables.
10
+ # Interceptable.observables
11
+ #
12
+ # @return [ Array<Symbol> ] The names of the observables.
13
+ def observables
14
+ CALLBACKS + registered_observables
15
+ end
16
+
17
+ # Get all registered callbacks that can be observed, not included in
18
+ # Mongoid's defaults.
19
+ #
20
+ # @example Get the observables.
21
+ # Interceptable.registered_observables
22
+ #
23
+ # @return [ Array<Symbol> ] The names of the registered observables.
24
+ def registered_observables
25
+ @registered_observables ||= [ ]
26
+ end
27
+ end
28
+
29
+ module ClassMethods
30
+
31
+ # Set a custom callback as able to be observed.
32
+ #
33
+ # @example Set a custom callback as observable.
34
+ # class Band
35
+ # include Mongoid::Document
36
+ #
37
+ # define_model_callbacks :notification
38
+ # observable :notification
39
+ # end
40
+ #
41
+ # @param [ Array<Symbol> ] args The names of the observable callbacks.
42
+ #
43
+ # @since 3.0.1
44
+ def observable ( *args )
45
+ observables = args . flat_map do |name |
46
+ [ :"before_#{ name } " , :"after_#{ name } " , :"around_#{ name } " ]
47
+ end
48
+ Interceptable . registered_observables . concat ( observables ) . uniq
49
+ end
50
+ end
51
+ end
52
+ end
Original file line number Diff line number Diff line change 1
1
class Actor
2
2
include Mongoid ::Document
3
3
field :name
4
+ field :after_custom_count , type : Integer , default : 0
5
+
6
+ define_model_callbacks :custom
7
+ observable :custom
4
8
5
9
def do_something
6
10
run_callbacks ( :custom ) do
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ def reset
14
14
@last_record = { }
15
15
end
16
16
17
- Mongoid ::Interceptable :: CALLBACKS . each do |callback |
17
+ Mongoid ::Interceptable . observables . each do |callback |
18
18
define_method ( callback ) do |record , &block |
19
19
@last_callback = callback
20
20
@call_count [ callback ] += 1
Original file line number Diff line number Diff line change @@ -201,7 +201,7 @@ class BandObserver < Mongoid::Observer
201
201
end
202
202
end
203
203
204
- context "when custom callbacks are being fired" , :pending do
204
+ context "when custom callbacks are being fired" do
205
205
206
206
let! ( :actor ) do
207
207
Actor . create!
@@ -273,7 +273,7 @@ class BandObserver < Mongoid::Observer
273
273
end
274
274
end
275
275
276
- context "when using a custom callback" , :pending do
276
+ context "when using a custom callback" do
277
277
278
278
let ( :actor ) do
279
279
Actor . new
You can’t perform that action at this time.
0 commit comments