11# frozen_string_literal: true
22
3- require 'active_support/core_ext/object '
3+ require 'active_support/json '
44require 'active_support/concern'
55require 'active_support/inflector'
66require 'fast_jsonapi/attribute'
@@ -65,7 +65,7 @@ def hash_for_collection
6565 end
6666
6767 def serialized_json
68- self . class . to_json ( serializable_hash )
68+ ActiveSupport :: JSON . encode ( serializable_hash )
6969 end
7070
7171 private
@@ -120,6 +120,7 @@ def inherited(subclass)
120120 subclass . data_links = data_links
121121 subclass . cached = cached
122122 subclass . set_type ( subclass . reflected_record_type ) if subclass . reflected_record_type
123+ subclass . meta_to_serialize = meta_to_serialize
123124 end
124125
125126 def reflected_record_type
@@ -194,7 +195,7 @@ def add_relationship(relationship)
194195 self . relationships_to_serialize = { } if relationships_to_serialize . nil?
195196 self . cachable_relationships_to_serialize = { } if cachable_relationships_to_serialize . nil?
196197 self . uncachable_relationships_to_serialize = { } if uncachable_relationships_to_serialize . nil?
197-
198+
198199 if !relationship . cached
199200 self . uncachable_relationships_to_serialize [ relationship . name ] = relationship
200201 else
@@ -218,6 +219,10 @@ def belongs_to(relationship_name, options = {}, &block)
218219 add_relationship ( relationship )
219220 end
220221
222+ def meta ( &block )
223+ self . meta_to_serialize = block
224+ end
225+
221226 def create_relationship ( base_key , relationship_type , options , block )
222227 name = base_key . to_sym
223228 if relationship_type == :has_many
@@ -232,18 +237,31 @@ def create_relationship(base_key, relationship_type, options, block)
232237 Relationship . new (
233238 key : options [ :key ] || run_key_transform ( base_key ) ,
234239 name : name ,
235- id_method_name : options [ :id_method_name ] || "#{ base_serialization_key } #{ id_postfix } " . to_sym ,
240+ id_method_name : compute_id_method_name (
241+ options [ :id_method_name ] ,
242+ "#{ base_serialization_key } #{ id_postfix } " . to_sym ,
243+ block
244+ ) ,
236245 record_type : options [ :record_type ] || run_key_transform ( base_key_sym ) ,
237246 object_method_name : options [ :object_method_name ] || name ,
238247 object_block : block ,
239248 serializer : compute_serializer_name ( options [ :serializer ] || base_key_sym ) ,
240249 relationship_type : relationship_type ,
241250 cached : options [ :cached ] ,
242251 polymorphic : fetch_polymorphic_option ( options ) ,
243- conditional_proc : options [ :if ]
252+ conditional_proc : options [ :if ] ,
253+ transform_method : @transform_method
244254 )
245255 end
246256
257+ def compute_id_method_name ( custom_id_method_name , id_method_name_from_relationship , block )
258+ if block . present?
259+ custom_id_method_name || :id
260+ else
261+ custom_id_method_name || id_method_name_from_relationship
262+ end
263+ end
264+
247265 def compute_serializer_name ( serializer_key )
248266 return serializer_key unless serializer_key . is_a? Symbol
249267 namespace = self . name . gsub ( /()?\w +Serializer$/ , '' )
@@ -275,10 +293,10 @@ def validate_includes!(includes)
275293 includes . detect do |include_item |
276294 klass = self
277295 parse_include_item ( include_item ) . each do |parsed_include |
278- relationship_to_include = klass . relationships_to_serialize [ parsed_include ]
296+ relationships_to_serialize = klass . relationships_to_serialize || { }
297+ relationship_to_include = relationships_to_serialize [ parsed_include ]
279298 raise ArgumentError , "#{ parsed_include } is not specified as a relationship on #{ klass . name } " unless relationship_to_include
280- raise NotImplementedError if relationship_to_include . polymorphic . is_a? ( Hash )
281- klass = relationship_to_include . serializer . to_s . constantize
299+ klass = relationship_to_include . serializer . to_s . constantize unless relationship_to_include . polymorphic . is_a? ( Hash )
282300 end
283301 end
284302 end
0 commit comments