3
3
module ActiveModel
4
4
class Serializer
5
5
extend ActiveSupport ::Autoload
6
+
6
7
autoload :Configuration
7
8
autoload :ArraySerializer
8
9
autoload :Adapter
9
10
autoload :Lint
11
+ autoload :Associations
10
12
include Configuration
13
+ include Associations
11
14
12
15
class << self
13
16
attr_accessor :_attributes
14
17
attr_accessor :_attributes_keys
15
- attr_accessor :_associations
16
18
attr_accessor :_urls
17
19
attr_accessor :_cache
18
20
attr_accessor :_fragmented
@@ -24,12 +26,12 @@ class << self
24
26
end
25
27
26
28
def self . inherited ( base )
27
- base . _attributes = self . _attributes . try ( :dup ) || [ ]
29
+ base . _attributes = self . _attributes . try ( :dup ) || [ ]
28
30
base . _attributes_keys = self . _attributes_keys . try ( :dup ) || { }
29
- base . _associations = self . _associations . try ( :dup ) || { }
30
31
base . _urls = [ ]
31
32
serializer_file = File . open ( caller . first [ /^[^:]+/ ] )
32
33
base . _cache_digest = Digest ::MD5 . hexdigest ( serializer_file . read )
34
+ super
33
35
end
34
36
35
37
def self . attributes ( *attrs )
@@ -46,7 +48,7 @@ def self.attributes(*attrs)
46
48
47
49
def self . attribute ( attr , options = { } )
48
50
key = options . fetch ( :key , attr )
49
- @_attributes_keys [ attr ] = { key : key } if key != attr
51
+ @_attributes_keys [ attr ] = { key : key } if key != attr
50
52
@_attributes << key unless @_attributes . include? ( key )
51
53
define_method key do
52
54
object . read_attribute_for_serialization ( attr )
@@ -59,58 +61,13 @@ def self.fragmented(serializer)
59
61
60
62
# Enables a serializer to be automatically cached
61
63
def self . cache ( options = { } )
62
- @_cache = ActionController ::Base . cache_store if Rails . configuration . action_controller . perform_caching
63
- @_cache_key = options . delete ( :key )
64
- @_cache_only = options . delete ( :only )
65
- @_cache_except = options . delete ( :except )
64
+ @_cache = ActionController ::Base . cache_store if Rails . configuration . action_controller . perform_caching
65
+ @_cache_key = options . delete ( :key )
66
+ @_cache_only = options . delete ( :only )
67
+ @_cache_except = options . delete ( :except )
66
68
@_cache_options = ( options . empty? ) ? nil : options
67
69
end
68
70
69
- # Defines an association in the object should be rendered.
70
- #
71
- # The serializer object should implement the association name
72
- # as a method which should return an array when invoked. If a method
73
- # with the association name does not exist, the association name is
74
- # dispatched to the serialized object.
75
- def self . has_many ( *attrs )
76
- associate ( :has_many , attrs )
77
- end
78
-
79
- # Defines an association in the object that should be rendered.
80
- #
81
- # The serializer object should implement the association name
82
- # as a method which should return an object when invoked. If a method
83
- # with the association name does not exist, the association name is
84
- # dispatched to the serialized object.
85
- def self . belongs_to ( *attrs )
86
- associate ( :belongs_to , attrs )
87
- end
88
-
89
- # Defines an association in the object should be rendered.
90
- #
91
- # The serializer object should implement the association name
92
- # as a method which should return an object when invoked. If a method
93
- # with the association name does not exist, the association name is
94
- # dispatched to the serialized object.
95
- def self . has_one ( *attrs )
96
- associate ( :has_one , attrs )
97
- end
98
-
99
- def self . associate ( type , attrs ) #:nodoc:
100
- options = attrs . extract_options!
101
- self . _associations = _associations . dup
102
-
103
- attrs . each do |attr |
104
- unless method_defined? ( attr )
105
- define_method attr do
106
- object . send attr
107
- end
108
- end
109
-
110
- self . _associations [ attr ] = { type : type , association_options : options }
111
- end
112
- end
113
-
114
71
def self . url ( attr )
115
72
@_urls . push attr
116
73
end
@@ -125,19 +82,17 @@ def self.serializer_for(resource, options = {})
125
82
elsif resource . respond_to? ( :to_ary )
126
83
config . array_serializer
127
84
else
128
- options
129
- . fetch ( :association_options , { } )
130
- . fetch ( :serializer , get_serializer_for ( resource . class ) )
85
+ options . fetch ( :serializer , get_serializer_for ( resource . class ) )
131
86
end
132
87
end
133
88
134
89
def self . adapter
135
90
adapter_class = case config . adapter
136
- when Symbol
137
- ActiveModel ::Serializer ::Adapter . adapter_class ( config . adapter )
138
- when Class
139
- config . adapter
140
- end
91
+ when Symbol
92
+ ActiveModel ::Serializer ::Adapter . adapter_class ( config . adapter )
93
+ when Class
94
+ config . adapter
95
+ end
141
96
unless adapter_class
142
97
valid_adapters = Adapter . constants . map { |klass | ":#{ klass . to_s . downcase } " }
143
98
raise ArgumentError , "Unknown adapter: #{ config . adapter } . Valid adapters are: #{ valid_adapters } "
@@ -153,12 +108,12 @@ def self.root_name
153
108
attr_accessor :object , :root , :meta , :meta_key , :scope
154
109
155
110
def initialize ( object , options = { } )
156
- @object = object
157
- @options = options
158
- @root = options [ :root ]
159
- @meta = options [ :meta ]
160
- @meta_key = options [ :meta_key ]
161
- @scope = options [ :scope ]
111
+ @object = object
112
+ @options = options
113
+ @root = options [ :root ]
114
+ @meta = options [ :meta ]
115
+ @meta_key = options [ :meta_key ]
116
+ @scope = options [ :scope ]
162
117
163
118
scope_name = options [ :scope_name ]
164
119
if scope_name && !respond_to? ( scope_name )
@@ -199,48 +154,10 @@ def attributes(options = {})
199
154
end
200
155
end
201
156
202
- def each_association ( &block )
203
- self . class . _associations . dup . each do |name , association_options |
204
- next unless object
205
- association_value = send ( name )
206
-
207
- serializer_class = ActiveModel ::Serializer . serializer_for ( association_value , association_options )
208
-
209
- if serializer_class
210
- begin
211
- serializer = serializer_class . new (
212
- association_value ,
213
- options . except ( :serializer ) . merge ( serializer_from_options ( association_options ) )
214
- )
215
- rescue ActiveModel ::Serializer ::ArraySerializer ::NoSerializerError
216
- virtual_value = association_value
217
- virtual_value = virtual_value . as_json if virtual_value . respond_to? ( :as_json )
218
- association_options [ :association_options ] [ :virtual_value ] = virtual_value
219
- end
220
- elsif !association_value . nil? && !association_value . instance_of? ( Object )
221
- association_options [ :association_options ] [ :virtual_value ] = association_value
222
- end
223
-
224
- association_key = association_options [ :association_options ] [ :key ] || name
225
- if block_given?
226
- block . call ( association_key , serializer , association_options [ :association_options ] )
227
- end
228
- end
229
- end
230
-
231
- def serializer_from_options ( options )
232
- opts = { }
233
- serializer = options . fetch ( :association_options , { } ) . fetch ( :serializer , nil )
234
- opts [ :serializer ] = serializer if serializer
235
- opts
236
- end
237
-
238
157
def self . serializers_cache
239
158
@serializers_cache ||= ThreadSafe ::Cache . new
240
159
end
241
160
242
- private
243
-
244
161
attr_reader :options
245
162
246
163
def self . get_serializer_for ( klass )
@@ -255,6 +172,5 @@ def self.get_serializer_for(klass)
255
172
end
256
173
end
257
174
end
258
-
259
175
end
260
176
end
0 commit comments