@@ -6,14 +6,14 @@ def self.fetch_fragments(resource_klass, serializer, context, cache_ids)
66 context_b64 = JSONAPI . configuration . resource_cache_digest_function . call ( context_json )
77 context_key = "ATTR-CTX-#{ context_b64 . gsub ( "/" , "_" ) } "
88
9- results = self . lookup ( resource_klass , serializer_config_key , context_key , cache_ids )
9+ results = self . lookup ( resource_klass , serializer_config_key , context , context_key , cache_ids )
1010
1111 miss_ids = results . select { |k , v | v . nil? } . keys
1212 unless miss_ids . empty?
1313 find_filters = { resource_klass . _primary_key => miss_ids . uniq }
1414 find_options = { context : context }
1515 resource_klass . find ( find_filters , find_options ) . each do |resource |
16- ( id , cr ) = write ( resource_klass , resource , serializer , serializer_config_key , context_key )
16+ ( id , cr ) = write ( resource_klass , resource , serializer , serializer_config_key , context , context_key )
1717 results [ id ] = cr
1818 end
1919 end
@@ -29,28 +29,16 @@ def self.fetch_fragments(resource_klass, serializer, context, cache_ids)
2929 return results
3030 end
3131
32- def self . from_cache_value ( resource_klass , h )
33- new (
34- resource_klass ,
35- h . fetch ( :id ) ,
36- h . fetch ( :type ) ,
37- h . fetch ( :fetchable ) ,
38- h . fetch ( :rels , nil ) ,
39- h . fetch ( :links , nil ) ,
40- h . fetch ( :attrs , nil ) ,
41- h . fetch ( :meta , nil )
42- )
43- end
44-
45- attr_reader :resource_klass , :id , :type , :fetchable_fields , :relationships ,
32+ attr_reader :resource_klass , :id , :type , :context , :fetchable_fields , :relationships ,
4633 :links_json , :attributes_json , :meta_json ,
4734 :preloaded_fragments
4835
49- def initialize ( resource_klass , id , type , fetchable_fields , relationships ,
36+ def initialize ( resource_klass , id , type , context , fetchable_fields , relationships ,
5037 links_json , attributes_json , meta_json )
5138 @resource_klass = resource_klass
5239 @id = id
5340 @type = type
41+ @context = context
5442 @fetchable_fields = Set . new ( fetchable_fields )
5543
5644 # Relationships left uncompiled because we'll often want to insert included ids on retrieval
@@ -76,9 +64,14 @@ def to_cache_value
7664 }
7765 end
7866
67+ def to_real_resource
68+ rs = Resource . resource_for ( self . type ) . find_by_keys ( [ self . id ] , { context : self . context } )
69+ return rs . try ( :first )
70+ end
71+
7972 private
8073
81- def self . lookup ( resource_klass , serializer_config_key , context_key , cache_ids )
74+ def self . lookup ( resource_klass , serializer_config_key , context , context_key , cache_ids )
8275 type = resource_klass . _type
8376
8477 keys = cache_ids . map do |( id , cache_key ) |
@@ -89,20 +82,35 @@ def self.lookup(resource_klass, serializer_config_key, context_key, cache_ids)
8982 return keys . each_with_object ( { } ) do |key , hash |
9083 ( _ , id , _ , _ ) = key
9184 if hits . has_key? ( key )
92- hash [ id ] = self . from_cache_value ( resource_klass , hits [ key ] )
85+ hash [ id ] = self . from_cache_value ( resource_klass , context , hits [ key ] )
9386 else
9487 hash [ id ] = nil
9588 end
9689 end
9790 end
9891
99- def self . write ( resource_klass , resource , serializer , serializer_config_key , context_key )
92+ def self . from_cache_value ( resource_klass , context , h )
93+ new (
94+ resource_klass ,
95+ h . fetch ( :id ) ,
96+ h . fetch ( :type ) ,
97+ context ,
98+ h . fetch ( :fetchable ) ,
99+ h . fetch ( :rels , nil ) ,
100+ h . fetch ( :links , nil ) ,
101+ h . fetch ( :attrs , nil ) ,
102+ h . fetch ( :meta , nil )
103+ )
104+ end
105+
106+ def self . write ( resource_klass , resource , serializer , serializer_config_key , context , context_key )
100107 ( id , cache_key ) = resource . cache_id
101108 json = serializer . object_hash ( resource ) # No inclusions passed to object_hash
102109 cr = self . new (
103110 resource_klass ,
104111 json [ 'id' ] ,
105112 json [ 'type' ] ,
113+ context ,
106114 resource . fetchable_fields ,
107115 json [ 'relationships' ] ,
108116 json [ 'links' ] ,
0 commit comments