@@ -44,15 +44,15 @@ def id_hash_from_record(record, record_types)
4444 id_hash ( record . id , record_type )
4545 end
4646
47- def ids_hash_from_record_and_relationship ( record , relationship )
47+ def ids_hash_from_record_and_relationship ( record , relationship , params = { } )
4848 polymorphic = relationship [ :polymorphic ]
4949
5050 return ids_hash (
51- fetch_id ( record , relationship ) ,
51+ fetch_id ( record , relationship , params ) ,
5252 relationship [ :record_type ]
5353 ) unless polymorphic
5454
55- return unless associated_object = fetch_associated_object ( record , relationship )
55+ return unless associated_object = fetch_associated_object ( record , relationship , params )
5656
5757 return associated_object . map do |object |
5858 id_hash_from_record object , polymorphic
@@ -71,14 +71,14 @@ def attributes_hash(record, params = {})
7171 end
7272 end
7373
74- def relationships_hash ( record , relationships = nil )
74+ def relationships_hash ( record , relationships = nil , params = { } )
7575 relationships = relationships_to_serialize if relationships . nil?
7676
7777 relationships . each_with_object ( { } ) do |( _k , relationship ) , hash |
7878 name = relationship [ :key ]
7979 empty_case = relationship [ :relationship_type ] == :has_many ? [ ] : nil
8080 hash [ name ] = {
81- data : ids_hash_from_record_and_relationship ( record , relationship ) || empty_case
81+ data : ids_hash_from_record_and_relationship ( record , relationship , params ) || empty_case
8282 }
8383 end
8484 end
@@ -89,15 +89,15 @@ def record_hash(record, params = {})
8989 temp_hash = id_hash ( id_from_record ( record ) , record_type , true )
9090 temp_hash [ :attributes ] = attributes_hash ( record , params ) if attributes_to_serialize . present?
9191 temp_hash [ :relationships ] = { }
92- temp_hash [ :relationships ] = relationships_hash ( record , cachable_relationships_to_serialize ) if cachable_relationships_to_serialize . present?
92+ temp_hash [ :relationships ] = relationships_hash ( record , cachable_relationships_to_serialize , params ) if cachable_relationships_to_serialize . present?
9393 temp_hash
9494 end
95- record_hash [ :relationships ] = record_hash [ :relationships ] . merge ( relationships_hash ( record , uncachable_relationships_to_serialize ) ) if uncachable_relationships_to_serialize . present?
95+ record_hash [ :relationships ] = record_hash [ :relationships ] . merge ( relationships_hash ( record , uncachable_relationships_to_serialize , params ) ) if uncachable_relationships_to_serialize . present?
9696 record_hash
9797 else
9898 record_hash = id_hash ( id_from_record ( record ) , record_type , true )
9999 record_hash [ :attributes ] = attributes_hash ( record , params ) if attributes_to_serialize . present?
100- record_hash [ :relationships ] = relationships_hash ( record ) if relationships_to_serialize . present?
100+ record_hash [ :relationships ] = relationships_hash ( record , nil , params ) if relationships_to_serialize . present?
101101 record_hash
102102 end
103103 end
@@ -115,9 +115,9 @@ def to_json(payload)
115115
116116 # includes handler
117117
118- def get_included_records ( record , includes_list , known_included_objects )
118+ def get_included_records ( record , includes_list , known_included_objects , params = { } )
119119 includes_list . each_with_object ( [ ] ) do |item , included_records |
120- included_objects = fetch_associated_object ( record , @relationships_to_serialize [ item ] )
120+ included_objects = fetch_associated_object ( record , @relationships_to_serialize [ item ] , params )
121121 next if included_objects . blank?
122122
123123 record_type = @relationships_to_serialize [ item ] [ :record_type ]
@@ -128,19 +128,19 @@ def get_included_records(record, includes_list, known_included_objects)
128128 code = "#{ record_type } _#{ inc_obj . id } "
129129 next if known_included_objects . key? ( code )
130130 known_included_objects [ code ] = inc_obj
131- included_records << serializer . record_hash ( inc_obj )
131+ included_records << serializer . record_hash ( inc_obj , params )
132132 end
133133 end
134134 end
135135
136- def fetch_associated_object ( record , relationship )
137- return relationship [ :object_block ] . call ( record ) unless relationship [ :object_block ] . nil?
136+ def fetch_associated_object ( record , relationship , params )
137+ return relationship [ :object_block ] . call ( record , params ) unless relationship [ :object_block ] . nil?
138138 record . send ( relationship [ :object_method_name ] )
139139 end
140140
141- def fetch_id ( record , relationship )
141+ def fetch_id ( record , relationship , params )
142142 unless relationship [ :object_block ] . nil?
143- object = relationship [ :object_block ] . call ( record )
143+ object = relationship [ :object_block ] . call ( record , params )
144144
145145 return object . map ( &:id ) if object . respond_to? :map
146146 return object . id
0 commit comments