4
4
require 'active_model/serializer/include_tree'
5
5
require 'active_model/serializer/associations'
6
6
require 'active_model/serializer/attributes'
7
+ require 'active_model/serializer/caching'
7
8
require 'active_model/serializer/configuration'
8
9
require 'active_model/serializer/fieldset'
9
10
require 'active_model/serializer/lint'
@@ -15,63 +16,19 @@ class Serializer
15
16
include Configuration
16
17
include Associations
17
18
include Attributes
19
+ include Caching
18
20
require 'active_model/serializer/adapter'
19
21
20
- # Matches
21
- # "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
22
- # AND
23
- # "/c/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
24
- # AS
25
- # c/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb
26
- CALLER_FILE = /
27
- \A # start of string
28
- .+ # file path (one or more characters)
29
- (?= # stop previous match when
30
- :\d + # a colon is followed by one or more digits
31
- :in # followed by a colon followed by in
32
- )
33
- /x
34
-
35
- # Hashes contents of file for +_cache_digest+
36
- def self . digest_caller_file ( caller_line )
37
- serializer_file_path = caller_line [ CALLER_FILE ]
38
- serializer_file_contents = IO . read ( serializer_file_path )
39
- Digest ::MD5 . hexdigest ( serializer_file_contents )
40
- rescue TypeError , Errno ::ENOENT
41
- warn <<-EOF . strip_heredoc
42
- Cannot digest non-existent file: '#{ caller_line } '.
43
- Please set `::_cache_digest` of the serializer
44
- if you'd like to cache it.
45
- EOF
46
- '' . freeze
47
- end
48
-
49
22
with_options instance_writer : false , instance_reader : false do |serializer |
50
23
serializer . class_attribute :_type , instance_reader : true
51
- serializer . class_attribute :_links # @api private : links definitions, @see Serializer#link
24
+ serializer . class_attribute :_links # @api private : links definitions, @see Serializer#link
52
25
self . _links ||= { }
53
- serializer . class_attribute :_cache # @api private : the cache object
54
- serializer . class_attribute :_fragmented # @api private : @see ::fragmented
55
- serializer . class_attribute :_cache_key # @api private : when present, is first item in cache_key
56
- serializer . class_attribute :_cache_only # @api private : when fragment caching, whitelists cached_attributes. Cannot combine with except
57
- serializer . class_attribute :_cache_except # @api private : when fragment caching, blacklists cached_attributes. Cannot combine with only
58
- serializer . class_attribute :_cache_options # @api private : used by CachedSerializer, passed to _cache.fetch
59
- # _cache_options include:
60
- # expires_in
61
- # compress
62
- # force
63
- # race_condition_ttl
64
- # Passed to ::_cache as
65
- # serializer._cache.fetch(cache_key, @klass._cache_options)
66
- serializer . class_attribute :_cache_digest # @api private : Generated
67
26
end
68
27
69
28
# Serializers inherit _attribute_mappings, _reflections, and _links.
70
29
# Generates a unique digest for each serializer at load.
71
30
def self . inherited ( base )
72
- caller_line = caller . first
73
31
base . _links = _links . dup
74
- base . _cache_digest = digest_caller_file ( caller_line )
75
32
super
76
33
end
77
34
@@ -86,43 +43,6 @@ def self.link(name, value = nil, &block)
86
43
_links [ name ] = block || value
87
44
end
88
45
89
- # @api private
90
- # Used by FragmentCache on the CachedSerializer
91
- # to call attribute methods on the fragmented cached serializer.
92
- def self . fragmented ( serializer )
93
- self . _fragmented = serializer
94
- end
95
-
96
- # Enables a serializer to be automatically cached
97
- #
98
- # Sets +::_cache+ object to <tt>ActionController::Base.cache_store</tt>
99
- # when Rails.configuration.action_controller.perform_caching
100
- #
101
- # @params options [Hash] with valid keys:
102
- # key : @see ::_cache_key
103
- # only : @see ::_cache_only
104
- # except : @see ::_cache_except
105
- # skip_digest : does not include digest in cache_key
106
- # all else : @see ::_cache_options
107
- #
108
- # @example
109
- # class PostSerializer < ActiveModel::Serializer
110
- # cache key: 'post', expires_in: 3.hours
111
- # attributes :title, :body
112
- #
113
- # has_many :comments
114
- # end
115
- #
116
- # @todo require less code comments. See
117
- # https://github.com/rails-api/active_model_serializers/pull/1249#issuecomment-146567837
118
- def self . cache ( options = { } )
119
- self . _cache = ActiveModelSerializers . config . cache_store if ActiveModelSerializers . config . perform_caching
120
- self . _cache_key = options . delete ( :key )
121
- self . _cache_only = options . delete ( :only )
122
- self . _cache_except = options . delete ( :except )
123
- self . _cache_options = ( options . empty? ) ? nil : options
124
- end
125
-
126
46
# @param resource [ActiveRecord::Base, ActiveModelSerializers::Model]
127
47
# @return [ActiveModel::Serializer]
128
48
# Preferentially returns
@@ -145,12 +65,6 @@ def self.adapter
145
65
ActiveModel ::Serializer ::Adapter . lookup ( config . adapter )
146
66
end
147
67
148
- # Used to cache serializer name => serializer class
149
- # when looked up by Serializer.get_serializer_for.
150
- def self . serializers_cache
151
- @serializers_cache ||= ThreadSafe ::Cache . new
152
- end
153
-
154
68
# @api private
155
69
def self . serializer_lookup_chain_for ( klass )
156
70
chain = [ ]
@@ -165,6 +79,12 @@ def self.serializer_lookup_chain_for(klass)
165
79
chain
166
80
end
167
81
82
+ # Used to cache serializer name => serializer class
83
+ # when looked up by Serializer.get_serializer_for.
84
+ def self . serializers_cache
85
+ @serializers_cache ||= ThreadSafe ::Cache . new
86
+ end
87
+
168
88
# @api private
169
89
# Find a serializer from a class and caches the lookup.
170
90
# Preferentially retuns:
0 commit comments