-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
serialization.rb
75 lines (63 loc) · 2.11 KB
/
serialization.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require 'active_support/core_ext/class/attribute'
module ActionController
# Action Controller Serialization
#
# Overrides render :json to check if the given object implements +active_model_serializer+
# as a method. If so, use the returned serializer instead of calling +to_json+ on the object.
#
# This module also provides a serialization_scope method that allows you to configure the
# +serialization_scope+ of the serializer. Most apps will likely set the +serialization_scope+
# to the current user:
#
# class ApplicationController < ActionController::Base
# serialization_scope :current_user
# end
#
# If you need more complex scope rules, you can simply override the serialization_scope:
#
# class ApplicationController < ActionController::Base
# private
#
# def serialization_scope
# current_user
# end
# end
#
module Serialization
extend ActiveSupport::Concern
include ActionController::Renderers
included do
class_attribute :_serialization_scope
self._serialization_scope = :current_user
end
module ClassMethods
def serialization_scope(scope)
self._serialization_scope = scope
end
end
def _render_option_json(resource, options)
serializer = build_json_serializer(resource, options)
if serializer
super(serializer, options)
else
super
end
end
private
def default_serializer_options
{}
end
def serialization_scope
_serialization_scope = self.class._serialization_scope
send(_serialization_scope) if _serialization_scope && respond_to?(_serialization_scope, true)
end
def build_json_serializer(resource, options)
options = default_serializer_options.merge(options || {})
if serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource))
options[:scope] = serialization_scope unless options.has_key?(:scope)
options[:resource_name] = self.controller_name if resource.respond_to?(:to_ary)
serializer.new(resource, options)
end
end
end
end