better_counters 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +21 -1
- data/lib/better_counters.rb +1 -2
- data/lib/better_counters/version.rb +1 -1
- metadata +9 -4
data/README.md
CHANGED
@@ -17,9 +17,29 @@ In your Gemfile add this line:
|
|
17
17
|
|
18
18
|
### Usage
|
19
19
|
|
20
|
+
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.
|
21
|
+
|
20
22
|
First you need to create a migration and add a counter column, like `tasks_count`.
|
21
23
|
|
22
|
-
rails g migration AddTasksCountToUsers
|
24
|
+
rails g migration AddTasksCountToUsers
|
25
|
+
|
26
|
+
Open up the generated migration file located at `/db/migrate/XXXXX.rb` and replace the content with something like:
|
27
|
+
|
28
|
+
def self.up
|
29
|
+
add_column :users, :tasks_count, :integer, :default => 0
|
30
|
+
|
31
|
+
# Fill in the new cache column with each user's tasks count
|
32
|
+
User.reset_column_information
|
33
|
+
User.find(:all).each do |p|
|
34
|
+
User.update_counters p.id, :tasks_count => p.tasks.count
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.down
|
39
|
+
remove_column :user, :tasks_count
|
40
|
+
end
|
41
|
+
|
42
|
+
Run `rake db:migrate`
|
23
43
|
|
24
44
|
Then simply on your model add the method `counter_cache` with a hash of models to keep the count column in.
|
25
45
|
|
data/lib/better_counters.rb
CHANGED
@@ -7,8 +7,7 @@ module BetterCounters
|
|
7
7
|
p.each do |c|
|
8
8
|
if c[1]
|
9
9
|
column = (c[1] == true) ? self.class.table_name + "_count" : c[1]
|
10
|
-
n > 0 ? send(c[0]).increment(column
|
11
|
-
send(c[0]).save
|
10
|
+
n > 0 ? send(c[0]).increment!(column) : send(c[0]).decrement!(column)
|
12
11
|
end
|
13
12
|
end
|
14
13
|
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.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-11-09 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,12 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
description: Improved alternative to rails built-in counter cache to work outside
|
26
31
|
belongs_to
|
27
32
|
email:
|
@@ -64,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
69
|
version: '0'
|
65
70
|
requirements: []
|
66
71
|
rubyforge_project:
|
67
|
-
rubygems_version: 1.8.
|
72
|
+
rubygems_version: 1.8.24
|
68
73
|
signing_key:
|
69
74
|
specification_version: 3
|
70
75
|
summary: Independent counter cache outside belongs_to
|