better_counters 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +69 -0
- data/Rakefile +1 -0
- data/better_counters.gemspec +20 -0
- data/lib/better_counters.rb +37 -0
- data/lib/better_counters/version.rb +3 -0
- metadata +71 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Better Counters
|
2
|
+
==============
|
3
|
+
|
4
|
+
### Description
|
5
|
+
|
6
|
+
Counter cache is a great feature in Rails to improve records counting performance by caching the number in a column.
|
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 models or shortcut methods that aren't associated through `belongs_to`
|
9
|
+
|
10
|
+
Here comes Better Counters with an independent syntax.
|
11
|
+
|
12
|
+
### Installation
|
13
|
+
|
14
|
+
In your Gemfile add this line:
|
15
|
+
|
16
|
+
gem 'better_counters'
|
17
|
+
|
18
|
+
### Usage
|
19
|
+
|
20
|
+
First you need to create a migration and add a counter column, like `tasks_count`.
|
21
|
+
|
22
|
+
rails g migration AddTasksCountToUsers tasks_count:integer
|
23
|
+
|
24
|
+
Then simply on your model add the method `counter_cache` with a hash of models to keep the count column in.
|
25
|
+
|
26
|
+
class Task < ActiveRecord::Base
|
27
|
+
belongs_to :project
|
28
|
+
counter_cache :user => true
|
29
|
+
|
30
|
+
def user
|
31
|
+
project.user
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Based on the example above, to fetch a user's total tasks count, run: `@user.tasks_count`.
|
36
|
+
You will no longer have to use the slow `@user.tasks.count`
|
37
|
+
|
38
|
+
You can use the counter cache in multiple models as long as they all have a counter column.
|
39
|
+
|
40
|
+
counter_cache :user => true, :project => true
|
41
|
+
|
42
|
+
### Custom counter column names
|
43
|
+
|
44
|
+
You can override the default column name with your own:
|
45
|
+
|
46
|
+
counter_cache :user => :total_tasks
|
47
|
+
|
48
|
+
* * *
|
49
|
+
|
50
|
+
![God of the Internet](http://i.imgur.com/ETKp5.jpg)
|
51
|
+
|
52
|
+
* * *
|
53
|
+
|
54
|
+
### Contributing
|
55
|
+
|
56
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
57
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
58
|
+
* Fork the project.
|
59
|
+
* Start a feature/bugfix branch.
|
60
|
+
* Commit and push until you are happy with your contribution.
|
61
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
62
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
63
|
+
|
64
|
+
**Copyright**
|
65
|
+
|
66
|
+
Copyright (c) 2012 Ryan Sacha. See LICENSE.txt for
|
67
|
+
further details.
|
68
|
+
|
69
|
+
If you happen to be bored, You can check out my [Portfolio](http://ryansacha.com).
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "better_counters/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "better_counters"
|
7
|
+
s.version = BetterCounters::VERSION
|
8
|
+
s.authors = ["Ryan Sacha"]
|
9
|
+
s.email = ["ryan@ryansacha.com"]
|
10
|
+
s.homepage = "https://github.com/RyanSacha/BetterCounters"
|
11
|
+
s.summary = %q{Independent counter cache outside belongs_to}
|
12
|
+
s.description = %q{Improved alternative to rails built-in counter cache to work outside belongs_to}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_runtime_dependency "activerecord"
|
20
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "better_counters/version"
|
2
|
+
|
3
|
+
module BetterCounters
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def update_cache_counters(p = {}, n = 1)
|
7
|
+
p.each do |c|
|
8
|
+
if c[1]
|
9
|
+
column = (c[1] == true) ? self.class.table_name + "_count" : c[1]
|
10
|
+
n > 0 ? send(c[0]).increment(column, 1) : send(c[0]).decrement(column, 1)
|
11
|
+
send(c[0]).save
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
# *Enable counter caching*
|
18
|
+
#
|
19
|
+
# Takes a +Hash+ where keys are the association singular name, and the values are either true or a custom counter column name.
|
20
|
+
#
|
21
|
+
# Example:
|
22
|
+
# >> counter_cache :user => true
|
23
|
+
#
|
24
|
+
# You can override the counter column name by replacing true with the name.
|
25
|
+
# >> counter_cache :user => :total_tasks
|
26
|
+
#
|
27
|
+
# Arguments
|
28
|
+
# p: (Hash)
|
29
|
+
|
30
|
+
def counter_cache(p)
|
31
|
+
after_create Proc.new { update_cache_counters(p, 1) }
|
32
|
+
after_destroy Proc.new { update_cache_counters(p, -1) }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
ActiveRecord::Base.send(:include, BetterCounters)
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: better_counters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryan Sacha
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &25656288 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *25656288
|
25
|
+
description: Improved alternative to rails built-in counter cache to work outside
|
26
|
+
belongs_to
|
27
|
+
email:
|
28
|
+
- ryan@ryansacha.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- !binary |-
|
34
|
+
LmdpdGlnbm9yZQ==
|
35
|
+
- !binary |-
|
36
|
+
R2VtZmlsZQ==
|
37
|
+
- !binary |-
|
38
|
+
UkVBRE1FLm1k
|
39
|
+
- !binary |-
|
40
|
+
UmFrZWZpbGU=
|
41
|
+
- !binary |-
|
42
|
+
YmV0dGVyX2NvdW50ZXJzLmdlbXNwZWM=
|
43
|
+
- !binary |-
|
44
|
+
bGliL2JldHRlcl9jb3VudGVycy5yYg==
|
45
|
+
- !binary |-
|
46
|
+
bGliL2JldHRlcl9jb3VudGVycy92ZXJzaW9uLnJi
|
47
|
+
homepage: https://github.com/RyanSacha/BetterCounters
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.16
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Independent counter cache outside belongs_to
|
71
|
+
test_files: []
|