@@ -20,7 +20,8 @@ class << self
2020 :record_id ,
2121 :cache_length ,
2222 :race_condition_ttl ,
23- :cached
23+ :cached ,
24+ :data_links
2425 end
2526 end
2627
@@ -61,6 +62,12 @@ def ids_hash_from_record_and_relationship(record, relationship, params = {})
6162 id_hash_from_record associated_object , polymorphic
6263 end
6364
65+ def links_hash ( record , serializer_instance )
66+ @data_links . each_with_object ( { } ) do |( key , method ) , link_hash |
67+ link_hash [ key ] = method . is_a? ( Proc ) ? serializer_instance . instance_exec ( record , &method ) : record . public_send ( method )
68+ end
69+ end
70+
6471 def attributes_hash ( record , params = { } )
6572 attributes_to_serialize . each_with_object ( { } ) do |( key , method ) , attr_hash |
6673 attr_hash [ key ] = if method . is_a? ( Proc )
@@ -83,13 +90,17 @@ def relationships_hash(record, relationships = nil, params = {})
8390 end
8491 end
8592
86- def record_hash ( record , params = { } )
93+ def record_hash ( record , params = { } , serializer_instance )
8794 if cached
8895 record_hash = Rails . cache . fetch ( record . cache_key , expires_in : cache_length , race_condition_ttl : race_condition_ttl ) do
8996 temp_hash = id_hash ( id_from_record ( record ) , record_type , true )
9097 temp_hash [ :attributes ] = attributes_hash ( record , params ) if attributes_to_serialize . present?
9198 temp_hash [ :relationships ] = { }
9299 temp_hash [ :relationships ] = relationships_hash ( record , cachable_relationships_to_serialize , params ) if cachable_relationships_to_serialize . present?
100+ if @data_links . present?
101+ temp_links_hash = links_hash ( record , serializer_instance )
102+ temp_hash [ :links ] = temp_links_hash if temp_links_hash
103+ end
93104 temp_hash
94105 end
95106 record_hash [ :relationships ] = record_hash [ :relationships ] . merge ( relationships_hash ( record , uncachable_relationships_to_serialize , params ) ) if uncachable_relationships_to_serialize . present?
@@ -98,6 +109,10 @@ def record_hash(record, params = {})
98109 record_hash = id_hash ( id_from_record ( record ) , record_type , true )
99110 record_hash [ :attributes ] = attributes_hash ( record , params ) if attributes_to_serialize . present?
100111 record_hash [ :relationships ] = relationships_hash ( record , nil , params ) if relationships_to_serialize . present?
112+ if @data_links . present?
113+ temp_links_hash = links_hash ( record , serializer_instance )
114+ record_hash [ :links ] = temp_links_hash if temp_links_hash
115+ end
101116 record_hash
102117 end
103118 end
@@ -153,7 +168,7 @@ def get_included_records(record, includes_list, known_included_objects, params =
153168 next if known_included_objects . key? ( code )
154169
155170 known_included_objects [ code ] = inc_obj
156- included_records << serializer . record_hash ( inc_obj , params )
171+ included_records << serializer . record_hash ( inc_obj , params , serializer . new ( record ) )
157172 end
158173 end
159174 end
0 commit comments