better_counters 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +20 -0
- data/README.md +4 -2
- data/lib/better_counters.rb +3 -3
- data/lib/better_counters/version.rb +1 -1
- metadata +4 -2
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Ryan Sacha
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -3,9 +3,9 @@
|
|
3
3
|
|
4
4
|
### Description
|
5
5
|
|
6
|
-
Counter cache is a great feature in Rails to improve
|
6
|
+
Counter cache is a great feature in Rails ActiveRecord to improve counting performance of a model's child associations by caching the number in a column.
|
7
7
|
|
8
|
-
Unfortunately it is tied to `belongs_to`, Many times i had to create similar custom methods from scratch just to work with `delegate` or simply
|
8
|
+
Unfortunately it is tied to `belongs_to`, Many times i had to create similar custom methods from scratch just to work with `delegate` or simply shortcut methods or models that aren't associated through `belongs_to`
|
9
9
|
|
10
10
|
Here comes Better Counters with an independent syntax.
|
11
11
|
|
@@ -15,6 +15,8 @@ In your Gemfile add this line:
|
|
15
15
|
|
16
16
|
gem 'better_counters'
|
17
17
|
|
18
|
+
Run `bundle install`
|
19
|
+
|
18
20
|
### Usage
|
19
21
|
|
20
22
|
In the following examples, we assume that we have a model `User` that has many `Project`s and many `Task`s through projects, and we'd like to cache the tasks count on the `User` model.
|
data/lib/better_counters.rb
CHANGED
@@ -3,7 +3,7 @@ require "better_counters/version"
|
|
3
3
|
module BetterCounters
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
|
-
def
|
6
|
+
def update_counter_cache(p = {}, n = 1)
|
7
7
|
p.each do |c|
|
8
8
|
if c[1]
|
9
9
|
column = (c[1] == true) ? self.class.table_name + "_count" : c[1]
|
@@ -27,8 +27,8 @@ module BetterCounters
|
|
27
27
|
# p: (Hash)
|
28
28
|
|
29
29
|
def counter_cache(p)
|
30
|
-
after_create Proc.new {
|
31
|
-
after_destroy Proc.new {
|
30
|
+
after_create Proc.new { update_counter_cache(p, 1) }
|
31
|
+
after_destroy Proc.new { update_counter_cache(p, -1) }
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: better_counters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -39,6 +39,8 @@ files:
|
|
39
39
|
LmdpdGlnbm9yZQ==
|
40
40
|
- !binary |-
|
41
41
|
R2VtZmlsZQ==
|
42
|
+
- !binary |-
|
43
|
+
TElDRU5TRS50eHQ=
|
42
44
|
- !binary |-
|
43
45
|
UkVBRE1FLm1k
|
44
46
|
- !binary |-
|