mongoid_countercache 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +92 -0
- data/Rakefile +6 -0
- data/lib/mongoid_countercache.rb +2 -0
- data/lib/mongoid_countercache/counter_cache.rb +48 -0
- data/lib/mongoid_countercache/version.rb +3 -0
- data/mongoid_countercache.gemspec +27 -0
- data/spec/models/comment.rb +18 -0
- data/spec/models/counter_cache_spec.rb +81 -0
- data/spec/models/like.rb +10 -0
- data/spec/models/post.rb +15 -0
- data/spec/spec_helper.rb +17 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 570f444cf6b28f85bf016728681ef70052a65d28
|
4
|
+
data.tar.gz: 4f5e4dd1bcdb176df1caa02b6b36b3df6870b162
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 96acfd0997a317342409365038709ac04e4d0973eb5a53ccec44eb9b42ffde6cd4a520dc0d7f7edfdf10b18bcfb84bc16dbf3ea7ae7aff2d0118067e6579b3f2
|
7
|
+
data.tar.gz: e647b87ec1ab59ec7450e2d6a1129e432a77a09647ee634dec7c8ee65cf4058ddd9d29595ee3e80b2f439f7a739f078471e18a78d7e5deb472f80cabc2d26406
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Intrepidd
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# MongoidCounterCache
|
2
|
+
|
3
|
+
This gem is used to maintain a cached counter of an object's children, this avoids N + 1 queries issues when doing a ``count`` in a loop.
|
4
|
+
|
5
|
+
This is basically an implementation of ActiveRecord counter cache for Mongoid.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'mongoid_countercache'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mongoid_countercache
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Include ``Mongoid::CounterCache`` into your embedded or referenced model :
|
24
|
+
|
25
|
+
class Comment
|
26
|
+
include Mongoid::Document
|
27
|
+
include Mongoid::CounterCache
|
28
|
+
|
29
|
+
belongs_to :post
|
30
|
+
|
31
|
+
counter_cache :post
|
32
|
+
end
|
33
|
+
|
34
|
+
Heads up ! Be sure to declare the field in the parent model, due to dependency restrictions, it can't be added programatically without having the children model to be required first
|
35
|
+
|
36
|
+
class Post
|
37
|
+
has_many :comments
|
38
|
+
|
39
|
+
field :comment_count, :type => Integer, :default => 0
|
40
|
+
end
|
41
|
+
|
42
|
+
Use the count method in the parent model :
|
43
|
+
|
44
|
+
Post.first.comment_count
|
45
|
+
|
46
|
+
You can rename the field used for storing the counter
|
47
|
+
|
48
|
+
counter_cache :post, :field_name => 'comment_counter'
|
49
|
+
|
50
|
+
Then :
|
51
|
+
|
52
|
+
Post.first.comment_counter
|
53
|
+
|
54
|
+
## Variants
|
55
|
+
|
56
|
+
Sometimes you want to keep a count but only for a subset of your document.
|
57
|
+
|
58
|
+
Fortunately, mongoid_counter_cache allows to keep alternative counters :
|
59
|
+
|
60
|
+
Don't forget to add in the Post class :
|
61
|
+
|
62
|
+
field :comment_count_positive, :type => Integer, :type => 0
|
63
|
+
field :comment_count_negative, :type => Integer, :type => 0
|
64
|
+
|
65
|
+
class Comment
|
66
|
+
include Mongoid::Document
|
67
|
+
include Mongoid::CounterCache
|
68
|
+
|
69
|
+
field :mark, :type => Integer
|
70
|
+
|
71
|
+
belongs_to :post
|
72
|
+
|
73
|
+
counter_cache :post, :variants => {
|
74
|
+
:positive => lambda { mark >= 8 },
|
75
|
+
:negative => lambda { mark <= 2 }
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
You just have to suffix the count method to get the number of positive comments here :
|
80
|
+
|
81
|
+
Post.first.comment_count_positive
|
82
|
+
Post.first.comment_count_negative
|
83
|
+
|
84
|
+
It's even magic ! If you update the comment so it doesn't belongs to the same counter, the counters will be updated accordingly.
|
85
|
+
|
86
|
+
## Contributing
|
87
|
+
|
88
|
+
1. Fork it
|
89
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
90
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
91
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
92
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module CounterCache
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.send(:extend, ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# Defines a counter cache on the given relation
|
10
|
+
#
|
11
|
+
# @param [Class] relation_name The name of the parent relation on which to create the cache
|
12
|
+
# @param [Hash] options The options hash
|
13
|
+
# @option options [String, Symbol] :field_name The name of the field in the parent document
|
14
|
+
# @option options [Hash] :variants A hash with a variant name in key and a lambda / proc in value
|
15
|
+
def counter_cache(relation_name, options = {})
|
16
|
+
field_name = (options[:field_name] || "#{self.to_s.demodulize.underscore}_count").to_s
|
17
|
+
|
18
|
+
options[:variants].to_a.each do |key,proc|
|
19
|
+
variant_name = "#{field_name}_#{key.to_s.strip}"
|
20
|
+
|
21
|
+
after_create { update_parent_counter(self.send(relation_name), variant_name, 1, proc) }
|
22
|
+
after_destroy { update_parent_counter(self.send(relation_name), variant_name, -1, proc) }
|
23
|
+
after_update { update_parent_counter(self.send(relation_name), variant_name, 1, proc) }
|
24
|
+
|
25
|
+
before_update do
|
26
|
+
attributes = self.attributes.dup
|
27
|
+
self.changes.each do |change, vals|
|
28
|
+
self.attributes[change] = vals.first
|
29
|
+
end
|
30
|
+
update_parent_counter(self.send(relation_name), variant_name, -1, proc)
|
31
|
+
self.attributes = attributes
|
32
|
+
end
|
33
|
+
end
|
34
|
+
after_create { update_parent_counter(self.send(relation_name), field_name, 1) }
|
35
|
+
after_destroy { update_parent_counter(self.send(relation_name), field_name, -1) }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def update_parent_counter(parent, field, inc, proc = nil)
|
42
|
+
return unless parent && parent[field]
|
43
|
+
parent[field] += inc if proc.nil? || proc.bind(self).call
|
44
|
+
parent.save
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid_countercache/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongoid_countercache"
|
8
|
+
spec.version = MongoidCounterCache::VERSION
|
9
|
+
spec.authors = ["Adrien Siami"]
|
10
|
+
spec.email = ["adrien@siami.fr"]
|
11
|
+
spec.description = "A simple counter cache implemented for mongoid, with neat additions such as lambda evaluation"
|
12
|
+
spec.summary = "A simple counter cache implemented for mongoid, with neat additions such as lambda evaluation"
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'mongoid', '>= 3.0.0', '<= 4.0.0'
|
22
|
+
spec.add_dependency("rake")
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
require 'mongoid_countercache'
|
3
|
+
|
4
|
+
class Comment
|
5
|
+
include Mongoid::Document
|
6
|
+
include Mongoid::CounterCache
|
7
|
+
|
8
|
+
belongs_to :post
|
9
|
+
|
10
|
+
field :mark, :type => Integer, :default => 5
|
11
|
+
|
12
|
+
counter_cache :post, :variants => {
|
13
|
+
:positive => lambda { mark > 8},
|
14
|
+
:negative => lambda { mark < 3}
|
15
|
+
}
|
16
|
+
|
17
|
+
counter_cache :post, :field_name => 'custom_field_name'
|
18
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'models/post'
|
3
|
+
require 'models/comment'
|
4
|
+
require 'models/like'
|
5
|
+
|
6
|
+
describe 'CounterCache' do
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
@post = Post.new
|
10
|
+
@post.save
|
11
|
+
end
|
12
|
+
|
13
|
+
after(:each) do
|
14
|
+
Post.destroy_all
|
15
|
+
Comment.destroy_all
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'No variants' do
|
19
|
+
it 'Updates the counter' do
|
20
|
+
@post.comments.create
|
21
|
+
@post.comment_count.should == 1
|
22
|
+
@post.custom_field_name.should == 1
|
23
|
+
|
24
|
+
@post.comments.create
|
25
|
+
@post.comment_count.should == 2
|
26
|
+
@post.custom_field_name.should == 2
|
27
|
+
|
28
|
+
@post.comments.last.destroy
|
29
|
+
@post.comment_count.should == 1
|
30
|
+
@post.custom_field_name.should == 1
|
31
|
+
|
32
|
+
@post.comments.last.destroy
|
33
|
+
@post.comment_count.should == 0
|
34
|
+
@post.custom_field_name.should == 0
|
35
|
+
|
36
|
+
@post.likes.create
|
37
|
+
@post.likes.create
|
38
|
+
@post.like_count.should == 2
|
39
|
+
@post.likes.last.destroy
|
40
|
+
@post.like_count.should == 1
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'Variants' do
|
45
|
+
it 'Updates the counter' do
|
46
|
+
@post.comments.create(:mark => 8)
|
47
|
+
@post.comment_count.should == 1
|
48
|
+
@post.comment_count_positive == 1
|
49
|
+
@post.comment_count_negative == 0
|
50
|
+
|
51
|
+
@post.comments.create(:mark => 9)
|
52
|
+
@post.comment_count.should == 2
|
53
|
+
@post.comment_count_positive == 2
|
54
|
+
@post.comment_count_negative == 0
|
55
|
+
|
56
|
+
@post.comments.create(:mark => 1)
|
57
|
+
@post.comment_count.should == 3
|
58
|
+
@post.comment_count_positive == 2
|
59
|
+
@post.comment_count_negative == 1
|
60
|
+
|
61
|
+
@post.comments.last.destroy
|
62
|
+
@post.comment_count.should == 2
|
63
|
+
@post.comment_count_positive == 2
|
64
|
+
@post.comment_count_negative == 0
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'Updates and variants' do
|
69
|
+
it 'Updates the cache after an edit' do
|
70
|
+
comment = @post.comments.create(:mark => 9)
|
71
|
+
@post.comment_count_positive.should == 1
|
72
|
+
@post.comment_count.should == 1
|
73
|
+
comment.mark = 2
|
74
|
+
comment.save
|
75
|
+
@post.reload
|
76
|
+
@post.comment_count_positive.should == 0
|
77
|
+
@post.comment_count_negative.should == 1
|
78
|
+
@post.comment_count.should == 1
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/spec/models/like.rb
ADDED
data/spec/models/post.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
|
3
|
+
class Post
|
4
|
+
include Mongoid::Document
|
5
|
+
|
6
|
+
field :comment_count, :type => Integer, :default => 0
|
7
|
+
field :comment_count_positive, :type => Integer, :default => 0
|
8
|
+
field :comment_count_negative, :type => Integer, :default => 0
|
9
|
+
field :like_count, :type => Integer, :default => 0
|
10
|
+
field :custom_field_name, :type => Integer, :default => 0
|
11
|
+
|
12
|
+
|
13
|
+
has_many :comments
|
14
|
+
embeds_many :likes
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'mongoid'
|
4
|
+
|
5
|
+
|
6
|
+
Mongoid.configure do |config|
|
7
|
+
config.connect_to("mongoid_counter_cache")
|
8
|
+
end
|
9
|
+
|
10
|
+
RSpec.configure do |c|
|
11
|
+
c.mock_with :rspec
|
12
|
+
|
13
|
+
c.after(:all) do
|
14
|
+
Mongoid.purge!
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_countercache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adrien Siami
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
- - <=
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 4.0.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- - <=
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 4.0.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.3'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.3'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
description: A simple counter cache implemented for mongoid, with neat additions such
|
90
|
+
as lambda evaluation
|
91
|
+
email:
|
92
|
+
- adrien@siami.fr
|
93
|
+
executables: []
|
94
|
+
extensions: []
|
95
|
+
extra_rdoc_files: []
|
96
|
+
files:
|
97
|
+
- .gitignore
|
98
|
+
- Gemfile
|
99
|
+
- LICENSE.txt
|
100
|
+
- README.md
|
101
|
+
- Rakefile
|
102
|
+
- lib/mongoid_countercache.rb
|
103
|
+
- lib/mongoid_countercache/counter_cache.rb
|
104
|
+
- lib/mongoid_countercache/version.rb
|
105
|
+
- mongoid_countercache.gemspec
|
106
|
+
- spec/models/comment.rb
|
107
|
+
- spec/models/counter_cache_spec.rb
|
108
|
+
- spec/models/like.rb
|
109
|
+
- spec/models/post.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
homepage: ''
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.0.0
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: A simple counter cache implemented for mongoid, with neat additions such
|
135
|
+
as lambda evaluation
|
136
|
+
test_files:
|
137
|
+
- spec/models/comment.rb
|
138
|
+
- spec/models/counter_cache_spec.rb
|
139
|
+
- spec/models/like.rb
|
140
|
+
- spec/models/post.rb
|
141
|
+
- spec/spec_helper.rb
|