counter_culture 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -185,6 +185,10 @@ end
185
185
 
186
186
  Manually populating counter caches with dynamicall over-written foreign keys (```:foreign_key_values``` option) is not supported. You will have to write code to handle this case yourself.
187
187
 
188
+ #### Polymorphic associations
189
+
190
+ counter_culture currently does *not* support polymorphic associations. Check [this issue](https://github.com/bestvendor/counter_culture/issues/4) for progress and alternatives.
191
+
188
192
  ## Contributing to counter_culture
189
193
 
190
194
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.9
1
+ 0.1.10
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "counter_culture"
8
- s.version = "0.1.9"
8
+ s.version = "0.1.10"
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 = "2013-01-08"
12
+ s.date = "2013-03-27"
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 = [
@@ -172,35 +172,55 @@ module CounterCulture
172
172
  end
173
173
 
174
174
  private
175
+ # need to make sure counter_culture is only activated once
176
+ # per commit; otherwise, if we do an update in an after_create,
177
+ # we would be triggered twice within the same transaction -- once
178
+ # for the create, once for the update
179
+ def _wrap_in_counter_culture_active(&block)
180
+ if @_counter_culture_active
181
+ # don't do anything; we are already active for this transaction
182
+ else
183
+ @_counter_culture_active = true
184
+ block.call
185
+ execute_after_commit { @_counter_culture_active = false}
186
+ end
187
+ end
188
+
175
189
  # called by after_create callback
176
190
  def _update_counts_after_create
177
- self.class.after_commit_counter_cache.each do |hash|
178
- # increment counter cache
179
- change_counter_cache(hash.merge(:increment => true))
191
+ _wrap_in_counter_culture_active do
192
+ self.class.after_commit_counter_cache.each do |hash|
193
+ # increment counter cache
194
+ change_counter_cache(hash.merge(:increment => true))
195
+ end
180
196
  end
181
197
  end
182
198
 
183
199
  # called by after_destroy callback
184
200
  def _update_counts_after_destroy
185
- self.class.after_commit_counter_cache.each do |hash|
186
- # decrement counter cache
187
- change_counter_cache(hash.merge(:increment => false))
201
+ _wrap_in_counter_culture_active do
202
+ self.class.after_commit_counter_cache.each do |hash|
203
+ # decrement counter cache
204
+ change_counter_cache(hash.merge(:increment => false))
205
+ end
188
206
  end
189
207
  end
190
208
 
191
209
  # called by after_update callback
192
210
  def _update_counts_after_update
193
- self.class.after_commit_counter_cache.each do |hash|
194
- # figure out whether the applicable counter cache changed (this can happen
195
- # with dynamic column names)
196
- counter_cache_name_was = counter_cache_name_for(previous_model, hash[:counter_cache_name])
197
- counter_cache_name = counter_cache_name_for(self, hash[:counter_cache_name])
198
-
199
- if send("#{first_level_relation_foreign_key(hash[:relation])}_changed?") || counter_cache_name != counter_cache_name_was
200
- # increment the counter cache of the new value
201
- change_counter_cache(hash.merge(:increment => true, :counter_column => counter_cache_name))
202
- # decrement the counter cache of the old value
203
- change_counter_cache(hash.merge(:increment => false, :was => true, :counter_column => counter_cache_name_was))
211
+ _wrap_in_counter_culture_active do
212
+ self.class.after_commit_counter_cache.each do |hash|
213
+ # figure out whether the applicable counter cache changed (this can happen
214
+ # with dynamic column names)
215
+ counter_cache_name_was = counter_cache_name_for(previous_model, hash[:counter_cache_name])
216
+ counter_cache_name = counter_cache_name_for(self, hash[:counter_cache_name])
217
+
218
+ if send("#{first_level_relation_foreign_key(hash[:relation])}_changed?") || counter_cache_name != counter_cache_name_was
219
+ # increment the counter cache of the new value
220
+ change_counter_cache(hash.merge(:increment => true, :counter_column => counter_cache_name))
221
+ # decrement the counter cache of the old value
222
+ change_counter_cache(hash.merge(:increment => false, :was => true, :counter_column => counter_cache_name_was))
223
+ end
204
224
  end
205
225
  end
206
226
  end
@@ -81,7 +81,7 @@ describe "CounterCulture" do
81
81
 
82
82
  it "increments second-level counter cache on create" do
83
83
  company = Company.create
84
- user = User.create :company_id => company.id
84
+ user = User.create :manages_company_id => company.id
85
85
  product = Product.create
86
86
 
87
87
  company.reviews_count.should == 0
@@ -101,7 +101,7 @@ describe "CounterCulture" do
101
101
 
102
102
  it "decrements second-level counter cache on destroy" do
103
103
  company = Company.create
104
- user = User.create :company_id => company.id
104
+ user = User.create :manages_company_id => company.id
105
105
  product = Product.create
106
106
 
107
107
  company.reviews_count.should == 0
@@ -132,8 +132,8 @@ describe "CounterCulture" do
132
132
  it "updates second-level counter cache on update" do
133
133
  company1 = Company.create
134
134
  company2 = Company.create
135
- user1 = User.create :company_id => company1.id
136
- user2 = User.create :company_id => company2.id
135
+ user1 = User.create :manages_company_id => company1.id
136
+ user2 = User.create :manages_company_id => company2.id
137
137
  product = Product.create
138
138
 
139
139
  user1.reviews_count.should == 0
@@ -364,7 +364,7 @@ describe "CounterCulture" do
364
364
  it "increments third-level counter cache on create" do
365
365
  industry = Industry.create
366
366
  company = Company.create :industry_id => industry.id
367
- user = User.create :company_id => company.id
367
+ user = User.create :manages_company_id => company.id
368
368
  product = Product.create
369
369
 
370
370
  industry.reviews_count.should == 0
@@ -388,7 +388,7 @@ describe "CounterCulture" do
388
388
  it "decrements third-level counter cache on destroy" do
389
389
  industry = Industry.create
390
390
  company = Company.create :industry_id => industry.id
391
- user = User.create :company_id => company.id
391
+ user = User.create :manages_company_id => company.id
392
392
  product = Product.create
393
393
 
394
394
  industry.reviews_count.should == 0
@@ -426,8 +426,8 @@ describe "CounterCulture" do
426
426
  industry2 = Industry.create
427
427
  company1 = Company.create :industry_id => industry1.id
428
428
  company2 = Company.create :industry_id => industry2.id
429
- user1 = User.create :company_id => company1.id
430
- user2 = User.create :company_id => company2.id
429
+ user1 = User.create :manages_company_id => company1.id
430
+ user2 = User.create :manages_company_id => company2.id
431
431
  product = Product.create
432
432
 
433
433
  industry1.reviews_count.should == 0
@@ -474,7 +474,7 @@ describe "CounterCulture" do
474
474
  it "increments third-level custom counter cache on create" do
475
475
  industry = Industry.create
476
476
  company = Company.create :industry_id => industry.id
477
- user = User.create :company_id => company.id
477
+ user = User.create :manages_company_id => company.id
478
478
  product = Product.create
479
479
 
480
480
  industry.rexiews_count.should == 0
@@ -489,7 +489,7 @@ describe "CounterCulture" do
489
489
  it "decrements third-level custom counter cache on destroy" do
490
490
  industry = Industry.create
491
491
  company = Company.create :industry_id => industry.id
492
- user = User.create :company_id => company.id
492
+ user = User.create :manages_company_id => company.id
493
493
  product = Product.create
494
494
 
495
495
  industry.rexiews_count.should == 0
@@ -510,8 +510,8 @@ describe "CounterCulture" do
510
510
  industry2 = Industry.create
511
511
  company1 = Company.create :industry_id => industry1.id
512
512
  company2 = Company.create :industry_id => industry2.id
513
- user1 = User.create :company_id => company1.id
514
- user2 = User.create :company_id => company2.id
513
+ user1 = User.create :manages_company_id => company1.id
514
+ user2 = User.create :manages_company_id => company2.id
515
515
  product = Product.create
516
516
 
517
517
  industry1.rexiews_count.should == 0
@@ -594,7 +594,7 @@ describe "CounterCulture" do
594
594
  it "increments third-level dynamic counter cache on create" do
595
595
  industry = Industry.create
596
596
  company = Company.create :industry_id => industry.id
597
- user = User.create :company_id => company.id
597
+ user = User.create :manages_company_id => company.id
598
598
  product = Product.create
599
599
 
600
600
  industry.using_count.should == 0
@@ -618,7 +618,7 @@ describe "CounterCulture" do
618
618
  it "decrements third-level custom counter cache on destroy" do
619
619
  industry = Industry.create
620
620
  company = Company.create :industry_id => industry.id
621
- user = User.create :company_id => company.id
621
+ user = User.create :manages_company_id => company.id
622
622
  product = Product.create
623
623
 
624
624
  industry.using_count.should == 0
@@ -658,8 +658,8 @@ describe "CounterCulture" do
658
658
  industry2 = Industry.create
659
659
  company1 = Company.create :industry_id => industry1.id
660
660
  company2 = Company.create :industry_id => industry2.id
661
- user1 = User.create :company_id => company1.id
662
- user2 = User.create :company_id => company2.id
661
+ user1 = User.create :manages_company_id => company1.id
662
+ user2 = User.create :manages_company_id => company2.id
663
663
  product = Product.create
664
664
 
665
665
  industry1.using_count.should == 0
@@ -774,7 +774,7 @@ describe "CounterCulture" do
774
774
 
775
775
  it "should fix a second-level counter cache correctly" do
776
776
  company = Company.create
777
- user = User.create :company_id => company.id
777
+ user = User.create :manages_company_id => company.id
778
778
  product = Product.create
779
779
 
780
780
  company.reviews_count.should == 0
@@ -888,18 +888,18 @@ describe "CounterCulture" do
888
888
  end
889
889
 
890
890
  describe "#previous_model" do
891
- let(:user){User.create :name => "John Smith", :company_id => 1}
891
+ let(:user){User.create :name => "John Smith", :manages_company_id => 1}
892
892
 
893
893
  it "should return a copy of the original model" do
894
894
  user.name = "Joe Smith"
895
- user.company_id = 2
895
+ user.manages_company_id = 2
896
896
  prev = user.send(:previous_model)
897
897
 
898
898
  prev.name.should == "John Smith"
899
- prev.company_id.should == 1
899
+ prev.manages_company_id.should == 1
900
900
 
901
901
  user.name.should =="Joe Smith"
902
- user.company_id.should == 2
902
+ user.manages_company_id.should == 2
903
903
  end
904
904
  end
905
905
 
@@ -6,8 +6,14 @@ class Review < ActiveRecord::Base
6
6
  counter_culture :product, :column_name => 'rexiews_count'
7
7
  counter_culture :user
8
8
  counter_culture :user, :column_name => Proc.new { |model| model.review_type ? "#{model.review_type}_count" : nil }, :column_names => {"reviews.review_type = 'using'" => 'using_count', "reviews.review_type = 'tried'" => 'tried_count'}
9
- counter_culture [:user, :company]
10
- counter_culture [:user, :company, :industry]
11
- counter_culture [:user, :company, :industry], :column_name => 'rexiews_count'
12
- counter_culture [:user, :company, :industry], :column_name => Proc.new { |model| model.review_type ? "#{model.review_type}_count" : nil }
9
+ counter_culture [:user, :manages_company]
10
+ counter_culture [:user, :manages_company, :industry]
11
+ counter_culture [:user, :manages_company, :industry], :column_name => 'rexiews_count'
12
+ counter_culture [:user, :manages_company, :industry], :column_name => Proc.new { |model| model.review_type ? "#{model.review_type}_count" : nil }
13
+
14
+ after_create :update_some_text
15
+
16
+ def update_some_text
17
+ update_attribute(:some_text, rand(36**12).to_s(36))
18
+ end
13
19
  end
@@ -39,6 +39,7 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
39
39
 
40
40
  create_table "reviews", :force => true do |t|
41
41
  t.string "review_type", :default => "using"
42
+ t.string "some_text"
42
43
  t.integer "user_id"
43
44
  t.integer "product_id"
44
45
  end
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.9
4
+ version: 0.1.10
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: 2013-01-08 00:00:00.000000000 Z
12
+ date: 2013-03-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -218,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
218
218
  version: '0'
219
219
  segments:
220
220
  - 0
221
- hash: 1797196708173178324
221
+ hash: 2320652416701814661
222
222
  required_rubygems_version: !ruby/object:Gem::Requirement
223
223
  none: false
224
224
  requirements: