custom_counter_cache 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/custom_counter_cache/model.rb +2 -1
- data/lib/custom_counter_cache/version.rb +1 -1
- data/test/counter_test.rb +31 -0
- data/test/test_helper.rb +35 -3
- metadata +6 -8
- data/test/controller_test.rb +0 -15
- data/test/helper_test.rb +0 -12
@@ -48,7 +48,8 @@ module CustomCounterCache::Model
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
# set callbacks
|
51
|
-
|
51
|
+
after_create method_name
|
52
|
+
after_update method_name, :if => options.delete(:if)
|
52
53
|
after_destroy method_name, :if => options.delete(:if)
|
53
54
|
end
|
54
55
|
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class CounterTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@user = User.create
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_default_counter_value
|
10
|
+
assert @user.published_count, 0
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_create_and_destroy_counter
|
14
|
+
@user.articles.create(:state => 'published')
|
15
|
+
assert Counter.count, 1
|
16
|
+
@user.articles.delete_all
|
17
|
+
assert Counter.count, 0
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_increment_and_decrement_counter_with_conditions
|
21
|
+
@article = @user.articles.create(:state => 'unpublished')
|
22
|
+
assert @user.published_count, 0
|
23
|
+
@article.update_attribute :state, 'published'
|
24
|
+
assert @user.published_count, 1
|
25
|
+
3.times { |i| @user.articles.create(:state => 'published') }
|
26
|
+
assert @user.published_count, 4
|
27
|
+
@user.articles.update_all "state = 'unpublished'"
|
28
|
+
assert @user.published_count, 0
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -3,9 +3,41 @@ $: << File.join(File.dirname(__FILE__))
|
|
3
3
|
|
4
4
|
require 'rubygems'
|
5
5
|
require 'test/unit'
|
6
|
-
require '
|
7
|
-
require 'action_controller'
|
8
|
-
require 'action_controller/test_case'
|
6
|
+
require 'sqlite3'
|
9
7
|
require 'action_view'
|
10
8
|
require 'active_record'
|
11
9
|
require 'custom_counter_cache'
|
10
|
+
require 'ruby-debug'
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define(:version => 1) do
|
15
|
+
create_table :users do |t|
|
16
|
+
end
|
17
|
+
create_table :articles do |t|
|
18
|
+
t.belongs_to :user
|
19
|
+
t.string :state, :default => 'unpublished'
|
20
|
+
end
|
21
|
+
create_table :counters do |t|
|
22
|
+
t.references :countable, :polymorphic => true
|
23
|
+
t.string :key, :null => false
|
24
|
+
t.integer :value, :null => false, :default => 0
|
25
|
+
end
|
26
|
+
add_index :counters, [ :countable_id, :countable_type, :key ], :unique => true
|
27
|
+
end
|
28
|
+
|
29
|
+
class User < ActiveRecord::Base
|
30
|
+
has_many :articles
|
31
|
+
define_counter_cache :published_count do |user|
|
32
|
+
user.articles.count(:conditions => { :articles => { :state => 'published' } })
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Article < ActiveRecord::Base
|
37
|
+
belongs_to :user
|
38
|
+
update_counter_cache :user, :published_count, :if => Proc.new { |article| article.state_changed? }
|
39
|
+
end
|
40
|
+
|
41
|
+
class Counter < ActiveRecord::Base
|
42
|
+
belongs_to :countable, :polymorphic => true
|
43
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_counter_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Cedric Howe
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-05-
|
18
|
+
date: 2011-05-05 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -45,8 +45,7 @@ files:
|
|
45
45
|
- lib/custom_counter_cache/model.rb
|
46
46
|
- lib/custom_counter_cache/version.rb
|
47
47
|
- lib/custom_counter_cache.rb
|
48
|
-
- test/
|
49
|
-
- test/helper_test.rb
|
48
|
+
- test/counter_test.rb
|
50
49
|
- test/test_helper.rb
|
51
50
|
has_rdoc: true
|
52
51
|
homepage: http://github.com/cedric/custom_counter_cache/
|
@@ -85,6 +84,5 @@ signing_key:
|
|
85
84
|
specification_version: 3
|
86
85
|
summary: Custom counter_cache functionality that supports conditions and multiple models.
|
87
86
|
test_files:
|
88
|
-
- test/
|
89
|
-
- test/helper_test.rb
|
87
|
+
- test/counter_test.rb
|
90
88
|
- test/test_helper.rb
|
data/test/controller_test.rb
DELETED