mongoid_magic_counter_cache 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +2 -0
- data/Gemfile +3 -0
- data/lib/mongoid/magic-counter-cache/version.rb +1 -1
- data/lib/mongoid/magic_counter_cache.rb +46 -23
- data/lib/mongoid_magic_counter_cache/version.rb +1 -1
- data/mongoid_magic_counter_cache.gemspec +3 -2
- data/spec/models/article.rb +9 -0
- data/spec/models/comment.rb +11 -0
- data/spec/models/post.rb +9 -0
- data/spec/models/review.rb +10 -0
- data/spec/mongoid/magic_counter_cache_spec.rb +132 -1
- metadata +27 -32
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a8def97e3ca48082329bb91512ecb6165d46eb34
|
4
|
+
data.tar.gz: 557caba7f0327b1425724486bf80741d937567ef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7fce252e2c54419f01a9abd3273b460b2e99fe08263c3881f77ebf2d54ea84f49d75c96aa443ebb66fa2b8d84c1a07d1a8db59588c0f0aa082bf246b5a228675
|
7
|
+
data.tar.gz: 983912dd336634be68f01544502628f985555d2a6f760d9731156beff01d56f1f3cf87787a7b2677de94f86675e11d3d69a6b9fd116dbc722e713cb5def3d758
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'mongoid'
|
2
|
+
require 'mongoid/version'
|
2
3
|
module Mongoid #:nodoc:
|
3
|
-
|
4
4
|
# The Counter Cache will yada yada
|
5
5
|
#
|
6
6
|
# class Person
|
@@ -41,45 +41,68 @@ module Mongoid #:nodoc:
|
|
41
41
|
module MagicCounterCache
|
42
42
|
extend ActiveSupport::Concern
|
43
43
|
|
44
|
+
module LegacyCache
|
45
|
+
def actual_model_name
|
46
|
+
model_name
|
47
|
+
end
|
48
|
+
|
49
|
+
def increment_association(association, counter_name, inc)
|
50
|
+
association.inc(counter_name, inc)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
module ModernCache
|
55
|
+
def actual_model_name
|
56
|
+
model_name.name
|
57
|
+
end
|
58
|
+
|
59
|
+
def increment_association(association, counter_name, inc)
|
60
|
+
association.inc(counter_name => inc)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
44
64
|
module ClassMethods
|
65
|
+
include (Mongoid::VERSION.to_i >= 4) ? ModernCache : LegacyCache
|
45
66
|
|
46
67
|
def counter_cache(*args, &block)
|
47
|
-
options
|
48
|
-
name
|
68
|
+
options = args.extract_options!
|
69
|
+
name = options[:class] || args.first.to_s
|
70
|
+
counter_name = get_counter_name(options)
|
71
|
+
condition = options[:if]
|
49
72
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
counter_name = "#{model_name.demodulize.underscore}_count"
|
54
|
-
end
|
55
|
-
after_create do |doc|
|
73
|
+
callback_proc = ->(doc, inc) do
|
74
|
+
result = condition_result(condition, doc)
|
75
|
+
return unless result
|
56
76
|
if doc.embedded?
|
57
77
|
parent = doc._parent
|
58
|
-
|
59
|
-
|
60
|
-
relation = doc.send(name)
|
61
|
-
if relation && relation.class.fields.keys.include?(counter_name)
|
62
|
-
relation.inc(counter_name.to_sym, 1)
|
78
|
+
if parent.respond_to?(counter_name)
|
79
|
+
increment_association(parent, counter_name.to_sym, inc)
|
63
80
|
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
after_destroy do |doc|
|
68
|
-
if doc.embedded?
|
69
|
-
parent = doc._parent
|
70
|
-
parent.inc(counter_name.to_sym, -1) if parent.respond_to? counter_name
|
71
81
|
else
|
72
82
|
relation = doc.send(name)
|
73
83
|
if relation && relation.class.fields.keys.include?(counter_name)
|
74
|
-
relation
|
84
|
+
increment_association(relation, counter_name.to_sym, inc)
|
75
85
|
end
|
76
86
|
end
|
77
87
|
end
|
78
88
|
|
89
|
+
after_create( ->(doc) { callback_proc.call(doc, 1) })
|
90
|
+
after_destroy(->(doc) { callback_proc.call(doc, -1) })
|
91
|
+
|
79
92
|
end
|
80
93
|
|
81
94
|
alias :magic_counter_cache :counter_cache
|
82
|
-
end
|
83
95
|
|
96
|
+
private
|
97
|
+
|
98
|
+
def get_counter_name(options)
|
99
|
+
options.fetch(:field, "#{actual_model_name.demodulize.underscore}_count").to_s
|
100
|
+
end
|
101
|
+
|
102
|
+
def condition_result(condition, doc)
|
103
|
+
return true if condition.nil?
|
104
|
+
condition.call(doc)
|
105
|
+
end
|
106
|
+
end
|
84
107
|
end
|
85
108
|
end
|
@@ -10,8 +10,9 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.email = ["justin@justinherrick.com"]
|
11
11
|
s.homepage = "https://github.com/jah2488/mongoid-magic-counter-cache"
|
12
12
|
s.summary = %q{Setup Counter Caches in Mongoid Documents}
|
13
|
-
s.description = %q{A quick and easy way to add counter cache functionality to model
|
14
|
-
|
13
|
+
s.description = %q{A quick and easy way to add counter cache functionality to model - document associations in Mongoid}
|
14
|
+
s.license = 'MIT'
|
15
|
+
|
15
16
|
s.rubyforge_project = "mongoid_magic_counter_cache"
|
16
17
|
|
17
18
|
s.files = `git ls-files`.split("\n")
|
data/spec/models/post.rb
ADDED
@@ -6,7 +6,7 @@ module Mongoid
|
|
6
6
|
|
7
7
|
describe ".counter_cache" do
|
8
8
|
|
9
|
-
context "when the document is
|
9
|
+
context "when the document is associatedi without condition" do
|
10
10
|
|
11
11
|
before do
|
12
12
|
Library.delete_all
|
@@ -81,6 +81,7 @@ module Mongoid
|
|
81
81
|
book.foreign_publication_count.should == 1
|
82
82
|
end
|
83
83
|
|
84
|
+
|
84
85
|
context "when the referenced document has an embedded document" do
|
85
86
|
|
86
87
|
let(:page) do
|
@@ -119,6 +120,7 @@ module Mongoid
|
|
119
120
|
end
|
120
121
|
|
121
122
|
end
|
123
|
+
|
122
124
|
context "when the document is embedded" do
|
123
125
|
|
124
126
|
before do
|
@@ -205,6 +207,135 @@ module Mongoid
|
|
205
207
|
person.all_my_feels.should == person.feelings.size
|
206
208
|
end
|
207
209
|
end
|
210
|
+
|
211
|
+
context "when the document is associated with condition" do
|
212
|
+
|
213
|
+
before do
|
214
|
+
Post.delete_all
|
215
|
+
end
|
216
|
+
|
217
|
+
let(:post) do
|
218
|
+
Post.new
|
219
|
+
end
|
220
|
+
|
221
|
+
let(:comment) do
|
222
|
+
Comment.new(:is_published => true)
|
223
|
+
end
|
224
|
+
|
225
|
+
before do
|
226
|
+
post.save
|
227
|
+
post.comments.create(:remark => "I agree with you", :is_published => true)
|
228
|
+
end
|
229
|
+
|
230
|
+
it "sets the target of the relation" do
|
231
|
+
post.comments.first.remark.should == "I agree with you"
|
232
|
+
end
|
233
|
+
|
234
|
+
it "should have 1 comment for post" do
|
235
|
+
post.comments.size.should == 1
|
236
|
+
end
|
237
|
+
|
238
|
+
it "should have 1 in comment counter" do
|
239
|
+
post.comment_count.should == 1
|
240
|
+
end
|
241
|
+
|
242
|
+
it "sets the counter cache equal to the relation count on addition" do
|
243
|
+
5.times do |n|
|
244
|
+
post.comments << Comment.new(:is_published => true)
|
245
|
+
post.comment_count.should == post.comments.size
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
it "should increase counter when new books are added" do
|
250
|
+
post.comments.push( comment )
|
251
|
+
post.comments.size.should == 2
|
252
|
+
end
|
253
|
+
|
254
|
+
it "should increase counter when new books are added" do
|
255
|
+
post.comments.push( comment )
|
256
|
+
post.comments.size.should == post.comment_count
|
257
|
+
end
|
258
|
+
|
259
|
+
it "should decrease counter when published comment is deleted" do
|
260
|
+
post.comments.push( comment )
|
261
|
+
comment.destroy
|
262
|
+
post.comments.size.should == 1
|
263
|
+
end
|
264
|
+
|
265
|
+
it "should increase counter when new books are added" do
|
266
|
+
post.comments.push( comment )
|
267
|
+
comment.destroy
|
268
|
+
post.comments.size.should == post.comment_count
|
269
|
+
end
|
270
|
+
|
271
|
+
it "shouldnot increase counter when unpublished comment is added" do
|
272
|
+
post.comments << Comment.new
|
273
|
+
post.comments.size.should == post.comment_count + 1
|
274
|
+
end
|
275
|
+
|
276
|
+
it "shouldnot decrease counter when unpublished comment is deleted" do
|
277
|
+
post.comments << Comment.new(:remark => "2nd comment")
|
278
|
+
post.comments << Comment.new(:remark => "3rd comment", :is_published => true)
|
279
|
+
Comment.where(:remark == "2nd comment").first.destroy
|
280
|
+
post.comment_count.should == 2
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
context "when the document is embedded and has condition for counter" do
|
285
|
+
|
286
|
+
before do
|
287
|
+
Article.delete_all
|
288
|
+
end
|
289
|
+
|
290
|
+
let(:article) do
|
291
|
+
Article.new
|
292
|
+
end
|
293
|
+
|
294
|
+
let(:review) do
|
295
|
+
Review.new(:comment => "This is nice article")
|
296
|
+
end
|
297
|
+
|
298
|
+
before do
|
299
|
+
article.save
|
300
|
+
article.reviews.create(:comment => "This is very good article", :is_published => true)
|
301
|
+
end
|
302
|
+
|
303
|
+
it "should have 1 review in reviews" do
|
304
|
+
article.reviews.length.should == 1
|
305
|
+
end
|
306
|
+
|
307
|
+
it "should have correct comment" do
|
308
|
+
article.reviews.first.comment.should == "This is very good article"
|
309
|
+
end
|
310
|
+
|
311
|
+
it "should have 1 review in counter" do
|
312
|
+
article.review_count.should == 1
|
313
|
+
end
|
314
|
+
|
315
|
+
it "sets the counter cache equal to the relation count" do
|
316
|
+
article.reviews.length.should == article.review_count
|
317
|
+
end
|
318
|
+
|
319
|
+
it "sets the counter cache equal to the relation count on addition" do
|
320
|
+
5.times do |n|
|
321
|
+
article.reviews << Review.new(:is_published => true)
|
322
|
+
article.reviews.length.should == article.review_count
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
it "decreases the counter cache when records are deleted" do
|
327
|
+
article.reviews.all.destroy
|
328
|
+
article.reviews.length.should == article.review_count
|
329
|
+
end
|
330
|
+
|
331
|
+
it "counter should not get incremented if condition is not meet" do
|
332
|
+
5.times do |n|
|
333
|
+
article.reviews << Review.new
|
334
|
+
end
|
335
|
+
article.reviews.length.should == 6
|
336
|
+
article.review_count.should == 1
|
337
|
+
end
|
338
|
+
end
|
208
339
|
end
|
209
340
|
|
210
341
|
end
|
metadata
CHANGED
@@ -1,65 +1,58 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_magic_counter_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Justin Herrick
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-03-04 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: mongoid
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rspec
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - ">="
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ">="
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
|
-
description: A quick and easy way to add counter cache functionality to model
|
55
|
+
description: A quick and easy way to add counter cache functionality to model - document
|
63
56
|
associations in Mongoid
|
64
57
|
email:
|
65
58
|
- justin@justinherrick.com
|
@@ -67,9 +60,9 @@ executables: []
|
|
67
60
|
extensions: []
|
68
61
|
extra_rdoc_files: []
|
69
62
|
files:
|
70
|
-
- .gitignore
|
71
|
-
- .rspec
|
72
|
-
- .travis.yml
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
73
66
|
- CHANGELOG.md
|
74
67
|
- Gemfile
|
75
68
|
- LICENSE
|
@@ -81,53 +74,55 @@ files:
|
|
81
74
|
- lib/mongoid_magic_counter_cache/version.rb
|
82
75
|
- mongoid_magic_counter_cache.gemspec
|
83
76
|
- spec/models/album.rb
|
77
|
+
- spec/models/article.rb
|
84
78
|
- spec/models/book.rb
|
85
79
|
- spec/models/book/foreign_publication.rb
|
80
|
+
- spec/models/comment.rb
|
86
81
|
- spec/models/feeling.rb
|
87
82
|
- spec/models/library.rb
|
88
83
|
- spec/models/page.rb
|
89
84
|
- spec/models/person.rb
|
85
|
+
- spec/models/post.rb
|
86
|
+
- spec/models/review.rb
|
90
87
|
- spec/models/song.rb
|
91
88
|
- spec/mongoid/magic_counter_cache_spec.rb
|
92
89
|
- spec/spec_helper.rb
|
93
90
|
homepage: https://github.com/jah2488/mongoid-magic-counter-cache
|
94
|
-
licenses:
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
95
94
|
post_install_message:
|
96
95
|
rdoc_options: []
|
97
96
|
require_paths:
|
98
97
|
- lib
|
99
98
|
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
-
none: false
|
101
99
|
requirements:
|
102
|
-
- -
|
100
|
+
- - ">="
|
103
101
|
- !ruby/object:Gem::Version
|
104
102
|
version: '0'
|
105
|
-
segments:
|
106
|
-
- 0
|
107
|
-
hash: -2439585169310591993
|
108
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
-
none: false
|
110
104
|
requirements:
|
111
|
-
- -
|
105
|
+
- - ">="
|
112
106
|
- !ruby/object:Gem::Version
|
113
107
|
version: '0'
|
114
|
-
segments:
|
115
|
-
- 0
|
116
|
-
hash: -2439585169310591993
|
117
108
|
requirements: []
|
118
109
|
rubyforge_project: mongoid_magic_counter_cache
|
119
|
-
rubygems_version:
|
110
|
+
rubygems_version: 2.2.2
|
120
111
|
signing_key:
|
121
|
-
specification_version:
|
112
|
+
specification_version: 4
|
122
113
|
summary: Setup Counter Caches in Mongoid Documents
|
123
114
|
test_files:
|
124
115
|
- spec/models/album.rb
|
116
|
+
- spec/models/article.rb
|
125
117
|
- spec/models/book.rb
|
126
118
|
- spec/models/book/foreign_publication.rb
|
119
|
+
- spec/models/comment.rb
|
127
120
|
- spec/models/feeling.rb
|
128
121
|
- spec/models/library.rb
|
129
122
|
- spec/models/page.rb
|
130
123
|
- spec/models/person.rb
|
124
|
+
- spec/models/post.rb
|
125
|
+
- spec/models/review.rb
|
131
126
|
- spec/models/song.rb
|
132
127
|
- spec/mongoid/magic_counter_cache_spec.rb
|
133
128
|
- spec/spec_helper.rb
|