counter_culture 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/counter_culture.gemspec +2 -2
- data/lib/counter_culture.rb +11 -10
- data/spec/counter_culture_spec.rb +69 -0
- data/spec/models/review.rb +1 -1
- data/spec/schema.rb +1 -1
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.6
|
data/counter_culture.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "counter_culture"
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.6"
|
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-06-
|
12
|
+
s.date = "2012-06-07"
|
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 = [
|
data/lib/counter_culture.rb
CHANGED
@@ -212,20 +212,21 @@ module CounterCulture
|
|
212
212
|
id_to_change = foreign_key_value(hash[:relation], was)
|
213
213
|
# allow overwriting of foreign key value by the caller
|
214
214
|
id_to_change = hash[:foreign_key_values].call(id_to_change) if hash[:foreign_key_values]
|
215
|
-
|
215
|
+
|
216
|
+
# figure out what the column name is
|
217
|
+
if hash[:counter_cache_name].is_a? Proc
|
218
|
+
# dynamic column name -- call the Proc
|
219
|
+
counter_cache_name = hash[:counter_cache_name].call(self)
|
220
|
+
else
|
221
|
+
# static column name
|
222
|
+
counter_cache_name = hash[:counter_cache_name]
|
223
|
+
end
|
224
|
+
|
225
|
+
if id_to_change && counter_cache_name
|
216
226
|
execute_after_commit do
|
217
227
|
# increment or decrement?
|
218
228
|
method = increment ? :increment_counter : :decrement_counter
|
219
229
|
|
220
|
-
# figure out what the column name is
|
221
|
-
if hash[:counter_cache_name].is_a? Proc
|
222
|
-
# dynamic column name -- call the Proc
|
223
|
-
counter_cache_name = hash[:counter_cache_name].call(self)
|
224
|
-
else
|
225
|
-
# static column name
|
226
|
-
counter_cache_name = hash[:counter_cache_name]
|
227
|
-
end
|
228
|
-
|
229
230
|
# do it!
|
230
231
|
relation_klass(hash[:relation]).send(method, counter_cache_name, id_to_change)
|
231
232
|
end
|
@@ -230,6 +230,75 @@ describe "CounterCulture" do
|
|
230
230
|
product2.rexiews_count.should == 1
|
231
231
|
end
|
232
232
|
|
233
|
+
it "handles nil column name in custom counter cache on create" do
|
234
|
+
user = User.create
|
235
|
+
product = Product.create
|
236
|
+
|
237
|
+
user.using_count.should == 0
|
238
|
+
user.tried_count.should == 0
|
239
|
+
|
240
|
+
review = Review.create :user_id => user.id, :product_id => product.id, :review_type => nil
|
241
|
+
|
242
|
+
user.reload
|
243
|
+
|
244
|
+
user.using_count.should == 0
|
245
|
+
user.tried_count.should == 0
|
246
|
+
end
|
247
|
+
|
248
|
+
it "handles nil column name in custom counter cache on destroy" do
|
249
|
+
user = User.create
|
250
|
+
product = Product.create
|
251
|
+
|
252
|
+
user.using_count.should == 0
|
253
|
+
user.tried_count.should == 0
|
254
|
+
|
255
|
+
review = Review.create :user_id => user.id, :product_id => product.id, :review_type => nil
|
256
|
+
|
257
|
+
product.reload
|
258
|
+
|
259
|
+
user.using_count.should == 0
|
260
|
+
user.tried_count.should == 0
|
261
|
+
|
262
|
+
review.destroy
|
263
|
+
|
264
|
+
product.reload
|
265
|
+
|
266
|
+
user.using_count.should == 0
|
267
|
+
user.tried_count.should == 0
|
268
|
+
end
|
269
|
+
|
270
|
+
it "handles nil column name in custom counter cache on update" do
|
271
|
+
product = Product.create
|
272
|
+
user1 = User.create
|
273
|
+
user2 = User.create
|
274
|
+
|
275
|
+
user1.using_count.should == 0
|
276
|
+
user1.tried_count.should == 0
|
277
|
+
user2.using_count.should == 0
|
278
|
+
user2.tried_count.should == 0
|
279
|
+
|
280
|
+
review = Review.create :user_id => user1.id, :product_id => product.id, :review_type => nil
|
281
|
+
|
282
|
+
user1.reload
|
283
|
+
user2.reload
|
284
|
+
|
285
|
+
user1.using_count.should == 0
|
286
|
+
user1.tried_count.should == 0
|
287
|
+
user2.using_count.should == 0
|
288
|
+
user2.tried_count.should == 0
|
289
|
+
|
290
|
+
review.user = user2
|
291
|
+
review.save!
|
292
|
+
|
293
|
+
user1.reload
|
294
|
+
user2.reload
|
295
|
+
|
296
|
+
user1.using_count.should == 0
|
297
|
+
user1.tried_count.should == 0
|
298
|
+
user2.using_count.should == 0
|
299
|
+
user2.tried_count.should == 0
|
300
|
+
end
|
301
|
+
|
233
302
|
it "increments third-level counter cache on create" do
|
234
303
|
industry = Industry.create
|
235
304
|
company = Company.create :industry_id => industry.id
|
data/spec/models/review.rb
CHANGED
@@ -5,7 +5,7 @@ class Review < ActiveRecord::Base
|
|
5
5
|
counter_culture :product
|
6
6
|
counter_culture :product, :column_name => 'rexiews_count'
|
7
7
|
counter_culture :user
|
8
|
-
counter_culture :user, :column_name => Proc.new { |model| "#{model.review_type}_count" }, :column_names => {"reviews.review_type = 'using'" => 'using_count', "reviews.review_type = 'tried'" => 'tried_count'}
|
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
9
|
counter_culture [:user, :company]
|
10
10
|
counter_culture [:user, :company, :industry]
|
11
11
|
counter_culture [:user, :company, :industry], :column_name => 'rexiews_count'
|
data/spec/schema.rb
CHANGED
@@ -37,7 +37,7 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
create_table "reviews", :force => true do |t|
|
40
|
-
t.string "review_type", :default => "using"
|
40
|
+
t.string "review_type", :default => "using"
|
41
41
|
t.integer "user_id"
|
42
42
|
t.integer "product_id"
|
43
43
|
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.
|
4
|
+
version: 0.1.6
|
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-06-
|
12
|
+
date: 2012-06-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -217,7 +217,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
217
217
|
version: '0'
|
218
218
|
segments:
|
219
219
|
- 0
|
220
|
-
hash: -
|
220
|
+
hash: -2752242088378468016
|
221
221
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
222
222
|
none: false
|
223
223
|
requirements:
|