|
| 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 |
0 commit comments