@@ -40,36 +40,39 @@ def links_hash(record, params = {})
4040 end
4141 end
4242
43- def attributes_hash ( record , params = { } )
44- attributes_to_serialize . each_with_object ( { } ) do |( _k , attribute ) , hash |
43+ def attributes_hash ( record , fieldset = nil , params = { } )
44+ attributes = attributes_to_serialize
45+ attributes = attributes . slice ( *fieldset ) if fieldset . present?
46+ attributes . each_with_object ( { } ) do |( _k , attribute ) , hash |
4547 attribute . serialize ( record , params , hash )
4648 end
4749 end
4850
49- def relationships_hash ( record , relationships = nil , params = { } )
51+ def relationships_hash ( record , relationships = nil , fieldset = nil , params = { } )
5052 relationships = relationships_to_serialize if relationships . nil?
53+ relationships = relationships . slice ( *fieldset ) if fieldset . present?
5154
5255 relationships . each_with_object ( { } ) do |( _k , relationship ) , hash |
5356 relationship . serialize ( record , params , hash )
5457 end
5558 end
5659
57- def record_hash ( record , params = { } )
60+ def record_hash ( record , fieldset , params = { } )
5861 if cached
5962 record_hash = Rails . cache . fetch ( record . cache_key , expires_in : cache_length , race_condition_ttl : race_condition_ttl ) do
6063 temp_hash = id_hash ( id_from_record ( record ) , record_type , true )
61- temp_hash [ :attributes ] = attributes_hash ( record , params ) if attributes_to_serialize . present?
64+ temp_hash [ :attributes ] = attributes_hash ( record , fieldset , params ) if attributes_to_serialize . present?
6265 temp_hash [ :relationships ] = { }
63- temp_hash [ :relationships ] = relationships_hash ( record , cachable_relationships_to_serialize , params ) if cachable_relationships_to_serialize . present?
66+ temp_hash [ :relationships ] = relationships_hash ( record , cachable_relationships_to_serialize , fieldset , params ) if cachable_relationships_to_serialize . present?
6467 temp_hash [ :links ] = links_hash ( record , params ) if data_links . present?
6568 temp_hash
6669 end
6770 record_hash [ :relationships ] = record_hash [ :relationships ] . merge ( relationships_hash ( record , uncachable_relationships_to_serialize , params ) ) if uncachable_relationships_to_serialize . present?
6871 record_hash
6972 else
7073 record_hash = id_hash ( id_from_record ( record ) , record_type , true )
71- record_hash [ :attributes ] = attributes_hash ( record , params ) if attributes_to_serialize . present?
72- record_hash [ :relationships ] = relationships_hash ( record , nil , params ) if relationships_to_serialize . present?
74+ record_hash [ :attributes ] = attributes_hash ( record , fieldset , params ) if attributes_to_serialize . present?
75+ record_hash [ :relationships ] = relationships_hash ( record , nil , fieldset , params ) if relationships_to_serialize . present?
7376 record_hash [ :links ] = links_hash ( record , params ) if data_links . present?
7477 record_hash
7578 end
@@ -100,7 +103,7 @@ def remaining_items(items)
100103 end
101104
102105 # includes handler
103- def get_included_records ( record , includes_list , known_included_objects , params = { } )
106+ def get_included_records ( record , includes_list , known_included_objects , fieldsets , params = { } )
104107 return unless includes_list . present?
105108
106109 includes_list . sort . each_with_object ( [ ] ) do |include_item , included_records |
@@ -120,15 +123,16 @@ def get_included_records(record, includes_list, known_included_objects, params =
120123
121124 included_objects . each do |inc_obj |
122125 if remaining_items ( items )
123- serializer_records = serializer . get_included_records ( inc_obj , remaining_items ( items ) , known_included_objects , params )
126+ serializer_records = serializer . get_included_records ( inc_obj , remaining_items ( items ) , known_included_objects , fieldsets , params )
124127 included_records . concat ( serializer_records ) unless serializer_records . empty?
125128 end
126129
127130 code = "#{ record_type } _#{ inc_obj . id } "
128131 next if known_included_objects . key? ( code )
129132
130133 known_included_objects [ code ] = inc_obj
131- included_records << serializer . record_hash ( inc_obj , params )
134+
135+ included_records << serializer . record_hash ( inc_obj , fieldsets [ serializer . record_type ] , params )
132136 end
133137 end
134138 end
0 commit comments