counter_culture 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "counter_culture"
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Magnus von Koeller"]
12
- s.date = "2012-05-30"
12
+ s.date = "2012-05-31"
13
13
  s.description = "counter_culture provides turbo-charged counter caches that are kept up-to-date not just on create and destroy, that support multiple levels of indirection through relationships, allow dynamic column names and that avoid deadlocks by updating in the after_commit callback."
14
14
  s.email = "magnus@vonkoeller.de"
15
15
  s.extra_rdoc_files = [
@@ -38,6 +38,9 @@ module CounterCulture
38
38
  # on original data; if the counter cache is incorrect, sets it to the correct
39
39
  # count
40
40
  #
41
+ # options:
42
+ # { :exclude => list of relations to skip when fixing counts,
43
+ # :only => only these relations will have their counts fixed }
41
44
  # returns: a list of fixed record as an array of hashes of the form:
42
45
  # { :entity => which model the count was fixed on,
43
46
  # :id => the id of the model that had the incorrect count,
@@ -45,9 +48,17 @@ module CounterCulture
45
48
  # :wrong => the previously saved, incorrect count,
46
49
  # :right => the newly fixed, correct count }
47
50
  #
48
- def counter_culture_fix_counts
51
+ def counter_culture_fix_counts(options = {})
52
+ options[:exclude] = [options[:exclude]] if options[:exclude] && !options[:exclude].is_a?(Enumerable)
53
+ options[:exclude] = options[:exclude].try(:map) {|x| x.is_a?(Enumerable) ? x : [x] }
54
+ options[:only] = [options[:only]] if options[:only] && !options[:only].is_a?(Enumerable)
55
+ options[:only] = options[:only].try(:map) {|x| x.is_a?(Enumerable) ? x : [x] }
56
+
49
57
  fixed = []
50
- @after_commit_counter_cache.map do |hash|
58
+ @after_commit_counter_cache.each do |hash|
59
+ next if options[:exclude] && options[:exclude].include?(hash[:relation])
60
+ next if options[:only] && !options[:only].include?(hash[:relation])
61
+
51
62
  # which class does this relation ultimately point to? that's where we have to start
52
63
  klass = relation_klass(hash[:relation])
53
64
 
@@ -55,6 +66,8 @@ module CounterCulture
55
66
  query = klass.select("#{klass.table_name}.id, COUNT(#{self.table_name}.id) AS count")
56
67
  query = query.group("#{klass.table_name}.id")
57
68
 
69
+ raise "Fixing counter caches is not supported when using :foreign_key_values" if hash[:foreign_key_values]
70
+
58
71
  # if we're provided a custom set of column names with conditions, use them; just use the
59
72
  # column name otherwise
60
73
  raise "Must provide :column_names option for relation #{hash[:relation].inspect} when :counter_cache_name is a Proc" if hash[:counter_cache_name].is_a?(Proc) && !hash[:column_names]
@@ -83,7 +96,7 @@ module CounterCulture
83
96
  # now that we know what the correct counts are, we need to iterate over all instances
84
97
  # and check whether the count is correct; if not, we correct it
85
98
  klass.find_each do |model|
86
- if model.send(column_name) != counts[model.id].to_i
99
+ if model.read_attribute(column_name) != counts[model.id].to_i
87
100
  # keep track of what we fixed, e.g. for a notification email
88
101
  fixed<< {
89
102
  :entity => klass.name,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: counter_culture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-30 00:00:00.000000000 Z
12
+ date: 2012-05-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -216,7 +216,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
216
216
  version: '0'
217
217
  segments:
218
218
  - 0
219
- hash: -3220703569858199407
219
+ hash: -1949241983326778340
220
220
  required_rubygems_version: !ruby/object:Gem::Requirement
221
221
  none: false
222
222
  requirements: