@@ -21,7 +21,8 @@ class << self
2121 :cache_length ,
2222 :race_condition_ttl ,
2323 :cached ,
24- :data_links
24+ :data_links ,
25+ :fieldset
2526 end
2627 end
2728
@@ -40,36 +41,39 @@ def links_hash(record, params = {})
4041 end
4142 end
4243
43- def attributes_hash ( record , params = { } )
44- attributes_to_serialize . each_with_object ( { } ) do |( _k , attribute ) , hash |
44+ def attributes_hash ( record , fieldset = nil , params = { } )
45+ attributes = attributes_to_serialize
46+ attributes = attributes . slice ( *fieldset ) if fieldset . present?
47+ attributes . each_with_object ( { } ) do |( _k , attribute ) , hash |
4548 attribute . serialize ( record , params , hash )
4649 end
4750 end
4851
49- def relationships_hash ( record , relationships = nil , params = { } )
52+ def relationships_hash ( record , relationships = nil , fieldset = nil , params = { } )
5053 relationships = relationships_to_serialize if relationships . nil?
54+ relationships = relationships . slice ( *fieldset ) if fieldset . present?
5155
5256 relationships . each_with_object ( { } ) do |( _k , relationship ) , hash |
5357 relationship . serialize ( record , params , hash )
5458 end
5559 end
5660
57- def record_hash ( record , params = { } )
61+ def record_hash ( record , fieldset , params = { } )
5862 if cached
5963 record_hash = Rails . cache . fetch ( record . cache_key , expires_in : cache_length , race_condition_ttl : race_condition_ttl ) do
6064 temp_hash = id_hash ( id_from_record ( record ) , record_type , true )
61- temp_hash [ :attributes ] = attributes_hash ( record , params ) if attributes_to_serialize . present?
65+ temp_hash [ :attributes ] = attributes_hash ( record , fieldset , params ) if attributes_to_serialize . present?
6266 temp_hash [ :relationships ] = { }
63- temp_hash [ :relationships ] = relationships_hash ( record , cachable_relationships_to_serialize , params ) if cachable_relationships_to_serialize . present?
67+ temp_hash [ :relationships ] = relationships_hash ( record , cachable_relationships_to_serialize , fieldset , params ) if cachable_relationships_to_serialize . present?
6468 temp_hash [ :links ] = links_hash ( record , params ) if data_links . present?
6569 temp_hash
6670 end
6771 record_hash [ :relationships ] = record_hash [ :relationships ] . merge ( relationships_hash ( record , uncachable_relationships_to_serialize , params ) ) if uncachable_relationships_to_serialize . present?
6872 record_hash
6973 else
7074 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?
75+ record_hash [ :attributes ] = attributes_hash ( record , fieldset , params ) if attributes_to_serialize . present?
76+ record_hash [ :relationships ] = relationships_hash ( record , nil , fieldset , params ) if relationships_to_serialize . present?
7377 record_hash [ :links ] = links_hash ( record , params ) if data_links . present?
7478 record_hash
7579 end
@@ -100,7 +104,7 @@ def remaining_items(items)
100104 end
101105
102106 # includes handler
103- def get_included_records ( record , includes_list , known_included_objects , params = { } )
107+ def get_included_records ( record , includes_list , known_included_objects , fieldsets , params = { } )
104108 return unless includes_list . present?
105109
106110 includes_list . sort . each_with_object ( [ ] ) do |include_item , included_records |
@@ -120,15 +124,16 @@ def get_included_records(record, includes_list, known_included_objects, params =
120124
121125 included_objects . each do |inc_obj |
122126 if remaining_items ( items )
123- serializer_records = serializer . get_included_records ( inc_obj , remaining_items ( items ) , known_included_objects )
127+ serializer_records = serializer . get_included_records ( inc_obj , remaining_items ( items ) , known_included_objects , fieldsets )
124128 included_records . concat ( serializer_records ) unless serializer_records . empty?
125129 end
126130
127131 code = "#{ record_type } _#{ inc_obj . id } "
128132 next if known_included_objects . key? ( code )
129133
130134 known_included_objects [ code ] = inc_obj
131- included_records << serializer . record_hash ( inc_obj , params )
135+
136+ included_records << serializer . record_hash ( inc_obj , fieldsets [ serializer . reflected_record_type ] , params )
132137 end
133138 end
134139 end
0 commit comments