custom_counter_cache 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -48,7 +48,8 @@ module CustomCounterCache::Model
48
48
  end
49
49
  end
50
50
  # set callbacks
51
- after_save method_name, :if => options.delete(:if)
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
 
@@ -1,5 +1,5 @@
1
1
  module CustomCounterCache
2
2
 
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
 
5
5
  end
@@ -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 'active_support'
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: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
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-04 00:00:00 -04:00
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/controller_test.rb
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/controller_test.rb
89
- - test/helper_test.rb
87
+ - test/counter_test.rb
90
88
  - test/test_helper.rb
@@ -1,15 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'test_helper')
2
-
3
- class ControllerTest < Test::Unit::TestCase
4
-
5
- def setup
6
- end
7
-
8
- def teardown
9
- end
10
-
11
- def test_foobar
12
- assert true
13
- end
14
-
15
- end
data/test/helper_test.rb DELETED
@@ -1,12 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'test_helper')
2
-
3
- class HelperTest < ActionView::TestCase
4
-
5
- def setup
6
- end
7
-
8
- def test_foobar
9
- assert true
10
- end
11
-
12
- end