custom_counter_cache 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/custom_counter_cache.rb +2 -1
- data/lib/custom_counter_cache/model.rb +7 -11
- data/lib/custom_counter_cache/version.rb +1 -1
- data/test/counter_test.rb +30 -9
- data/test/test_helper.rb +33 -15
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 182f8ab75fb7fc29e78199d05904ac3a8b432b20
|
4
|
+
data.tar.gz: 7bae0ad737c88fd4bfc682e212c3c87de790ccb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 434dafe982b66628bf6562cb9cad6a873677fc599042b516a3d12a3e84b55352954c7d26c8989c433123fc23223bf53b4e9f5b65835d4f1cc6fbdf06861b0de3
|
7
|
+
data.tar.gz: 20192d909056d15648e7960d60d13de4c8ca2f6f33cb9ed171f35cfeaa11386ade1dc5433fcdcc416a18be45f0f35f2ac18952585033ecc16a9c9a622c06e6f0
|
data/lib/custom_counter_cache.rb
CHANGED
@@ -1,17 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
def self.included(base)
|
4
|
-
base.extend ActsAsMethods
|
5
|
-
end
|
1
|
+
require 'active_support/concern'
|
6
2
|
|
7
|
-
|
3
|
+
module CustomCounterCache::Model
|
4
|
+
extend ActiveSupport::Concern
|
8
5
|
|
6
|
+
module ClassMethods
|
9
7
|
def define_counter_cache(cache_column, &block)
|
10
8
|
return unless table_exists?
|
11
9
|
|
12
10
|
# counter accessors
|
13
11
|
unless column_names.include?(cache_column.to_s)
|
14
|
-
has_many :counters, :
|
12
|
+
has_many :counters, as: :countable, dependent: :destroy
|
15
13
|
define_method "#{cache_column}" do
|
16
14
|
# check if the counter is loaded
|
17
15
|
if counters.loaded? && counter = counters.detect{|c| c.key == cache_column.to_s }
|
@@ -24,7 +22,7 @@ module CustomCounterCache::Model
|
|
24
22
|
if ( counter = counters.find_by_key(cache_column.to_s) )
|
25
23
|
counter.update_attribute :value, count.to_i
|
26
24
|
else
|
27
|
-
counters.create :
|
25
|
+
counters.create key: cache_column.to_s, value: count.to_i
|
28
26
|
end
|
29
27
|
end
|
30
28
|
end
|
@@ -46,7 +44,7 @@ module CustomCounterCache::Model
|
|
46
44
|
cache_column = cache_column.to_sym
|
47
45
|
method_name = "callback_#{cache_column}".to_sym
|
48
46
|
reflection = reflect_on_association(association)
|
49
|
-
foreign_key = reflection.
|
47
|
+
foreign_key = reflection.try(:foreign_key) || reflection.association_foreign_key
|
50
48
|
|
51
49
|
# define callback
|
52
50
|
define_method method_name do
|
@@ -73,9 +71,7 @@ module CustomCounterCache::Model
|
|
73
71
|
after_update method_name, options
|
74
72
|
after_destroy method_name, options
|
75
73
|
end
|
76
|
-
|
77
74
|
end
|
78
|
-
|
79
75
|
end
|
80
76
|
|
81
77
|
ActiveRecord::Base.send :include, CustomCounterCache::Model
|
data/test/counter_test.rb
CHANGED
@@ -14,44 +14,65 @@ class CounterTest < MiniTest::Unit::TestCase
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def test_create_and_destroy_counter
|
17
|
-
@user.articles.create(:
|
17
|
+
@user.articles.create(state: 'published')
|
18
18
|
assert_equal 1, Counter.count
|
19
19
|
@user.destroy
|
20
20
|
assert_equal 0, Counter.count
|
21
21
|
end
|
22
22
|
|
23
|
+
def test_create_and_destroy_polymorphic_association_counter
|
24
|
+
@article = @user.articles.create(state: "published")
|
25
|
+
assert_equal 0, @article.comments.size
|
26
|
+
@comment = @article.comments.create(state: "published")
|
27
|
+
assert_equal 1, @article.comments.size
|
28
|
+
@article.destroy
|
29
|
+
assert_equal 0, @article.comments.size
|
30
|
+
end
|
31
|
+
|
23
32
|
def test_increment_and_decrement_counter_with_conditions
|
24
|
-
@article = @user.articles.create(:
|
33
|
+
@article = @user.articles.create(state: 'unpublished')
|
25
34
|
assert_equal 0, @user.published_count
|
26
35
|
@article.update_attribute :state, 'published'
|
27
36
|
assert_equal 1, @user.published_count
|
28
|
-
3.times { |i| @user.articles.create(:
|
37
|
+
3.times { |i| @user.articles.create(state: 'published') }
|
29
38
|
assert_equal 4, @user.published_count
|
30
|
-
@user.articles.each {|a| a.update_attributes(:
|
39
|
+
@user.articles.each {|a| a.update_attributes(state: 'unpublished') }
|
31
40
|
assert_equal 0, @user.published_count
|
32
41
|
end
|
33
42
|
|
43
|
+
def test_increment_and_decrement_polymorphic_counter_with_conditions
|
44
|
+
@article = @user.articles.create(state: "published")
|
45
|
+
@comment = @article.comments.create(state: "unpublished")
|
46
|
+
assert_equal 0, @article.comments_count
|
47
|
+
@comment.update_attribute :state, "published"
|
48
|
+
assert_equal 1, @article.comments_count
|
49
|
+
3.times { |i| @article.comments.create(state: "published") }
|
50
|
+
assert_equal 4, @article.comments_count
|
51
|
+
@article.comments.each { |c| c.update_attributes(state: "unpublished") }
|
52
|
+
assert_equal 0, @article.comments_count
|
53
|
+
end
|
54
|
+
|
34
55
|
def test_increment_and_decrement_counter_with_conditions_on_model_with_counter_column
|
35
|
-
@ball = @box.balls.create(:
|
56
|
+
@ball = @box.balls.create(color: 'red')
|
36
57
|
assert_equal 0, @box.reload.green_balls_count
|
37
58
|
@ball.update_attribute :color, 'green'
|
38
59
|
assert_equal 1, @box.reload.green_balls_count
|
39
|
-
3.times { |i| @box.balls.create(:
|
60
|
+
3.times { |i| @box.balls.create(color: 'green') }
|
40
61
|
assert_equal 4, @box.reload.green_balls_count
|
41
|
-
@box.balls.each {|b| b.update_attributes(:
|
62
|
+
@box.balls.each {|b| b.update_attributes(color: 'red') }
|
42
63
|
assert_equal 0, @box.reload.green_balls_count
|
43
64
|
end
|
44
65
|
|
45
66
|
# Test that an eager loaded
|
46
67
|
def test_eager_loading_with_no_counter
|
47
|
-
@article = @user.articles.create(:
|
68
|
+
@article = @user.articles.create(state: 'unpublished')
|
48
69
|
user = User.includes(:counters).first
|
49
70
|
assert_equal 0, user.published_count
|
50
71
|
|
51
72
|
end
|
52
73
|
|
53
74
|
def test_eager_loading_with_counter
|
54
|
-
@article = @user.articles.create(:
|
75
|
+
@article = @user.articles.create(state: 'published')
|
55
76
|
@user = User.includes(:counters).find(@user.id)
|
56
77
|
assert_equal 1, @user.published_count
|
57
78
|
end
|
data/test/test_helper.rb
CHANGED
@@ -5,45 +5,63 @@ require 'action_view'
|
|
5
5
|
require 'active_record'
|
6
6
|
require 'custom_counter_cache'
|
7
7
|
|
8
|
-
ActiveRecord::Base.establish_connection(:
|
8
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
9
9
|
|
10
|
-
ActiveRecord::Schema.define(:
|
10
|
+
ActiveRecord::Schema.define(version: 1) do
|
11
11
|
create_table :users do |t|
|
12
12
|
end
|
13
|
+
|
13
14
|
create_table :articles do |t|
|
14
15
|
t.belongs_to :user
|
15
|
-
t.string :state, :
|
16
|
+
t.string :state, default: 'unpublished'
|
16
17
|
end
|
18
|
+
|
19
|
+
create_table :comments do |t|
|
20
|
+
t.belongs_to :user
|
21
|
+
t.references :commentable, polymorphic: true
|
22
|
+
t.string :state, default: "unpublished"
|
23
|
+
end
|
24
|
+
|
17
25
|
create_table :counters do |t|
|
18
|
-
t.references :countable, :
|
19
|
-
t.string :key, :
|
20
|
-
t.integer :value, :
|
26
|
+
t.references :countable, polymorphic: true
|
27
|
+
t.string :key, null: false
|
28
|
+
t.integer :value, null: false, default: 0
|
21
29
|
end
|
22
|
-
add_index :counters, [ :countable_id, :countable_type, :key ], :
|
30
|
+
add_index :counters, [ :countable_id, :countable_type, :key ], unique: true
|
23
31
|
|
24
32
|
create_table :boxes do |t|
|
25
|
-
t.integer :green_balls_count, :
|
33
|
+
t.integer :green_balls_count, default: 0
|
26
34
|
end
|
35
|
+
|
27
36
|
create_table :balls do |t|
|
28
37
|
t.belongs_to :box
|
29
|
-
t.string :color, :
|
38
|
+
t.string :color, default: 'red'
|
30
39
|
end
|
31
40
|
end
|
32
41
|
|
33
42
|
class User < ActiveRecord::Base
|
34
|
-
has_many :articles
|
43
|
+
has_many :articles, dependent: :destroy
|
35
44
|
define_counter_cache :published_count do |user|
|
36
|
-
user.articles.where(:
|
45
|
+
user.articles.where(articles: { state: 'published' }).count
|
37
46
|
end
|
38
47
|
end
|
39
48
|
|
40
49
|
class Article < ActiveRecord::Base
|
41
50
|
belongs_to :user
|
42
|
-
update_counter_cache :user, :published_count, :
|
51
|
+
update_counter_cache :user, :published_count, if: Proc.new { |article| article.state_changed? }
|
52
|
+
has_many :comments, as: :commentable, dependent: :destroy
|
53
|
+
define_counter_cache :comments_count do |article|
|
54
|
+
article.comments.where(state: "published").count
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class Comment < ActiveRecord::Base
|
59
|
+
belongs_to :commentable, polymorphic: true
|
60
|
+
update_counter_cache :commentable, :comments_count, if: Proc.new { |comment| comment.state_changed? }
|
43
61
|
end
|
44
62
|
|
45
63
|
class Counter < ActiveRecord::Base
|
46
|
-
belongs_to :countable, :
|
64
|
+
belongs_to :countable, polymorphic: true
|
47
65
|
end
|
48
66
|
|
49
67
|
class Box < ActiveRecord::Base
|
@@ -55,6 +73,6 @@ end
|
|
55
73
|
|
56
74
|
class Ball < ActiveRecord::Base
|
57
75
|
belongs_to :box
|
58
|
-
scope :green, lambda { where(:
|
59
|
-
update_counter_cache :box, :green_balls_count, :
|
76
|
+
scope :green, lambda { where(color: 'green') }
|
77
|
+
update_counter_cache :box, :green_balls_count, if: Proc.new { |ball| ball.color_changed? }
|
60
78
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_counter_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cedric Howe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06
|
11
|
+
date: 2015-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|