Skip to content

Commit 360ecc8

Browse files
committed
Clean up notification code with some meta-prog
1 parent 21bb306 commit 360ecc8

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

lib/action_controller/serialization.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def get_serializer(resource, options = {})
3131
serializable_resource.serialization_scope ||= serialization_scope
3232
serializable_resource.serialization_scope_name = _serialization_scope
3333
begin
34-
serializable_resource.adapter
35-
serializable_resource
34+
serializable_resource.tap(&:adapter)
3635
rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError
3736
resource
3837
end

lib/active_model/serializable_resource.rb

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ class SerializableResource
44
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links])
55
include ActiveModelSerializers::Logging
66

7+
delegate :serializable_hash, :as_json, :to_json, to: :adapter
8+
notify :serializable_hash, :render
9+
notify :as_json, :render
10+
notify :to_json, :render
11+
712
# Primary interface to composing a resource with a serializer and adapter.
813
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
914
def initialize(resource, options = {})
@@ -12,24 +17,6 @@ def initialize(resource, options = {})
1217
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
1318
end
1419

15-
def serializable_hash(*args)
16-
run_callbacks :render do
17-
adapter.serializable_hash(*args)
18-
end
19-
end
20-
21-
def as_json(*args)
22-
run_callbacks :render do
23-
adapter.as_json(*args)
24-
end
25-
end
26-
27-
def to_json(*args)
28-
run_callbacks :render do
29-
adapter.to_json(*args)
30-
end
31-
end
32-
3320
def serialization_scope=(scope)
3421
serializer_opts[:scope] = scope
3522
end

lib/active_model/serializer/railtie.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require 'active_model_serializers'
21
require 'rails/railtie'
32

43
module ActiveModel

lib/active_model_serializers/logging.rb

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,39 @@
22
# Adapted from:
33
# https://github.com/rails/rails/blob/280654ef88/activejob/lib/active_job/logging.rb
44
module ActiveModelSerializers::Logging
5-
extend ActiveSupport::Concern
5+
def self.included(base)
6+
base.send(:include, ActiveSupport::Callbacks)
7+
base.extend ClassMethods
8+
base.class_eval do
9+
define_callbacks :render
10+
around_render do |_, block|
11+
notify_active_support do
12+
block.call
13+
end
14+
end
15+
end
16+
end
617

7-
included do
8-
extend ActiveModel::Callbacks
9-
define_model_callbacks :render
10-
around_render do |_, block, _|
11-
notify_active_support do
12-
block.call
18+
module ClassMethods
19+
def around_render(*filters, &blk)
20+
set_callback(:render, :around, *filters, &blk)
21+
end
22+
##
23+
# Simple notify method that wraps up +name+
24+
# in a dummy method. It notifies on each call to the dummy method
25+
# telling what the current serializer and adapter are being rendered.
26+
# Adapted from:
27+
# https://github.com/rubygems/rubygems/blob/cb28f5e991/lib/rubygems/deprecate.rb
28+
29+
def notify(name, callback_name)
30+
class_eval do
31+
old = "_notifying_#{callback_name}_#{name}"
32+
alias_method old, name
33+
define_method name do |*args, &block|
34+
run_callbacks callback_name do
35+
send old, *args, &block
36+
end
37+
end
1338
end
1439
end
1540
end

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def Minitest.after_run(&block)
5050
end
5151

5252
require 'active_model_serializers'
53+
require 'active_model/serializer/railtie'
5354

5455
require 'support/stream_capture'
5556

0 commit comments

Comments
 (0)