custom_counter_cache 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/custom_counter_cache/model.rb +10 -1
- data/lib/custom_counter_cache/version.rb +1 -1
- data/test/counter_test.rb +13 -0
- data/test/test_helper.rb +21 -0
- metadata +13 -21
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6e5fc4d939f22ea47d554fccc9fc4a1d38bbfbd8
|
4
|
+
data.tar.gz: 320ea9e7b270ae977f1baf59cb35a7a6a1e4f470
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b7b67a4159f8266cd5a9dc6b46baebd5ac4cc06821fa93100dfc968d6cd52ff3020c93df3797a730ed08fe16866f21d4cc301d0eaaf9dc49b9f1594a173ea642
|
7
|
+
data.tar.gz: 7906c1b7ed13b8c43870a1066279c3434e86c2104a761838ad74b0f97c592b347ef3eb236611e7c0adac2715a6f4ef3769dd3d3f203b44c8f1a9316a7792a181
|
@@ -8,6 +8,7 @@ module CustomCounterCache::Model
|
|
8
8
|
|
9
9
|
def define_counter_cache(cache_column, &block)
|
10
10
|
return unless table_exists?
|
11
|
+
|
11
12
|
# counter accessors
|
12
13
|
unless column_names.include?(cache_column.to_s)
|
13
14
|
has_many :counters, :as => :countable, :dependent => :destroy
|
@@ -27,19 +28,26 @@ module CustomCounterCache::Model
|
|
27
28
|
end
|
28
29
|
end
|
29
30
|
end
|
31
|
+
|
30
32
|
# counter update method
|
31
33
|
define_method "update_#{cache_column}" do
|
32
|
-
|
34
|
+
if self.class.column_names.include?(cache_column.to_s)
|
35
|
+
update_attribute cache_column, block.call(self)
|
36
|
+
else
|
37
|
+
send "#{cache_column}=", block.call(self)
|
38
|
+
end
|
33
39
|
end
|
34
40
|
end
|
35
41
|
|
36
42
|
def update_counter_cache(association, cache_column, options = {})
|
37
43
|
return unless table_exists?
|
44
|
+
|
38
45
|
association = association.to_sym
|
39
46
|
cache_column = cache_column.to_sym
|
40
47
|
method_name = "callback_#{cache_column}".to_sym
|
41
48
|
reflection = reflect_on_association(association)
|
42
49
|
foreign_key = reflection.options[:foreign_key] || reflection.association_foreign_key
|
50
|
+
|
43
51
|
# define callback
|
44
52
|
define_method method_name do
|
45
53
|
# update old association
|
@@ -59,6 +67,7 @@ module CustomCounterCache::Model
|
|
59
67
|
record.send("update_#{cache_column}")
|
60
68
|
end
|
61
69
|
end
|
70
|
+
|
62
71
|
# set callbacks
|
63
72
|
after_create method_name, options
|
64
73
|
after_update method_name, options
|
data/test/counter_test.rb
CHANGED
@@ -4,10 +4,12 @@ class CounterTest < Test::Unit::TestCase
|
|
4
4
|
|
5
5
|
def setup
|
6
6
|
@user = User.create
|
7
|
+
@box = Box.create
|
7
8
|
end
|
8
9
|
|
9
10
|
def test_default_counter_value
|
10
11
|
assert_equal 0, @user.published_count
|
12
|
+
assert_equal 0, @box.green_balls_count
|
11
13
|
end
|
12
14
|
|
13
15
|
def test_create_and_destroy_counter
|
@@ -28,6 +30,17 @@ class CounterTest < Test::Unit::TestCase
|
|
28
30
|
assert_equal 0, @user.published_count
|
29
31
|
end
|
30
32
|
|
33
|
+
def test_increment_and_decrement_counter_with_conditions_on_model_with_counter_column
|
34
|
+
@ball = @box.balls.create(:color => 'red')
|
35
|
+
assert_equal 0, @box.reload.green_balls_count
|
36
|
+
@ball.update_attribute :color, 'green'
|
37
|
+
assert_equal 1, @box.reload.green_balls_count
|
38
|
+
3.times { |i| @box.balls.create(:color => 'green') }
|
39
|
+
assert_equal 4, @box.reload.green_balls_count
|
40
|
+
@box.balls.each {|b| b.update_attributes(:color => 'red') }
|
41
|
+
assert_equal 0, @box.reload.green_balls_count
|
42
|
+
end
|
43
|
+
|
31
44
|
# Test that an eager loaded
|
32
45
|
def test_eager_loading_with_no_counter
|
33
46
|
@article = @user.articles.create(:state => 'unpublished')
|
data/test/test_helper.rb
CHANGED
@@ -20,6 +20,14 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
20
20
|
t.integer :value, :null => false, :default => 0
|
21
21
|
end
|
22
22
|
add_index :counters, [ :countable_id, :countable_type, :key ], :unique => true
|
23
|
+
|
24
|
+
create_table :boxes do |t|
|
25
|
+
t.integer :green_balls_count, :default => 0
|
26
|
+
end
|
27
|
+
create_table :balls do |t|
|
28
|
+
t.belongs_to :box
|
29
|
+
t.string :color, :default => 'red'
|
30
|
+
end
|
23
31
|
end
|
24
32
|
|
25
33
|
class User < ActiveRecord::Base
|
@@ -37,3 +45,16 @@ end
|
|
37
45
|
class Counter < ActiveRecord::Base
|
38
46
|
belongs_to :countable, :polymorphic => true
|
39
47
|
end
|
48
|
+
|
49
|
+
class Box < ActiveRecord::Base
|
50
|
+
has_many :balls
|
51
|
+
define_counter_cache :green_balls_count do |box|
|
52
|
+
box.balls.green.count
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Ball < ActiveRecord::Base
|
57
|
+
belongs_to :box
|
58
|
+
scope :green, lambda { where(:color => 'green') }
|
59
|
+
update_counter_cache :box, :green_balls_count, :if => Proc.new { |ball| ball.color_changed? }
|
60
|
+
end
|
metadata
CHANGED
@@ -1,46 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_counter_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Cedric Howe
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-04-05 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rails
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '3.1'
|
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: '3.1'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: sqlite3
|
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: :development
|
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
|
description: ''
|
@@ -55,31 +50,28 @@ files:
|
|
55
50
|
- test/counter_test.rb
|
56
51
|
- test/test_helper.rb
|
57
52
|
homepage: http://github.com/cedric/custom_counter_cache/
|
58
|
-
licenses:
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata: {}
|
59
56
|
post_install_message:
|
60
57
|
rdoc_options: []
|
61
58
|
require_paths:
|
62
59
|
- lib
|
63
60
|
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
-
none: false
|
65
61
|
requirements:
|
66
|
-
- -
|
62
|
+
- - '>='
|
67
63
|
- !ruby/object:Gem::Version
|
68
64
|
version: '0'
|
69
|
-
segments:
|
70
|
-
- 0
|
71
|
-
hash: 603101207245387797
|
72
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
66
|
requirements:
|
75
|
-
- -
|
67
|
+
- - '>='
|
76
68
|
- !ruby/object:Gem::Version
|
77
69
|
version: 1.3.6
|
78
70
|
requirements: []
|
79
71
|
rubyforge_project: custom_counter_cache
|
80
|
-
rubygems_version:
|
72
|
+
rubygems_version: 2.0.3
|
81
73
|
signing_key:
|
82
|
-
specification_version:
|
74
|
+
specification_version: 4
|
83
75
|
summary: Custom counter_cache functionality that supports conditions and multiple
|
84
76
|
models.
|
85
77
|
test_files:
|