incrdecr_cached_counts 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.rdoc +7 -5
- data/lib/cached_counts/version.rb +1 -1
- metadata +2 -51
- data/html/CachedCounts/ClassMethods.html +0 -960
- data/html/CachedCounts/Railtie.html +0 -102
- data/html/CachedCounts.html +0 -197
- data/html/README_rdoc.html +0 -152
- data/html/created.rid +0 -7
- data/html/css/fonts.css +0 -167
- data/html/css/rdoc.css +0 -590
- data/html/fonts/Lato-Light.ttf +0 -0
- data/html/fonts/Lato-LightItalic.ttf +0 -0
- data/html/fonts/Lato-Regular.ttf +0 -0
- data/html/fonts/Lato-RegularItalic.ttf +0 -0
- data/html/fonts/SourceCodePro-Bold.ttf +0 -0
- data/html/fonts/SourceCodePro-Regular.ttf +0 -0
- data/html/images/add.png +0 -0
- data/html/images/arrow_up.png +0 -0
- data/html/images/brick.png +0 -0
- data/html/images/brick_link.png +0 -0
- data/html/images/bug.png +0 -0
- data/html/images/bullet_black.png +0 -0
- data/html/images/bullet_toggle_minus.png +0 -0
- data/html/images/bullet_toggle_plus.png +0 -0
- data/html/images/date.png +0 -0
- data/html/images/delete.png +0 -0
- data/html/images/find.png +0 -0
- data/html/images/loadingAnimation.gif +0 -0
- data/html/images/macFFBgHack.png +0 -0
- data/html/images/package.png +0 -0
- data/html/images/page_green.png +0 -0
- data/html/images/page_white_text.png +0 -0
- data/html/images/page_white_width.png +0 -0
- data/html/images/plugin.png +0 -0
- data/html/images/ruby.png +0 -0
- data/html/images/tag_blue.png +0 -0
- data/html/images/tag_green.png +0 -0
- data/html/images/transparent.png +0 -0
- data/html/images/wrench.png +0 -0
- data/html/images/wrench_orange.png +0 -0
- data/html/images/zoom.png +0 -0
- data/html/index.html +0 -155
- data/html/js/darkfish.js +0 -161
- data/html/js/jquery.js +0 -4
- data/html/js/navigation.js +0 -142
- data/html/js/navigation.js.gz +0 -0
- data/html/js/search.js +0 -109
- data/html/js/search_index.js +0 -1
- data/html/js/search_index.js.gz +0 -0
- data/html/js/searcher.js +0 -228
- data/html/js/searcher.js.gz +0 -0
- data/html/table_of_contents.html +0 -144
@@ -1,960 +0,0 @@
|
|
1
|
-
<!DOCTYPE html>
|
2
|
-
|
3
|
-
<html>
|
4
|
-
<head>
|
5
|
-
<meta charset="UTF-8">
|
6
|
-
|
7
|
-
<title>module CachedCounts::ClassMethods - RDoc Documentation</title>
|
8
|
-
|
9
|
-
<script type="text/javascript">
|
10
|
-
var rdoc_rel_prefix = "../";
|
11
|
-
</script>
|
12
|
-
|
13
|
-
<script src="../js/jquery.js"></script>
|
14
|
-
<script src="../js/darkfish.js"></script>
|
15
|
-
|
16
|
-
<link href="../css/fonts.css" rel="stylesheet">
|
17
|
-
<link href="../css/rdoc.css" rel="stylesheet">
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
<body id="top" role="document" class="module">
|
22
|
-
<nav role="navigation">
|
23
|
-
<div id="project-navigation">
|
24
|
-
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
|
25
|
-
<h2>
|
26
|
-
<a href="../index.html" rel="home">Home</a>
|
27
|
-
</h2>
|
28
|
-
|
29
|
-
<div id="table-of-contents-navigation">
|
30
|
-
<a href="../table_of_contents.html#pages">Pages</a>
|
31
|
-
<a href="../table_of_contents.html#classes">Classes</a>
|
32
|
-
<a href="../table_of_contents.html#methods">Methods</a>
|
33
|
-
</div>
|
34
|
-
</div>
|
35
|
-
|
36
|
-
<div id="search-section" role="search" class="project-section initially-hidden">
|
37
|
-
<form action="#" method="get" accept-charset="utf-8">
|
38
|
-
<div id="search-field-wrapper">
|
39
|
-
<input id="search-field" role="combobox" aria-label="Search"
|
40
|
-
aria-autocomplete="list" aria-controls="search-results"
|
41
|
-
type="text" name="search" placeholder="Search" spellcheck="false"
|
42
|
-
title="Type to search, Up and Down to navigate, Enter to load">
|
43
|
-
</div>
|
44
|
-
|
45
|
-
<ul id="search-results" aria-label="Search Results"
|
46
|
-
aria-busy="false" aria-expanded="false"
|
47
|
-
aria-atomic="false" class="initially-hidden"></ul>
|
48
|
-
</form>
|
49
|
-
</div>
|
50
|
-
|
51
|
-
</div>
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
<div id="class-metadata">
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
<!-- Method Quickref -->
|
61
|
-
<div id="method-list-section" class="nav-section">
|
62
|
-
<h3>Methods</h3>
|
63
|
-
|
64
|
-
<ul class="link-list" role="directory">
|
65
|
-
|
66
|
-
<li ><a href="#method-i-add_association_counting_hooks">#add_association_counting_hooks</a>
|
67
|
-
|
68
|
-
<li ><a href="#method-i-add_count_attribute_methods">#add_count_attribute_methods</a>
|
69
|
-
|
70
|
-
<li ><a href="#method-i-add_counting_hooks">#add_counting_hooks</a>
|
71
|
-
|
72
|
-
<li ><a href="#method-i-add_scope_counting_hooks">#add_scope_counting_hooks</a>
|
73
|
-
|
74
|
-
<li ><a href="#method-i-association_count_key">#association_count_key</a>
|
75
|
-
|
76
|
-
<li ><a href="#method-i-caches_count_of">#caches_count_of</a>
|
77
|
-
|
78
|
-
<li ><a href="#method-i-caches_count_of-21">#caches_count_of!</a>
|
79
|
-
|
80
|
-
<li ><a href="#method-i-caches_count_where">#caches_count_where</a>
|
81
|
-
|
82
|
-
<li ><a href="#method-i-caches_count_where-21">#caches_count_where!</a>
|
83
|
-
|
84
|
-
<li ><a href="#method-i-default_race_condition_fallback_proc">#default_race_condition_fallback_proc</a>
|
85
|
-
|
86
|
-
<li ><a href="#method-i-define_association_count_attribute">#define_association_count_attribute</a>
|
87
|
-
|
88
|
-
<li ><a href="#method-i-define_scope_count_attribute">#define_scope_count_attribute</a>
|
89
|
-
|
90
|
-
<li ><a href="#method-i-generate_association_counting_hook_key_getter">#generate_association_counting_hook_key_getter</a>
|
91
|
-
|
92
|
-
<li ><a href="#method-i-generate_association_relation_getter">#generate_association_relation_getter</a>
|
93
|
-
|
94
|
-
<li ><a href="#method-i-scope_count_key">#scope_count_key</a>
|
95
|
-
|
96
|
-
</ul>
|
97
|
-
</div>
|
98
|
-
|
99
|
-
</div>
|
100
|
-
</nav>
|
101
|
-
|
102
|
-
<main role="main" aria-labelledby="module-CachedCounts::ClassMethods">
|
103
|
-
<h1 id="module-CachedCounts::ClassMethods" class="module">
|
104
|
-
module CachedCounts::ClassMethods
|
105
|
-
</h1>
|
106
|
-
|
107
|
-
<section class="description">
|
108
|
-
|
109
|
-
</section>
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
<section id="5Buntitled-5D" class="documentation-section">
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
<section id="public-instance-5Buntitled-5D-method-details" class="method-section">
|
125
|
-
<header>
|
126
|
-
<h3>Public Instance Methods</h3>
|
127
|
-
</header>
|
128
|
-
|
129
|
-
|
130
|
-
<div id="method-i-association_count_key" class="method-detail ">
|
131
|
-
|
132
|
-
<div class="method-heading">
|
133
|
-
<span class="method-name">association_count_key</span><span
|
134
|
-
class="method-args">(counter_id, attribute_name, version = 1)</span>
|
135
|
-
|
136
|
-
<span class="method-click-advice">click to toggle source</span>
|
137
|
-
|
138
|
-
</div>
|
139
|
-
|
140
|
-
|
141
|
-
<div class="method-description">
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
<div class="method-source-code" id="association_count_key-source">
|
149
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 98</span>
|
150
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">association_count_key</span>(<span class="ruby-identifier">counter_id</span>, <span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">version</span> = <span class="ruby-value">1</span>)
|
151
|
-
<span class="ruby-node">"#{name}:#{counter_id}:#{attribute_name}_count:#{version}"</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">counter_id</span>.<span class="ruby-identifier">nil?</span>
|
152
|
-
<span class="ruby-keyword">end</span></pre>
|
153
|
-
</div>
|
154
|
-
|
155
|
-
</div>
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
</div>
|
161
|
-
|
162
|
-
|
163
|
-
<div id="method-i-caches_count_of" class="method-detail ">
|
164
|
-
|
165
|
-
<div class="method-heading">
|
166
|
-
<span class="method-name">caches_count_of</span><span
|
167
|
-
class="method-args">(attribute_name, options = {})</span>
|
168
|
-
|
169
|
-
<span class="method-click-advice">click to toggle source</span>
|
170
|
-
|
171
|
-
</div>
|
172
|
-
|
173
|
-
|
174
|
-
<div class="method-description">
|
175
|
-
|
176
|
-
<p>Cache the count for an association in memcached.</p>
|
177
|
-
|
178
|
-
<p>e.g.</p>
|
179
|
-
|
180
|
-
<pre>User.caches_count_of :friends
|
181
|
-
> User.first.friends_count # Users.first.friends.count, but cached</pre>
|
182
|
-
|
183
|
-
<p>Automatically adds after_commit hooks to the associated class which
|
184
|
-
increment/decrement the value in memcached when needed. Queries the db on
|
185
|
-
cache miss.</p>
|
186
|
-
|
187
|
-
<p>Valid options:</p>
|
188
|
-
|
189
|
-
<pre>:association
|
190
|
-
Name of the association to count. Defaults to the attribute_name
|
191
|
-
(the required argument to `caches_count_of`).
|
192
|
-
|
193
|
-
:alias
|
194
|
-
Alias(es) for the count attribute. Useful with join tables.
|
195
|
-
e.g. `caches_count_of :user_departments, alias: 'users'`
|
196
|
-
> Department.first.users_count
|
197
|
-
|
198
|
-
:expires_in
|
199
|
-
Expiry for the cached value.
|
200
|
-
|
201
|
-
:if
|
202
|
-
proc passed through to the after_commit hooks on the counted class;
|
203
|
-
decides whether an object counts towards the association total.
|
204
|
-
|
205
|
-
:scope
|
206
|
-
proc used like an ActiveRecord scope on the counted class on cache misses.
|
207
|
-
|
208
|
-
:version
|
209
|
-
Cache version - bump if you change the definition of a count.</pre>
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
<div class="method-source-code" id="caches_count_of-source">
|
215
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 86</span>
|
216
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">caches_count_of</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">options</span> = {})
|
217
|
-
<span class="ruby-comment"># Delay actual run to work around circular dependencies</span>
|
218
|
-
<span class="ruby-identifier">klass</span> = <span class="ruby-keyword">self</span>
|
219
|
-
<span class="ruby-constant">ActiveSupport</span>.<span class="ruby-identifier">on_load</span> <span class="ruby-value">:cached_counts</span> <span class="ruby-keyword">do</span>
|
220
|
-
<span class="ruby-identifier">klass</span>.<span class="ruby-identifier">send</span> <span class="ruby-value">:caches_count_of!</span>, <span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">options</span>
|
221
|
-
<span class="ruby-keyword">end</span>
|
222
|
-
<span class="ruby-keyword">end</span></pre>
|
223
|
-
</div>
|
224
|
-
|
225
|
-
</div>
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
</div>
|
231
|
-
|
232
|
-
|
233
|
-
<div id="method-i-caches_count_where" class="method-detail ">
|
234
|
-
|
235
|
-
<div class="method-heading">
|
236
|
-
<span class="method-name">caches_count_where</span><span
|
237
|
-
class="method-args">(attribute_name, options = {})</span>
|
238
|
-
|
239
|
-
<span class="method-click-advice">click to toggle source</span>
|
240
|
-
|
241
|
-
</div>
|
242
|
-
|
243
|
-
|
244
|
-
<div class="method-description">
|
245
|
-
|
246
|
-
<p>Cache the count for a scope in memcached.</p>
|
247
|
-
|
248
|
-
<p>e.g.</p>
|
249
|
-
|
250
|
-
<pre>User.caches_count_where :confirmed
|
251
|
-
> User.confirmed_count # User.confirmed.count, but cached</pre>
|
252
|
-
|
253
|
-
<p>Automatically adds after_commit hooks which increment/decrement the value
|
254
|
-
in memcached when needed. Queries the db on cache miss.</p>
|
255
|
-
|
256
|
-
<p>Valid options:</p>
|
257
|
-
|
258
|
-
<pre>:scope
|
259
|
-
Name of the scope to count. Defaults to the attribute_name
|
260
|
-
(the required argument to `caches_count_where`).
|
261
|
-
|
262
|
-
:alias
|
263
|
-
Alias(es) for the count attribute.
|
264
|
-
e.g. `caches_count_where :confirmed, alias: 'sitemap'`
|
265
|
-
> User.sitemap_count
|
266
|
-
|
267
|
-
:expires_in
|
268
|
-
Expiry for the cached value.
|
269
|
-
|
270
|
-
:if
|
271
|
-
proc passed through to the after_commit hooks;
|
272
|
-
decides whether an object counts towards the association total.
|
273
|
-
|
274
|
-
:version
|
275
|
-
Cache version - bump if you change the definition of a count.
|
276
|
-
|
277
|
-
:race_condition_fallback
|
278
|
-
Fallback to the result of this proc if the cache is empty, while
|
279
|
-
loading the actual value from the db. Works similarly to
|
280
|
-
race_condition_ttl but for empty caches rather than expired values.
|
281
|
-
Meant to prevent a thundering-herd scenario, if for example a
|
282
|
-
memcached instance goes away. Can be nil; defaults to using a value
|
283
|
-
grabbed from the cache or DB at startup.</pre>
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
<div class="method-source-code" id="caches_count_where-source">
|
289
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 45</span>
|
290
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">caches_count_where</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">options</span> = {})
|
291
|
-
<span class="ruby-comment"># Delay actual run to work around circular dependencies</span>
|
292
|
-
<span class="ruby-identifier">klass</span> = <span class="ruby-keyword">self</span>
|
293
|
-
<span class="ruby-constant">ActiveSupport</span>.<span class="ruby-identifier">on_load</span> <span class="ruby-value">:cached_counts</span> <span class="ruby-keyword">do</span>
|
294
|
-
<span class="ruby-identifier">klass</span>.<span class="ruby-identifier">send</span> <span class="ruby-value">:caches_count_where!</span>, <span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">options</span>
|
295
|
-
<span class="ruby-keyword">end</span>
|
296
|
-
<span class="ruby-keyword">end</span></pre>
|
297
|
-
</div>
|
298
|
-
|
299
|
-
</div>
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
</div>
|
305
|
-
|
306
|
-
|
307
|
-
<div id="method-i-scope_count_key" class="method-detail ">
|
308
|
-
|
309
|
-
<div class="method-heading">
|
310
|
-
<span class="method-name">scope_count_key</span><span
|
311
|
-
class="method-args">(attribute_name, version = 1)</span>
|
312
|
-
|
313
|
-
<span class="method-click-advice">click to toggle source</span>
|
314
|
-
|
315
|
-
</div>
|
316
|
-
|
317
|
-
|
318
|
-
<div class="method-description">
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
<div class="method-source-code" id="scope_count_key-source">
|
326
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 94</span>
|
327
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">scope_count_key</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">version</span> = <span class="ruby-value">1</span>)
|
328
|
-
<span class="ruby-node">"#{name}:#{attribute_name}_count:#{version}"</span>
|
329
|
-
<span class="ruby-keyword">end</span></pre>
|
330
|
-
</div>
|
331
|
-
|
332
|
-
</div>
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
</div>
|
338
|
-
|
339
|
-
|
340
|
-
</section>
|
341
|
-
|
342
|
-
<section id="protected-instance-5Buntitled-5D-method-details" class="method-section">
|
343
|
-
<header>
|
344
|
-
<h3>Protected Instance Methods</h3>
|
345
|
-
</header>
|
346
|
-
|
347
|
-
|
348
|
-
<div id="method-i-add_association_counting_hooks" class="method-detail ">
|
349
|
-
|
350
|
-
<div class="method-heading">
|
351
|
-
<span class="method-name">add_association_counting_hooks</span><span
|
352
|
-
class="method-args">(attribute_name, association, options)</span>
|
353
|
-
|
354
|
-
<span class="method-click-advice">click to toggle source</span>
|
355
|
-
|
356
|
-
</div>
|
357
|
-
|
358
|
-
|
359
|
-
<div class="method-description">
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
<div class="method-source-code" id="add_association_counting_hooks-source">
|
367
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 204</span>
|
368
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">add_association_counting_hooks</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">association</span>, <span class="ruby-identifier">options</span>)
|
369
|
-
<span class="ruby-identifier">key_getter</span> = <span class="ruby-identifier">generate_association_counting_hook_key_getter</span> <span class="ruby-identifier">association</span>, <span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">options</span>
|
370
|
-
|
371
|
-
<span class="ruby-identifier">add_counting_hooks</span>(
|
372
|
-
<span class="ruby-node">"#{name.demodulize.underscore}_#{attribute_name}"</span>,
|
373
|
-
<span class="ruby-identifier">key_getter</span>,
|
374
|
-
<span class="ruby-identifier">association</span>.<span class="ruby-identifier">klass</span>,
|
375
|
-
<span class="ruby-identifier">options</span>
|
376
|
-
)
|
377
|
-
<span class="ruby-keyword">end</span></pre>
|
378
|
-
</div>
|
379
|
-
|
380
|
-
</div>
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
</div>
|
386
|
-
|
387
|
-
|
388
|
-
<div id="method-i-add_count_attribute_methods" class="method-detail ">
|
389
|
-
|
390
|
-
<div class="method-heading">
|
391
|
-
<span class="method-name">add_count_attribute_methods</span><span
|
392
|
-
class="method-args">(attribute_name, key_getter, relation_getter, define_with, counted_class, options)</span>
|
393
|
-
|
394
|
-
<span class="method-click-advice">click to toggle source</span>
|
395
|
-
|
396
|
-
</div>
|
397
|
-
|
398
|
-
|
399
|
-
<div class="method-description">
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
<div class="method-source-code" id="add_count_attribute_methods-source">
|
407
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 215</span>
|
408
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">add_count_attribute_methods</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">key_getter</span>, <span class="ruby-identifier">relation_getter</span>, <span class="ruby-identifier">define_with</span>, <span class="ruby-identifier">counted_class</span>, <span class="ruby-identifier">options</span>)
|
409
|
-
<span class="ruby-identifier">expires_in</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span> <span class="ruby-value">:expires_in</span>, <span class="ruby-value">1</span>.<span class="ruby-identifier">week</span>
|
410
|
-
<span class="ruby-identifier">race_condition_fallback</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span> <span class="ruby-value">:race_condition_fallback</span>, <span class="ruby-keyword">nil</span>
|
411
|
-
|
412
|
-
<span class="ruby-identifier">key_method</span> = <span class="ruby-node">"#{attribute_name}_count_key"</span>
|
413
|
-
|
414
|
-
<span class="ruby-identifier">send</span> <span class="ruby-identifier">define_with</span>, <span class="ruby-identifier">key_method</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">key_getter</span>
|
415
|
-
|
416
|
-
<span class="ruby-identifier">send</span> <span class="ruby-identifier">define_with</span>, <span class="ruby-node">"#{attribute_name}_count"</span> <span class="ruby-keyword">do</span>
|
417
|
-
<span class="ruby-identifier">val</span> = <span class="ruby-constant">Rails</span>.<span class="ruby-identifier">cache</span>.<span class="ruby-identifier">fetch</span>(
|
418
|
-
<span class="ruby-identifier">send</span>(<span class="ruby-identifier">key_method</span>),
|
419
|
-
<span class="ruby-identifier">expires_in</span><span class="ruby-operator">:</span> <span class="ruby-identifier">expires_in</span>,
|
420
|
-
<span class="ruby-identifier">race_condition_ttl</span><span class="ruby-operator">:</span> <span class="ruby-value">30</span>.<span class="ruby-identifier">seconds</span>,
|
421
|
-
<span class="ruby-identifier">raw</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span> <span class="ruby-comment"># Necessary for incrementing to work correctly</span>
|
422
|
-
) <span class="ruby-keyword">do</span>
|
423
|
-
<span class="ruby-keyword">if</span> <span class="ruby-identifier">race_condition_fallback</span>
|
424
|
-
<span class="ruby-comment"># Ensure that other reads find something in the cache, but</span>
|
425
|
-
<span class="ruby-comment"># continue calculating here because the default is likely inaccurate.</span>
|
426
|
-
<span class="ruby-identifier">fallback_value</span> = <span class="ruby-identifier">instance_exec</span> <span class="ruby-operator">&</span><span class="ruby-identifier">race_condition_fallback</span>
|
427
|
-
<span class="ruby-constant">Rails</span>.<span class="ruby-identifier">cache</span>.<span class="ruby-identifier">write</span>(
|
428
|
-
<span class="ruby-identifier">send</span>(<span class="ruby-identifier">key_method</span>),
|
429
|
-
<span class="ruby-identifier">fallback_value</span>.<span class="ruby-identifier">to_i</span>,
|
430
|
-
<span class="ruby-identifier">expires_in</span><span class="ruby-operator">:</span> <span class="ruby-value">30</span>.<span class="ruby-identifier">seconds</span>,
|
431
|
-
<span class="ruby-identifier">raw</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>
|
432
|
-
)
|
433
|
-
<span class="ruby-keyword">end</span>
|
434
|
-
|
435
|
-
<span class="ruby-identifier">relation</span> = <span class="ruby-identifier">instance_exec</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">relation_getter</span>)
|
436
|
-
<span class="ruby-identifier">relation</span> = <span class="ruby-identifier">relation</span>.<span class="ruby-identifier">reorder</span>(<span class="ruby-string">''</span>)
|
437
|
-
<span class="ruby-identifier">relation</span>.<span class="ruby-identifier">select_values</span> = [<span class="ruby-string">'count(*)'</span>]
|
438
|
-
|
439
|
-
<span class="ruby-identifier">conn</span> = <span class="ruby-constant">CachedCounts</span>.<span class="ruby-identifier">connection_for</span>(<span class="ruby-identifier">counted_class</span>)
|
440
|
-
<span class="ruby-keyword">if</span> <span class="ruby-constant">Rails</span>.<span class="ruby-identifier">version</span> <span class="ruby-operator"><</span> <span class="ruby-string">'4.2'</span>.<span class="ruby-identifier">freeze</span>
|
441
|
-
<span class="ruby-identifier">conn</span>.<span class="ruby-identifier">select_value</span>(<span class="ruby-identifier">relation</span>.<span class="ruby-identifier">to_sql</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">relation</span>.<span class="ruby-identifier">values</span>[<span class="ruby-value">:bind</span>] <span class="ruby-operator">||</span> []).<span class="ruby-identifier">to_i</span>
|
442
|
-
<span class="ruby-keyword">else</span>
|
443
|
-
<span class="ruby-identifier">conn</span>.<span class="ruby-identifier">select_value</span>(<span class="ruby-identifier">relation</span>.<span class="ruby-identifier">to_sql</span>).<span class="ruby-identifier">to_i</span>
|
444
|
-
<span class="ruby-keyword">end</span>
|
445
|
-
<span class="ruby-keyword">end</span>
|
446
|
-
|
447
|
-
<span class="ruby-keyword">if</span> <span class="ruby-identifier">val</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Cache</span><span class="ruby-operator">::</span><span class="ruby-constant">Entry</span>)
|
448
|
-
<span class="ruby-identifier">val</span>.<span class="ruby-identifier">value</span>.<span class="ruby-identifier">to_i</span>
|
449
|
-
<span class="ruby-keyword">else</span>
|
450
|
-
<span class="ruby-identifier">val</span>.<span class="ruby-identifier">to_i</span>
|
451
|
-
<span class="ruby-keyword">end</span>
|
452
|
-
<span class="ruby-keyword">end</span>
|
453
|
-
|
454
|
-
<span class="ruby-identifier">send</span> <span class="ruby-identifier">define_with</span>, <span class="ruby-node">"#{attribute_name}_count="</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
|
455
|
-
<span class="ruby-constant">Rails</span>.<span class="ruby-identifier">cache</span>.<span class="ruby-identifier">write</span>(
|
456
|
-
<span class="ruby-identifier">send</span>(<span class="ruby-identifier">key_method</span>),
|
457
|
-
<span class="ruby-identifier">value</span>.<span class="ruby-identifier">to_i</span>,
|
458
|
-
<span class="ruby-identifier">expires_in</span><span class="ruby-operator">:</span> <span class="ruby-identifier">expires_in</span>,
|
459
|
-
<span class="ruby-identifier">raw</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>
|
460
|
-
)
|
461
|
-
<span class="ruby-keyword">end</span>
|
462
|
-
|
463
|
-
<span class="ruby-identifier">send</span> <span class="ruby-identifier">define_with</span>, <span class="ruby-node">"expire_#{attribute_name}_count"</span> <span class="ruby-keyword">do</span>
|
464
|
-
<span class="ruby-constant">Rails</span>.<span class="ruby-identifier">cache</span>.<span class="ruby-identifier">delete</span> <span class="ruby-identifier">send</span>(<span class="ruby-identifier">key_method</span>)
|
465
|
-
<span class="ruby-keyword">end</span>
|
466
|
-
<span class="ruby-keyword">end</span></pre>
|
467
|
-
</div>
|
468
|
-
|
469
|
-
</div>
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
</div>
|
475
|
-
|
476
|
-
|
477
|
-
<div id="method-i-add_counting_hooks" class="method-detail ">
|
478
|
-
|
479
|
-
<div class="method-heading">
|
480
|
-
<span class="method-name">add_counting_hooks</span><span
|
481
|
-
class="method-args">(attribute_name, key_getter, counted_class, options)</span>
|
482
|
-
|
483
|
-
<span class="method-click-advice">click to toggle source</span>
|
484
|
-
|
485
|
-
</div>
|
486
|
-
|
487
|
-
|
488
|
-
<div class="method-description">
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
<div class="method-source-code" id="add_counting_hooks-source">
|
496
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 275</span>
|
497
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">add_counting_hooks</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">key_getter</span>, <span class="ruby-identifier">counted_class</span>, <span class="ruby-identifier">options</span>)
|
498
|
-
<span class="ruby-identifier">increment_hook</span> = <span class="ruby-node">"increment_#{attribute_name}_count"</span>
|
499
|
-
<span class="ruby-identifier">counted_class</span>.<span class="ruby-identifier">send</span> <span class="ruby-value">:define_method</span>, <span class="ruby-identifier">increment_hook</span> <span class="ruby-keyword">do</span>
|
500
|
-
<span class="ruby-keyword">if</span> (<span class="ruby-identifier">key</span> = <span class="ruby-identifier">instance_exec</span> <span class="ruby-operator">&</span><span class="ruby-identifier">key_getter</span>)
|
501
|
-
<span class="ruby-constant">Rails</span>.<span class="ruby-identifier">cache</span>.<span class="ruby-identifier">increment</span>(
|
502
|
-
<span class="ruby-identifier">key</span>,
|
503
|
-
<span class="ruby-value">1</span>,
|
504
|
-
<span class="ruby-identifier">initial</span><span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span> <span class="ruby-comment"># Increment only if the key already exists</span>
|
505
|
-
)
|
506
|
-
<span class="ruby-keyword">end</span>
|
507
|
-
<span class="ruby-keyword">end</span>
|
508
|
-
|
509
|
-
<span class="ruby-identifier">decrement_hook</span> = <span class="ruby-node">"decrement_#{attribute_name}_count"</span>
|
510
|
-
<span class="ruby-identifier">counted_class</span>.<span class="ruby-identifier">send</span> <span class="ruby-value">:define_method</span>, <span class="ruby-identifier">decrement_hook</span> <span class="ruby-keyword">do</span>
|
511
|
-
<span class="ruby-keyword">if</span> (<span class="ruby-identifier">key</span> = <span class="ruby-identifier">instance_exec</span> <span class="ruby-operator">&</span><span class="ruby-identifier">key_getter</span>)
|
512
|
-
<span class="ruby-constant">Rails</span>.<span class="ruby-identifier">cache</span>.<span class="ruby-identifier">decrement</span>(
|
513
|
-
<span class="ruby-identifier">key</span>,
|
514
|
-
<span class="ruby-value">1</span>,
|
515
|
-
<span class="ruby-identifier">initial</span><span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span> <span class="ruby-comment"># Decrement only if the key already exists</span>
|
516
|
-
)
|
517
|
-
<span class="ruby-keyword">end</span>
|
518
|
-
<span class="ruby-keyword">end</span>
|
519
|
-
|
520
|
-
<span class="ruby-identifier">counted_class</span>.<span class="ruby-identifier">after_commit</span> <span class="ruby-identifier">increment_hook</span>, <span class="ruby-identifier">options</span>.<span class="ruby-identifier">slice</span>(<span class="ruby-value">:if</span>).<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">on</span><span class="ruby-operator">:</span> <span class="ruby-value">:create</span>)
|
521
|
-
|
522
|
-
<span class="ruby-keyword">if</span> (<span class="ruby-identifier">if_proc</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:if</span>])
|
523
|
-
<span class="ruby-keyword">if</span> <span class="ruby-identifier">if_proc</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>)
|
524
|
-
<span class="ruby-identifier">if_proc</span> = <span class="ruby-operator">-</span><span class="ruby-operator">></span>{ <span class="ruby-identifier">send</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:if</span>]) }
|
525
|
-
<span class="ruby-keyword">end</span>
|
526
|
-
|
527
|
-
<span class="ruby-identifier">recorded_eligibility_var</span> = <span class="ruby-node">"@_was_eligible_for_#{attribute_name}_count"</span>
|
528
|
-
<span class="ruby-identifier">counted_class</span>.<span class="ruby-identifier">before_destroy</span> <span class="ruby-keyword">do</span>
|
529
|
-
<span class="ruby-identifier">instance_variable_set</span> <span class="ruby-identifier">recorded_eligibility_var</span>, <span class="ruby-operator">!</span><span class="ruby-operator">!</span><span class="ruby-identifier">instance_exec</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">if_proc</span>)
|
530
|
-
<span class="ruby-keyword">true</span>
|
531
|
-
<span class="ruby-keyword">end</span>
|
532
|
-
<span class="ruby-identifier">counted_class</span>.<span class="ruby-identifier">after_commit</span> <span class="ruby-identifier">on</span><span class="ruby-operator">:</span> <span class="ruby-value">:destroy</span> <span class="ruby-keyword">do</span>
|
533
|
-
<span class="ruby-keyword">if</span> <span class="ruby-identifier">instance_variable_get</span>(<span class="ruby-identifier">recorded_eligibility_var</span>)
|
534
|
-
<span class="ruby-identifier">send</span>(<span class="ruby-identifier">decrement_hook</span>)
|
535
|
-
<span class="ruby-keyword">end</span>
|
536
|
-
<span class="ruby-keyword">end</span>
|
537
|
-
|
538
|
-
<span class="ruby-identifier">counted_class</span>.<span class="ruby-identifier">after_commit</span> <span class="ruby-identifier">on</span><span class="ruby-operator">:</span> <span class="ruby-value">:update</span> <span class="ruby-keyword">do</span>
|
539
|
-
<span class="ruby-comment"># There is no before-hook which will reliably have access to the</span>
|
540
|
-
<span class="ruby-comment"># previous version of the object, so we need to simulate it.</span>
|
541
|
-
<span class="ruby-identifier">previous_values</span> = <span class="ruby-identifier">previous_changes</span>.<span class="ruby-identifier">each_with_object</span>({}) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span>(<span class="ruby-identifier">key</span>,<span class="ruby-identifier">vals</span>), <span class="ruby-identifier">memo</span><span class="ruby-operator">|</span>
|
542
|
-
<span class="ruby-identifier">memo</span>[<span class="ruby-identifier">key</span>] = <span class="ruby-identifier">vals</span>.<span class="ruby-identifier">first</span>
|
543
|
-
<span class="ruby-keyword">end</span>
|
544
|
-
|
545
|
-
<span class="ruby-identifier">old_version</span> = <span class="ruby-identifier">dup</span>
|
546
|
-
<span class="ruby-keyword">if</span> <span class="ruby-identifier">previous_values</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:permitted?</span>)
|
547
|
-
<span class="ruby-identifier">old_version</span>.<span class="ruby-identifier">assign_attributes</span> <span class="ruby-identifier">previous_values</span>, <span class="ruby-identifier">without_protection</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>
|
548
|
-
<span class="ruby-keyword">else</span>
|
549
|
-
<span class="ruby-identifier">old_version</span>.<span class="ruby-identifier">assign_attributes</span> <span class="ruby-identifier">previous_values</span>
|
550
|
-
<span class="ruby-keyword">end</span>
|
551
|
-
|
552
|
-
<span class="ruby-identifier">was</span> = <span class="ruby-operator">!</span><span class="ruby-operator">!</span><span class="ruby-identifier">old_version</span>.<span class="ruby-identifier">instance_exec</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">if_proc</span>)
|
553
|
-
<span class="ruby-identifier">is</span> = <span class="ruby-operator">!</span><span class="ruby-operator">!</span><span class="ruby-identifier">instance_exec</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">if_proc</span>)
|
554
|
-
<span class="ruby-keyword">if</span> <span class="ruby-identifier">was</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">is</span>
|
555
|
-
<span class="ruby-keyword">if</span> <span class="ruby-identifier">is</span>
|
556
|
-
<span class="ruby-identifier">send</span>(<span class="ruby-identifier">increment_hook</span>)
|
557
|
-
<span class="ruby-keyword">else</span>
|
558
|
-
<span class="ruby-identifier">send</span>(<span class="ruby-identifier">decrement_hook</span>)
|
559
|
-
<span class="ruby-keyword">end</span>
|
560
|
-
<span class="ruby-keyword">end</span>
|
561
|
-
<span class="ruby-keyword">end</span>
|
562
|
-
<span class="ruby-keyword">else</span>
|
563
|
-
<span class="ruby-identifier">counted_class</span>.<span class="ruby-identifier">after_commit</span> <span class="ruby-identifier">decrement_hook</span>, <span class="ruby-identifier">on</span><span class="ruby-operator">:</span> <span class="ruby-value">:destroy</span>
|
564
|
-
<span class="ruby-keyword">end</span>
|
565
|
-
<span class="ruby-keyword">end</span></pre>
|
566
|
-
</div>
|
567
|
-
|
568
|
-
</div>
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
</div>
|
574
|
-
|
575
|
-
|
576
|
-
<div id="method-i-add_scope_counting_hooks" class="method-detail ">
|
577
|
-
|
578
|
-
<div class="method-heading">
|
579
|
-
<span class="method-name">add_scope_counting_hooks</span><span
|
580
|
-
class="method-args">(attribute_name, options)</span>
|
581
|
-
|
582
|
-
<span class="method-click-advice">click to toggle source</span>
|
583
|
-
|
584
|
-
</div>
|
585
|
-
|
586
|
-
|
587
|
-
<div class="method-description">
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
<div class="method-source-code" id="add_scope_counting_hooks-source">
|
595
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 192</span>
|
596
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">add_scope_counting_hooks</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">options</span>)
|
597
|
-
<span class="ruby-identifier">version</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span> <span class="ruby-value">:version</span>, <span class="ruby-value">1</span>
|
598
|
-
<span class="ruby-identifier">key</span> = <span class="ruby-identifier">scope_count_key</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">version</span>)
|
599
|
-
|
600
|
-
<span class="ruby-identifier">add_counting_hooks</span>(
|
601
|
-
<span class="ruby-identifier">attribute_name</span>,
|
602
|
-
<span class="ruby-operator">-</span><span class="ruby-operator">></span> { <span class="ruby-identifier">key</span> },
|
603
|
-
<span class="ruby-keyword">self</span>,
|
604
|
-
<span class="ruby-identifier">options</span>
|
605
|
-
)
|
606
|
-
<span class="ruby-keyword">end</span></pre>
|
607
|
-
</div>
|
608
|
-
|
609
|
-
</div>
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
</div>
|
615
|
-
|
616
|
-
|
617
|
-
<div id="method-i-caches_count_of-21" class="method-detail ">
|
618
|
-
|
619
|
-
<div class="method-heading">
|
620
|
-
<span class="method-name">caches_count_of!</span><span
|
621
|
-
class="method-args">(attribute_name, options)</span>
|
622
|
-
|
623
|
-
<span class="method-click-advice">click to toggle source</span>
|
624
|
-
|
625
|
-
</div>
|
626
|
-
|
627
|
-
|
628
|
-
<div class="method-description">
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
<div class="method-source-code" id="caches_count_of-21-source">
|
636
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 113</span>
|
637
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">caches_count_of!</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">options</span>)
|
638
|
-
<span class="ruby-identifier">association_name</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span> <span class="ruby-value">:association</span>, <span class="ruby-identifier">attribute_name</span>
|
639
|
-
<span class="ruby-identifier">association</span> = <span class="ruby-identifier">reflect_on_association</span>(<span class="ruby-identifier">association_name</span>.<span class="ruby-identifier">to_sym</span>)
|
640
|
-
<span class="ruby-identifier">raise</span> <span class="ruby-node">"#{self} does not have an association named #{association_name}"</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">association</span>
|
641
|
-
|
642
|
-
<span class="ruby-identifier">define_association_count_attribute</span> <span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">association</span>, <span class="ruby-identifier">options</span>
|
643
|
-
<span class="ruby-identifier">add_association_counting_hooks</span> <span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">association</span>, <span class="ruby-identifier">options</span>
|
644
|
-
<span class="ruby-keyword">end</span></pre>
|
645
|
-
</div>
|
646
|
-
|
647
|
-
</div>
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
</div>
|
653
|
-
|
654
|
-
|
655
|
-
<div id="method-i-caches_count_where-21" class="method-detail ">
|
656
|
-
|
657
|
-
<div class="method-heading">
|
658
|
-
<span class="method-name">caches_count_where!</span><span
|
659
|
-
class="method-args">(attribute_name, options)</span>
|
660
|
-
|
661
|
-
<span class="method-click-advice">click to toggle source</span>
|
662
|
-
|
663
|
-
</div>
|
664
|
-
|
665
|
-
|
666
|
-
<div class="method-description">
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
<div class="method-source-code" id="caches_count_where-21-source">
|
674
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 104</span>
|
675
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">caches_count_where!</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">options</span>)
|
676
|
-
<span class="ruby-identifier">scope_name</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span> <span class="ruby-value">:scope</span>, <span class="ruby-identifier">attribute_name</span>
|
677
|
-
<span class="ruby-identifier">relation</span> = <span class="ruby-identifier">send</span>(<span class="ruby-identifier">scope_name</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">scope_name</span>)
|
678
|
-
<span class="ruby-identifier">raise</span> <span class="ruby-node">"#{self} does not have a scope named #{scope_name}"</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">relation</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Relation</span>)
|
679
|
-
|
680
|
-
<span class="ruby-identifier">define_scope_count_attribute</span> <span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">relation</span>, <span class="ruby-identifier">options</span>
|
681
|
-
<span class="ruby-identifier">add_scope_counting_hooks</span> <span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">options</span>
|
682
|
-
<span class="ruby-keyword">end</span></pre>
|
683
|
-
</div>
|
684
|
-
|
685
|
-
</div>
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
</div>
|
691
|
-
|
692
|
-
|
693
|
-
<div id="method-i-default_race_condition_fallback_proc" class="method-detail ">
|
694
|
-
|
695
|
-
<div class="method-heading">
|
696
|
-
<span class="method-name">default_race_condition_fallback_proc</span><span
|
697
|
-
class="method-args">(key, relation, options)</span>
|
698
|
-
|
699
|
-
<span class="method-click-advice">click to toggle source</span>
|
700
|
-
|
701
|
-
</div>
|
702
|
-
|
703
|
-
|
704
|
-
<div class="method-description">
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
<div class="method-source-code" id="default_race_condition_fallback_proc-source">
|
712
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 148</span>
|
713
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">default_race_condition_fallback_proc</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">relation</span>, <span class="ruby-identifier">options</span>)
|
714
|
-
<span class="ruby-identifier">fallback</span> = <span class="ruby-constant">Rails</span>.<span class="ruby-identifier">cache</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">key</span>)
|
715
|
-
<span class="ruby-identifier">fallback</span> = <span class="ruby-identifier">fallback</span>.<span class="ruby-identifier">value</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">fallback</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Cache</span><span class="ruby-operator">::</span><span class="ruby-constant">Entry</span>)
|
716
|
-
|
717
|
-
<span class="ruby-keyword">if</span> <span class="ruby-identifier">fallback</span>.<span class="ruby-identifier">nil?</span>
|
718
|
-
<span class="ruby-keyword">begin</span>
|
719
|
-
<span class="ruby-identifier">fallback</span> = <span class="ruby-identifier">relation</span>.<span class="ruby-identifier">count</span>
|
720
|
-
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">StatementInvalid</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
|
721
|
-
<span class="ruby-identifier">fallback</span> = <span class="ruby-value">0</span>
|
722
|
-
<span class="ruby-keyword">end</span>
|
723
|
-
|
724
|
-
<span class="ruby-constant">Rails</span>.<span class="ruby-identifier">cache</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">key</span>, <span class="ruby-identifier">fallback</span>, <span class="ruby-identifier">expires_in</span><span class="ruby-operator">:</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:expires_in</span>, <span class="ruby-value">1</span>.<span class="ruby-identifier">week</span>)
|
725
|
-
<span class="ruby-keyword">end</span>
|
726
|
-
|
727
|
-
<span class="ruby-operator">-</span><span class="ruby-operator">></span> { <span class="ruby-identifier">fallback</span> }
|
728
|
-
<span class="ruby-keyword">end</span></pre>
|
729
|
-
</div>
|
730
|
-
|
731
|
-
</div>
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
</div>
|
737
|
-
|
738
|
-
|
739
|
-
<div id="method-i-define_association_count_attribute" class="method-detail ">
|
740
|
-
|
741
|
-
<div class="method-heading">
|
742
|
-
<span class="method-name">define_association_count_attribute</span><span
|
743
|
-
class="method-args">(attribute_name, association, options)</span>
|
744
|
-
|
745
|
-
<span class="method-click-advice">click to toggle source</span>
|
746
|
-
|
747
|
-
</div>
|
748
|
-
|
749
|
-
|
750
|
-
<div class="method-description">
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
<div class="method-source-code" id="define_association_count_attribute-source">
|
758
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 165</span>
|
759
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">define_association_count_attribute</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">association</span>, <span class="ruby-identifier">options</span>)
|
760
|
-
<span class="ruby-identifier">options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">dup</span>
|
761
|
-
|
762
|
-
<span class="ruby-identifier">version</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span> <span class="ruby-value">:version</span>, <span class="ruby-value">1</span>
|
763
|
-
<span class="ruby-identifier">key_getter</span> = <span class="ruby-operator">-</span><span class="ruby-operator">></span> { <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">association_count_key</span>(<span class="ruby-identifier">id</span>, <span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">version</span>) }
|
764
|
-
<span class="ruby-identifier">relation_getter</span> = <span class="ruby-identifier">generate_association_relation_getter</span>(<span class="ruby-identifier">association</span>, <span class="ruby-identifier">options</span>)
|
765
|
-
|
766
|
-
[<span class="ruby-identifier">attribute_name</span>, <span class="ruby-operator">*</span><span class="ruby-constant">Array</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:alias</span>])].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">attr_name</span><span class="ruby-operator">|</span>
|
767
|
-
<span class="ruby-identifier">define_singleton_method</span> <span class="ruby-node">"#{attr_name}_count_key"</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">id</span><span class="ruby-operator">|</span>
|
768
|
-
<span class="ruby-identifier">association_count_key</span>(<span class="ruby-identifier">id</span>, <span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">version</span>)
|
769
|
-
<span class="ruby-keyword">end</span>
|
770
|
-
|
771
|
-
<span class="ruby-identifier">define_singleton_method</span> <span class="ruby-node">"#{attr_name}_count_for"</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">id</span><span class="ruby-operator">|</span>
|
772
|
-
<span class="ruby-identifier">new</span>({<span class="ruby-identifier">id</span><span class="ruby-operator">:</span> <span class="ruby-identifier">id</span>}, <span class="ruby-identifier">without_protection</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>).<span class="ruby-identifier">send</span>(<span class="ruby-node">"#{attr_name}_count"</span>)
|
773
|
-
<span class="ruby-keyword">end</span>
|
774
|
-
|
775
|
-
<span class="ruby-identifier">add_count_attribute_methods</span>(
|
776
|
-
<span class="ruby-identifier">attr_name</span>,
|
777
|
-
<span class="ruby-identifier">key_getter</span>,
|
778
|
-
<span class="ruby-identifier">relation_getter</span>,
|
779
|
-
<span class="ruby-value">:define_method</span>,
|
780
|
-
<span class="ruby-identifier">association</span>.<span class="ruby-identifier">klass</span>,
|
781
|
-
<span class="ruby-identifier">options</span>
|
782
|
-
)
|
783
|
-
<span class="ruby-keyword">end</span>
|
784
|
-
<span class="ruby-keyword">end</span></pre>
|
785
|
-
</div>
|
786
|
-
|
787
|
-
</div>
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
</div>
|
793
|
-
|
794
|
-
|
795
|
-
<div id="method-i-define_scope_count_attribute" class="method-detail ">
|
796
|
-
|
797
|
-
<div class="method-heading">
|
798
|
-
<span class="method-name">define_scope_count_attribute</span><span
|
799
|
-
class="method-args">(attribute_name, relation, options)</span>
|
800
|
-
|
801
|
-
<span class="method-click-advice">click to toggle source</span>
|
802
|
-
|
803
|
-
</div>
|
804
|
-
|
805
|
-
|
806
|
-
<div class="method-description">
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
<div class="method-source-code" id="define_scope_count_attribute-source">
|
814
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 122</span>
|
815
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">define_scope_count_attribute</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">relation</span>, <span class="ruby-identifier">options</span>)
|
816
|
-
<span class="ruby-identifier">options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">dup</span>
|
817
|
-
|
818
|
-
<span class="ruby-identifier">version</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span> <span class="ruby-value">:version</span>, <span class="ruby-value">1</span>
|
819
|
-
<span class="ruby-identifier">key</span> = <span class="ruby-identifier">scope_count_key</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">version</span>)
|
820
|
-
|
821
|
-
<span class="ruby-keyword">unless</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-value">:race_condition_fallback</span>)
|
822
|
-
<span class="ruby-identifier">options</span>[<span class="ruby-value">:race_condition_fallback</span>] = <span class="ruby-identifier">default_race_condition_fallback_proc</span>(
|
823
|
-
<span class="ruby-identifier">key</span>,
|
824
|
-
<span class="ruby-identifier">relation</span>,
|
825
|
-
<span class="ruby-identifier">options</span>
|
826
|
-
)
|
827
|
-
<span class="ruby-keyword">end</span>
|
828
|
-
|
829
|
-
[<span class="ruby-identifier">attribute_name</span>, <span class="ruby-operator">*</span><span class="ruby-constant">Array</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:alias</span>])].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">attr_name</span><span class="ruby-operator">|</span>
|
830
|
-
<span class="ruby-identifier">add_count_attribute_methods</span>(
|
831
|
-
<span class="ruby-identifier">attr_name</span>,
|
832
|
-
<span class="ruby-operator">-</span><span class="ruby-operator">></span> { <span class="ruby-identifier">key</span> },
|
833
|
-
<span class="ruby-operator">-</span><span class="ruby-operator">></span> { <span class="ruby-identifier">relation</span> },
|
834
|
-
<span class="ruby-value">:define_singleton_method</span>,
|
835
|
-
<span class="ruby-keyword">self</span>,
|
836
|
-
<span class="ruby-identifier">options</span>
|
837
|
-
)
|
838
|
-
<span class="ruby-keyword">end</span>
|
839
|
-
<span class="ruby-keyword">end</span></pre>
|
840
|
-
</div>
|
841
|
-
|
842
|
-
</div>
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
</div>
|
848
|
-
|
849
|
-
|
850
|
-
<div id="method-i-generate_association_counting_hook_key_getter" class="method-detail ">
|
851
|
-
|
852
|
-
<div class="method-heading">
|
853
|
-
<span class="method-name">generate_association_counting_hook_key_getter</span><span
|
854
|
-
class="method-args">(association, attribute_name, options)</span>
|
855
|
-
|
856
|
-
<span class="method-click-advice">click to toggle source</span>
|
857
|
-
|
858
|
-
</div>
|
859
|
-
|
860
|
-
|
861
|
-
<div class="method-description">
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
<div class="method-source-code" id="generate_association_counting_hook_key_getter-source">
|
869
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 345</span>
|
870
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">generate_association_counting_hook_key_getter</span>(<span class="ruby-identifier">association</span>, <span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">options</span>)
|
871
|
-
<span class="ruby-identifier">version</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span> <span class="ruby-value">:version</span>, <span class="ruby-value">1</span>
|
872
|
-
<span class="ruby-identifier">counting_class</span> = <span class="ruby-keyword">self</span>
|
873
|
-
|
874
|
-
<span class="ruby-keyword">if</span> <span class="ruby-identifier">association</span>.<span class="ruby-identifier">through_reflection</span>
|
875
|
-
<span class="ruby-identifier">method_chain</span> = <span class="ruby-identifier">association</span>.<span class="ruby-identifier">chain</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">association</span><span class="ruby-operator">|</span>
|
876
|
-
<span class="ruby-keyword">if</span> (<span class="ruby-identifier">source</span> = <span class="ruby-identifier">association</span>.<span class="ruby-identifier">source_reflection</span>)
|
877
|
-
<span class="ruby-identifier">raise</span> <span class="ruby-string">"Chained associations without `inverse_of` are not supported!"</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">inverse_of</span>
|
878
|
-
<span class="ruby-identifier">source</span>.<span class="ruby-identifier">inverse_of</span>.<span class="ruby-identifier">name</span>
|
879
|
-
<span class="ruby-keyword">else</span>
|
880
|
-
<span class="ruby-identifier">association</span>.<span class="ruby-identifier">foreign_key</span>
|
881
|
-
<span class="ruby-keyword">end</span>
|
882
|
-
<span class="ruby-keyword">end</span>
|
883
|
-
|
884
|
-
<span class="ruby-identifier">proc</span> <span class="ruby-keyword">do</span>
|
885
|
-
<span class="ruby-identifier">counter_id</span> = <span class="ruby-identifier">method_chain</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-keyword">self</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">memo</span>, <span class="ruby-identifier">method</span><span class="ruby-operator">|</span>
|
886
|
-
<span class="ruby-identifier">memo</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">method</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">memo</span>.<span class="ruby-identifier">nil?</span>
|
887
|
-
<span class="ruby-keyword">end</span>
|
888
|
-
<span class="ruby-identifier">counter_id</span> = <span class="ruby-identifier">counter_id</span>.<span class="ruby-identifier">id</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">counter_id</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>)
|
889
|
-
|
890
|
-
<span class="ruby-identifier">counting_class</span>.<span class="ruby-identifier">association_count_key</span> <span class="ruby-identifier">counter_id</span>, <span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">version</span>
|
891
|
-
<span class="ruby-keyword">end</span>
|
892
|
-
<span class="ruby-keyword">else</span>
|
893
|
-
<span class="ruby-identifier">foreign_key</span> = <span class="ruby-identifier">association</span>.<span class="ruby-identifier">foreign_key</span>
|
894
|
-
|
895
|
-
<span class="ruby-identifier">proc</span> <span class="ruby-keyword">do</span>
|
896
|
-
<span class="ruby-identifier">counting_class</span>.<span class="ruby-identifier">association_count_key</span> <span class="ruby-identifier">send</span>(<span class="ruby-identifier">foreign_key</span>), <span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">version</span>
|
897
|
-
<span class="ruby-keyword">end</span>
|
898
|
-
<span class="ruby-keyword">end</span>
|
899
|
-
<span class="ruby-keyword">end</span></pre>
|
900
|
-
</div>
|
901
|
-
|
902
|
-
</div>
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
</div>
|
908
|
-
|
909
|
-
|
910
|
-
<div id="method-i-generate_association_relation_getter" class="method-detail ">
|
911
|
-
|
912
|
-
<div class="method-heading">
|
913
|
-
<span class="method-name">generate_association_relation_getter</span><span
|
914
|
-
class="method-args">(association, options)</span>
|
915
|
-
|
916
|
-
<span class="method-click-advice">click to toggle source</span>
|
917
|
-
|
918
|
-
</div>
|
919
|
-
|
920
|
-
|
921
|
-
<div class="method-description">
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
<div class="method-source-code" id="generate_association_relation_getter-source">
|
929
|
-
<pre><span class="ruby-comment"># File lib/cached_counts.rb, line 376</span>
|
930
|
-
<span class="ruby-keyword">def</span> <span class="ruby-identifier">generate_association_relation_getter</span>(<span class="ruby-identifier">association</span>, <span class="ruby-identifier">options</span>)
|
931
|
-
<span class="ruby-identifier">counted_class</span> = <span class="ruby-identifier">association</span>.<span class="ruby-identifier">klass</span>
|
932
|
-
<span class="ruby-identifier">association_name</span> = <span class="ruby-identifier">association</span>.<span class="ruby-identifier">name</span>
|
933
|
-
<span class="ruby-keyword">if</span> (<span class="ruby-identifier">scope_proc</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:scope</span>])
|
934
|
-
<span class="ruby-operator">-</span><span class="ruby-operator">></span> { <span class="ruby-identifier">send</span>(<span class="ruby-identifier">association_name</span>).<span class="ruby-identifier">spawn</span>.<span class="ruby-identifier">scoping</span> { <span class="ruby-identifier">counted_class</span>.<span class="ruby-identifier">instance_exec</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">scope_proc</span>) } }
|
935
|
-
<span class="ruby-keyword">else</span>
|
936
|
-
<span class="ruby-operator">-</span><span class="ruby-operator">></span> { <span class="ruby-identifier">send</span>(<span class="ruby-identifier">association_name</span>).<span class="ruby-identifier">spawn</span> }
|
937
|
-
<span class="ruby-keyword">end</span>
|
938
|
-
<span class="ruby-keyword">end</span></pre>
|
939
|
-
</div>
|
940
|
-
|
941
|
-
</div>
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
</div>
|
947
|
-
|
948
|
-
|
949
|
-
</section>
|
950
|
-
|
951
|
-
</section>
|
952
|
-
</main>
|
953
|
-
|
954
|
-
|
955
|
-
<footer id="validator-badges" role="contentinfo">
|
956
|
-
<p><a href="http://validator.w3.org/check/referer">Validate</a>
|
957
|
-
<p>Generated by <a href="http://docs.seattlerb.org/rdoc/">RDoc</a> 4.2.0.
|
958
|
-
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
|
959
|
-
</footer>
|
960
|
-
|