@@ -10,7 +10,7 @@ class Serializer
10
10
11
11
class << self
12
12
attr_accessor :_attributes
13
- attr_accessor :_deserialize
13
+ attr_accessor :_params
14
14
attr_accessor :_attributes_keys
15
15
attr_accessor :_associations
16
16
attr_accessor :_urls
@@ -27,15 +27,15 @@ def self.inherited(base)
27
27
base . _attributes = self . _attributes . try ( :dup ) || [ ]
28
28
base . _associations = self . _associations . try ( :dup ) || { }
29
29
base . _attributes_keys = self . _attributes_keys . try ( :dup ) || { }
30
- base . _deserialize = self . _attributes . try ( :dup ) || [ ]
30
+ base . _params = self . _attributes . try ( :dup ) || [ ]
31
31
base . _urls = [ ]
32
32
serializer_file = File . open ( caller . first [ /^[^:]+/ ] )
33
33
base . _cache_digest = Digest ::MD5 . hexdigest ( serializer_file . read )
34
34
end
35
35
36
- def self . deserialize ( *attrs )
37
- @_deserialize . concat attrs
38
- @_deserialize . uniq!
36
+ def self . params ( *attrs )
37
+ @_params . concat attrs
38
+ @_params . uniq!
39
39
end
40
40
41
41
def self . attributes ( *attrs )
@@ -156,6 +156,11 @@ def self.root_name
156
156
name . demodulize . underscore . sub ( /_serializer$/ , '' ) if name
157
157
end
158
158
159
+ def self . deserialize ( params )
160
+ object_class = root_name . split ( '_' ) . first . camelize . constantize
161
+ object_class . find_or_initialize_by ( params )
162
+ end
163
+
159
164
attr_accessor :object , :root , :meta , :meta_key , :scope
160
165
161
166
def initialize ( object , options = { } )
@@ -219,8 +224,7 @@ def each_association(&block)
219
224
options . except ( :serializer ) . merge ( serializer_from_options ( association_options ) )
220
225
)
221
226
rescue ActiveModel ::Serializer ::ArraySerializer ::NoSerializerError
222
- virtual_value = association_value
223
- virtual_value = virtual_value . as_json if virtual_value . respond_to? ( :as_json )
227
+ virtual_value = association_value . as_json if association_value . respond_to? ( :as_json )
224
228
association_options [ :association_options ] [ :virtual_value ] = virtual_value
225
229
end
226
230
elsif !association_value . nil? && !association_value . instance_of? ( Object )
0 commit comments