@@ -46,8 +46,8 @@ def self.digest_caller_file(caller_line)
46
46
47
47
with_options instance_writer : false , instance_reader : false do |serializer |
48
48
class_attribute :_type , instance_reader : true
49
- class_attribute :_attributes # @api private : names of attribute methods, @see Serializer#attribute
50
- self . _attributes ||= [ ]
49
+ class_attribute :serialized_attributes , instance_writer : false # @api public: maps attribute name to 'Attribute' function
50
+ self . serialized_attributes ||= { }
51
51
class_attribute :_attributes_keys # @api private : maps attribute value to explict key name, @see Serializer#attribute
52
52
self . _attributes_keys ||= { }
53
53
class_attribute :_links # @api private : links definitions, @see Serializer#link
@@ -69,11 +69,11 @@ def self.digest_caller_file(caller_line)
69
69
serializer . class_attribute :_cache_digest # @api private : Generated
70
70
end
71
71
72
- # Serializers inherit _attributes and _attributes_keys.
72
+ # Serializers inherit serialized_attributes and _attributes_keys.
73
73
# Generates a unique digest for each serializer at load.
74
74
def self . inherited ( base )
75
75
caller_line = caller . first
76
- base . _attributes = _attributes . dup
76
+ base . serialized_attributes = serialized_attributes . dup
77
77
base . _attributes_keys = _attributes_keys . dup
78
78
base . _links = _links . dup
79
79
base . _cache_digest = digest_caller_file ( caller_line )
@@ -91,6 +91,10 @@ def self.link(name, value = nil, &block)
91
91
_links [ name ] = block || value
92
92
end
93
93
94
+ def self . _attributes
95
+ serialized_attributes . keys
96
+ end
97
+
94
98
# @example
95
99
# class AdminAuthorSerializer < ActiveModel::Serializer
96
100
# attributes :id, :name, :recent_edits
@@ -102,22 +106,25 @@ def self.attributes(*attrs)
102
106
end
103
107
end
104
108
109
+ # TODO: remove the dynamic method definition
105
110
# @example
106
111
# class AdminAuthorSerializer < ActiveModel::Serializer
107
112
# attributes :id, :recent_edits
108
113
# attribute :name, key: :title
109
114
#
110
115
# def recent_edits
111
116
# object.edits.last(5)
112
- # enr
117
+ # end
113
118
def self . attribute ( attr , options = { } )
114
119
key = options . fetch ( :key , attr )
115
120
_attributes_keys [ attr ] = { key : key } if key != attr
116
121
_attributes << key unless _attributes . include? ( key )
117
122
123
+ serialized_attributes [ key ] = -> ( object ) { object . read_attribute_for_serialization ( attr ) }
124
+
118
125
ActiveModelSerializers . silence_warnings do
119
126
define_method key do
120
- object . read_attribute_for_serialization ( attr )
127
+ serialized_attributes [ key ] . call ( object )
121
128
end unless method_defined? ( key ) || _fragmented . respond_to? ( attr )
122
129
end
123
130
end
@@ -249,14 +256,14 @@ def json_key
249
256
def attributes ( requested_attrs = nil )
250
257
self . class . _attributes . each_with_object ( { } ) do |name , hash |
251
258
next unless requested_attrs . nil? || requested_attrs . include? ( name )
252
- if self . class . _fragmented
253
- hash [ name ] = self . class . _fragmented . public_send ( name )
254
- else
255
- hash [ name ] = send ( name )
256
- end
259
+ hash [ name ] = read_attribute_for_serialization ( name )
257
260
end
258
261
end
259
262
263
+ def read_attribute_for_serialization ( key )
264
+ self . class . _fragmented ? self . class . _fragmented . public_send ( key ) : send ( key )
265
+ end
266
+
260
267
# @api private
261
268
# Used by JsonApi adapter to build resource links.
262
269
def links
0 commit comments