custom_counter_cache 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/custom_counter_cache/model.rb +6 -1
- data/lib/custom_counter_cache/version.rb +1 -1
- data/test/counter_test.rb +27 -13
- metadata +34 -8
@@ -11,7 +11,12 @@ module CustomCounterCache::Model
|
|
11
11
|
unless column_names.include?(cache_column) # Object.const_defined?(:Counter)
|
12
12
|
has_many :counters, :as => :countable, :dependent => :destroy
|
13
13
|
define_method "#{cache_column}" do
|
14
|
-
|
14
|
+
# Check if the counter is already loaded (e.g. eager-loaded)
|
15
|
+
if counters.loaded? && counter = counters.detect{|c| c.key == cache_column.to_s }
|
16
|
+
counter.value
|
17
|
+
else
|
18
|
+
counters.find(:first, :conditions => { :key => cache_column.to_s }).value rescue 0
|
19
|
+
end
|
15
20
|
end
|
16
21
|
define_method "#{cache_column}=" do |count|
|
17
22
|
if ( counter = counters.find(:first, :conditions => { :key => cache_column.to_s }) )
|
data/test/counter_test.rb
CHANGED
@@ -5,27 +5,41 @@ class CounterTest < Test::Unit::TestCase
|
|
5
5
|
def setup
|
6
6
|
@user = User.create
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def test_default_counter_value
|
10
|
-
|
10
|
+
assert_equal 0, @user.published_count
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def test_create_and_destroy_counter
|
14
14
|
@user.articles.create(:state => 'published')
|
15
|
-
|
16
|
-
@user.
|
17
|
-
|
15
|
+
assert_equal 1, Counter.count
|
16
|
+
@user.destroy
|
17
|
+
assert_equal 0, Counter.count
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def test_increment_and_decrement_counter_with_conditions
|
21
21
|
@article = @user.articles.create(:state => 'unpublished')
|
22
|
-
|
22
|
+
assert_equal 0, @user.published_count
|
23
23
|
@article.update_attribute :state, 'published'
|
24
|
-
|
24
|
+
assert_equal 1, @user.published_count
|
25
25
|
3.times { |i| @user.articles.create(:state => 'published') }
|
26
|
-
|
27
|
-
@user.articles.
|
28
|
-
|
26
|
+
assert_equal 4, @user.published_count
|
27
|
+
@user.articles.each {|a| a.update_attributes(:state => 'unpublished') }
|
28
|
+
assert_equal 0, @user.published_count
|
29
|
+
end
|
30
|
+
|
31
|
+
# Test that an eager loaded
|
32
|
+
def test_eager_loading_with_no_counter
|
33
|
+
@article = @user.articles.create(:state => 'unpublished')
|
34
|
+
user = User.includes(:counters).first
|
35
|
+
assert_equal 0, user.published_count
|
36
|
+
|
29
37
|
end
|
30
|
-
|
38
|
+
|
39
|
+
def test_eager_loading_with_counter
|
40
|
+
@article = @user.articles.create(:state => 'published')
|
41
|
+
@user = User.includes(:counters).find(@user.id)
|
42
|
+
assert_equal 1, @user.published_count
|
43
|
+
end
|
44
|
+
|
31
45
|
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: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Cedric Howe
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
19
|
-
default_executable:
|
18
|
+
date: 2012-07-13 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
21
|
name: rails
|
@@ -33,8 +32,36 @@ dependencies:
|
|
33
32
|
version: "2.3"
|
34
33
|
type: :runtime
|
35
34
|
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: sqlite3-ruby
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: debugger
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
36
63
|
description: ""
|
37
|
-
email: cedric@
|
64
|
+
email: cedric@howe.net
|
38
65
|
executables: []
|
39
66
|
|
40
67
|
extensions: []
|
@@ -47,7 +74,6 @@ files:
|
|
47
74
|
- lib/custom_counter_cache.rb
|
48
75
|
- test/counter_test.rb
|
49
76
|
- test/test_helper.rb
|
50
|
-
has_rdoc: true
|
51
77
|
homepage: http://github.com/cedric/custom_counter_cache/
|
52
78
|
licenses: []
|
53
79
|
|
@@ -79,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
105
|
requirements: []
|
80
106
|
|
81
107
|
rubyforge_project: custom_counter_cache
|
82
|
-
rubygems_version: 1.
|
108
|
+
rubygems_version: 1.8.24
|
83
109
|
signing_key:
|
84
110
|
specification_version: 3
|
85
111
|
summary: Custom counter_cache functionality that supports conditions and multiple models.
|