Skip to content

Commit b36cc42

Browse files
committed
Separate out callbacks per ActiveJob pattern
1 parent e8efc4e commit b36cc42

File tree

3 files changed

+86
-30
lines changed

3 files changed

+86
-30
lines changed

lib/active_model_serializers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module ActiveModelSerializers
99

1010
extend ActiveSupport::Autoload
1111
autoload :Model
12+
autoload :Callbacks
1213
autoload :Logging
1314

1415
module_function
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Adapted from
2+
# https://github.com/rails/rails/blob/7f18ea14c8/activejob/lib/active_job/callbacks.rb
3+
require 'active_support/callbacks'
4+
5+
module ActiveModelSerializers
6+
# = ActiveModelSerializers Callbacks
7+
#
8+
# ActiveModelSerializers provides hooks during the life cycle of serialization and
9+
# allow you to trigger logic. Available callbacks are:
10+
#
11+
# * <tt>around_render</tt>
12+
#
13+
module Callbacks
14+
extend ActiveSupport::Concern
15+
include ActiveSupport::Callbacks
16+
17+
included do
18+
define_callbacks :render
19+
end
20+
21+
# These methods will be included into any ActiveModelSerializers object, adding
22+
# callbacks for +render+.
23+
module ClassMethods
24+
# Defines a callback that will get called around the render method,
25+
# whether it is as_json, to_json, or serializable_hash
26+
#
27+
# class ActiveModel::SerializableResource
28+
# include ActiveModelSerializers::Callbacks
29+
#
30+
# around_render do |args, block|
31+
# tag_logger do
32+
# notify_render do
33+
# block.call(args)
34+
# end
35+
# end
36+
# end
37+
#
38+
# def as_json
39+
# run_callbacks :render do
40+
# adapter.as_json
41+
# end
42+
# end
43+
# # Note: So that we can re-use the instrumenter for as_json, to_json, and
44+
# # serializable_hash, we aren't using the usual format, which would be:
45+
# # def render(args)
46+
# # adapter.as_json
47+
# # end
48+
# end
49+
#
50+
def around_render(*filters, &blk)
51+
set_callback(:render, :around, *filters, &blk)
52+
end
53+
end
54+
end
55+
end

lib/active_model_serializers/logging.rb

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
11
##
2-
# Adapted from:
3-
# https://github.com/rubygems/rubygems/blob/cb28f5e991/lib/rubygems/deprecate.rb
4-
# https://github.com/rails/rails/blob/280654ef88/activejob/lib/active_job/logging.rb
5-
#
6-
# Provides a single method +notify+ to be used to declare when
7-
# something a method notifies.
8-
#
9-
# class Adapter
10-
# def self.klass_method
11-
# # ...
12-
# end
13-
#
14-
# def instance_method
15-
# # ...
16-
# end
2+
# ActiveModelSerializers::Logging
173
#
18-
# extend ActiveModelSerializers::Logging
19-
# notify :instance_method, :render
4+
# https://github.com/rails/rails/blob/280654ef88/activejob/lib/active_job/logging.rb
205
#
21-
# class << self
22-
# extend ActiveModelSerializers::Logging
23-
# notify :klass_method, :render
24-
# end
25-
# end
266
module ActiveModelSerializers::Logging
277
extend ActiveSupport::Concern
288

299
included do
30-
include ActiveSupport::Callbacks
31-
instrument_around_render
10+
include ActiveModelSerializers::Callbacks
11+
extend NotificationMacro
12+
instrument_rendering
3213
end
3314

3415
module ClassMethods
35-
def instrument_around_render
36-
define_callbacks :render
16+
def instrument_rendering
3717
around_render do |args, block|
3818
tag_logger do
3919
notify_render do
@@ -42,11 +22,31 @@ def instrument_around_render
4222
end
4323
end
4424
end
25+
end
4526

46-
def around_render(*filters, &blk)
47-
set_callback(:render, :around, *filters, &blk)
48-
end
49-
27+
# Adapted from:
28+
# https://github.com/rubygems/rubygems/blob/cb28f5e991/lib/rubygems/deprecate.rb
29+
# Provides a single method +notify+ to be used to declare when
30+
# something a method notifies, with the argument +callback_name+ of the notification callback.
31+
#
32+
# class Adapter
33+
# def self.klass_method
34+
# # ...
35+
# end
36+
#
37+
# def instance_method
38+
# # ...
39+
# end
40+
#
41+
# include ActiveModelSerializers::Logging
42+
# notify :instance_method, :render
43+
#
44+
# class << self
45+
# extend ActiveModelSerializers::Logging::NotificationMacro
46+
# notify :klass_method, :render
47+
# end
48+
# end
49+
module NotificationMacro
5050
##
5151
# Simple notify method that wraps up +name+
5252
# in a dummy method. It notifies on with the +callback_name+ notifier on

0 commit comments

Comments
 (0)