custom_counter_cache 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/custom_counter_cache/model.rb +6 -4
- data/lib/custom_counter_cache/version.rb +1 -1
- data/test/test_helper.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YWY5OTJjNDlkYTZhY2VjY2RkOGQ1YWUyMzMxNTBlNTgyZGFkNzcyNQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MjFmY2MxMTFkOGNlNTZlODgxMTg3ZTJmZTVmM2EzNWNkNmIyY2U1MQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Mzg5N2U5N2NlNTI4MjNmZjM5OWU5MWUyMGZlNTJjNjc2ODZlMTZiMmVlNzgx
|
10
|
+
ZTk0YTg1YmIyY2JmZDJkYTA5NDA0N2NjZWQ1NDIxMTJiYzBkMTk4NjM1ZTE5
|
11
|
+
OTQyOWViZWRkZTMyOWIxMjdmZDZmZDA4MGNiZDFmMDMzYzg2NTA=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YTk5NTg0NjY3ZWYwMDEyMGY3ZmE4MzRhYmE5NWRkYzMxMDFmNDNhZjQ0MTFl
|
14
|
+
ZjZiY2Q0ODgyYTVjMmI4N2M2YTVjY2MyMTQ4NGQ5NTg0ODljYzE4MjdhYTA1
|
15
|
+
ZGRiNjY3NTE3ZTVjNzFjOTMxZmMxMWU0MjY2ZGExODliMTI1ZWU=
|
@@ -7,19 +7,20 @@ module CustomCounterCache::Model
|
|
7
7
|
module ActsAsMethods
|
8
8
|
|
9
9
|
def define_counter_cache(cache_column, &block)
|
10
|
+
return unless table_exists?
|
10
11
|
# counter accessors
|
11
|
-
unless column_names.include?(cache_column
|
12
|
+
unless column_names.include?(cache_column.to_s)
|
12
13
|
has_many :counters, :as => :countable, :dependent => :destroy
|
13
14
|
define_method "#{cache_column}" do
|
14
|
-
#
|
15
|
+
# check if the counter is loaded
|
15
16
|
if counters.loaded? && counter = counters.detect{|c| c.key == cache_column.to_s }
|
16
17
|
counter.value
|
17
18
|
else
|
18
|
-
counters.
|
19
|
+
counters.where(:key => cache_column.to_s).first.try(:value).to_i
|
19
20
|
end
|
20
21
|
end
|
21
22
|
define_method "#{cache_column}=" do |count|
|
22
|
-
if ( counter = counters.
|
23
|
+
if ( counter = counters.where(:key => cache_column.to_s).first )
|
23
24
|
counter.update_attribute :value, count.to_i
|
24
25
|
else
|
25
26
|
counters.create :key => cache_column.to_s, :value => count.to_i
|
@@ -33,6 +34,7 @@ module CustomCounterCache::Model
|
|
33
34
|
end
|
34
35
|
|
35
36
|
def update_counter_cache(association, cache_column, options = {})
|
37
|
+
return unless table_exists?
|
36
38
|
association = association.to_sym
|
37
39
|
cache_column = cache_column.to_sym
|
38
40
|
method_name = "callback_#{cache_column}".to_sym
|
data/test/test_helper.rb
CHANGED
@@ -25,7 +25,7 @@ end
|
|
25
25
|
class User < ActiveRecord::Base
|
26
26
|
has_many :articles
|
27
27
|
define_counter_cache :published_count do |user|
|
28
|
-
user.articles.
|
28
|
+
user.articles.where(:articles => { :state => 'published' }).count
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
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.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cedric Howe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|