From b36cc42f0347ab15a4c1d99ddbb2e51d9ffbbdbe Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 8 Nov 2015 21:01:02 -0600 Subject: [PATCH] Separate out callbacks per ActiveJob pattern --- lib/active_model_serializers.rb | 1 + lib/active_model_serializers/callbacks.rb | 55 +++++++++++++++++++++ lib/active_model_serializers/logging.rb | 60 +++++++++++------------ 3 files changed, 86 insertions(+), 30 deletions(-) create mode 100644 lib/active_model_serializers/callbacks.rb diff --git a/lib/active_model_serializers.rb b/lib/active_model_serializers.rb index d2eb0fab..ce09b08a 100644 --- a/lib/active_model_serializers.rb +++ b/lib/active_model_serializers.rb @@ -9,6 +9,7 @@ module ActiveModelSerializers extend ActiveSupport::Autoload autoload :Model + autoload :Callbacks autoload :Logging module_function diff --git a/lib/active_model_serializers/callbacks.rb b/lib/active_model_serializers/callbacks.rb new file mode 100644 index 00000000..7f1cd689 --- /dev/null +++ b/lib/active_model_serializers/callbacks.rb @@ -0,0 +1,55 @@ +# Adapted from +# https://github.com/rails/rails/blob/7f18ea14c8/activejob/lib/active_job/callbacks.rb +require 'active_support/callbacks' + +module ActiveModelSerializers + # = ActiveModelSerializers Callbacks + # + # ActiveModelSerializers provides hooks during the life cycle of serialization and + # allow you to trigger logic. Available callbacks are: + # + # * around_render + # + module Callbacks + extend ActiveSupport::Concern + include ActiveSupport::Callbacks + + included do + define_callbacks :render + end + + # These methods will be included into any ActiveModelSerializers object, adding + # callbacks for +render+. + module ClassMethods + # Defines a callback that will get called around the render method, + # whether it is as_json, to_json, or serializable_hash + # + # class ActiveModel::SerializableResource + # include ActiveModelSerializers::Callbacks + # + # around_render do |args, block| + # tag_logger do + # notify_render do + # block.call(args) + # end + # end + # end + # + # def as_json + # run_callbacks :render do + # adapter.as_json + # end + # end + # # Note: So that we can re-use the instrumenter for as_json, to_json, and + # # serializable_hash, we aren't using the usual format, which would be: + # # def render(args) + # # adapter.as_json + # # end + # end + # + def around_render(*filters, &blk) + set_callback(:render, :around, *filters, &blk) + end + end + end +end diff --git a/lib/active_model_serializers/logging.rb b/lib/active_model_serializers/logging.rb index 2658da84..5b30a1a1 100644 --- a/lib/active_model_serializers/logging.rb +++ b/lib/active_model_serializers/logging.rb @@ -1,39 +1,19 @@ ## -# Adapted from: -# https://github.com/rubygems/rubygems/blob/cb28f5e991/lib/rubygems/deprecate.rb +# ActiveModelSerializers::Logging +# # https://github.com/rails/rails/blob/280654ef88/activejob/lib/active_job/logging.rb # -# Provides a single method +notify+ to be used to declare when -# something a method notifies. -# -# class Adapter -# def self.klass_method -# # ... -# end -# -# def instance_method -# # ... -# end -# -# extend ActiveModelSerializers::Logging -# notify :instance_method, :render -# -# class << self -# extend ActiveModelSerializers::Logging -# notify :klass_method, :render -# end -# end module ActiveModelSerializers::Logging extend ActiveSupport::Concern included do - include ActiveSupport::Callbacks - instrument_around_render + include ActiveModelSerializers::Callbacks + extend NotificationMacro + instrument_rendering end module ClassMethods - def instrument_around_render - define_callbacks :render + def instrument_rendering around_render do |args, block| tag_logger do notify_render do @@ -42,11 +22,31 @@ module ActiveModelSerializers::Logging end end end + end - def around_render(*filters, &blk) - set_callback(:render, :around, *filters, &blk) - end - + # Adapted from: + # https://github.com/rubygems/rubygems/blob/cb28f5e991/lib/rubygems/deprecate.rb + # Provides a single method +notify+ to be used to declare when + # something a method notifies, with the argument +callback_name+ of the notification callback. + # + # class Adapter + # def self.klass_method + # # ... + # end + # + # def instance_method + # # ... + # end + # + # include ActiveModelSerializers::Logging + # notify :instance_method, :render + # + # class << self + # extend ActiveModelSerializers::Logging::NotificationMacro + # notify :klass_method, :render + # end + # end + module NotificationMacro ## # Simple notify method that wraps up +name+ # in a dummy method. It notifies on with the +callback_name+ notifier on