custom_counter_cache 0.0.8 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ODJmZjM2NmQxMGY5MzhiMWRjZTQxNWFiODk3NGZiYWExOTlkNTIyOA==
4
+ YWY5OTJjNDlkYTZhY2VjY2RkOGQ1YWUyMzMxNTBlNTgyZGFkNzcyNQ==
5
5
  data.tar.gz: !binary |-
6
- YjY0NjUwZTQwOGFiYzczNTA0ODZhNmZlYjkzN2ZjMTAzZTgxOGUwMw==
6
+ MjFmY2MxMTFkOGNlNTZlODgxMTg3ZTJmZTVmM2EzNWNkNmIyY2U1MQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YWRjYmUzZWQ0MmUyMDVjZmM4NmQ4YTRiNWY2NGNmZTE4NGIyNTVkMDM4MDIx
10
- MzZhYmZlN2ZiOGE5NjU3OGEyNTE2ZGVmNDI2ZjcyMTcwZTZlMGU3MWVlOGU5
11
- OWZkYWVkYzdkNTQ3MmEzNmNjZTc0YzIwMzU3MDIxODE0MDEzNmY=
9
+ Mzg5N2U5N2NlNTI4MjNmZjM5OWU5MWUyMGZlNTJjNjc2ODZlMTZiMmVlNzgx
10
+ ZTk0YTg1YmIyY2JmZDJkYTA5NDA0N2NjZWQ1NDIxMTJiYzBkMTk4NjM1ZTE5
11
+ OTQyOWViZWRkZTMyOWIxMjdmZDZmZDA4MGNiZDFmMDMzYzg2NTA=
12
12
  data.tar.gz: !binary |-
13
- YWQwMGNkYjA0ZjhjOTI1YTA0ZjI4MGZhMmQ1YTNmODQyZjZiODk0MDNkNDFk
14
- NTkwMmZlN2U4ZTVjMDFjMTllMjUzN2EyNjkwNDMzOTM1NmRmMTBmNjEyYmQx
15
- MGY5YjJjZDRhOWRiOGQ3ZGU5NzgwMGY5MmM4NTM0NzE4Y2IwNTA=
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) # Object.const_defined?(:Counter)
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
- # Check if the counter is already loaded (e.g. eager-loaded)
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.find(:first, :conditions => { :key => cache_column.to_s }).value rescue 0
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.find(:first, :conditions => { :key => cache_column.to_s }) )
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
@@ -1,5 +1,5 @@
1
1
  module CustomCounterCache
2
2
 
3
- VERSION = '0.0.8'
3
+ VERSION = '0.1.0'
4
4
 
5
5
  end
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.count(:conditions => { :articles => { :state => 'published' } })
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.8
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-05-16 00:00:00.000000000 Z
11
+ date: 2013-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails