counter_culture 0.1.24 → 0.1.25

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e9a0ab9e01e7417d2c32fb3b6d142018f2616be
4
- data.tar.gz: 5ca2eb2a8cd0ccd2f7af333fae3685cbb9f7606c
3
+ metadata.gz: 0e6b6af5c5d06c1ac2acc64ece67762e7283d716
4
+ data.tar.gz: 26ef80810b4e221925ede5a498af6f97d025b6ec
5
5
  SHA512:
6
- metadata.gz: 1a301eed807af138528e03fc33edb9c0e8317874d31584bee4a43166f965b1ea91b4cdf9c7edd75a92d0d294919e60c6f568554ee441d24631ab4ba0914e6ec0
7
- data.tar.gz: 1ed11abdefe33c23b3af92212a7df337390e6ef412394dbfeb8e8db86ea314c756c20232ef34b21b167c7df3200d4a15c29247cbb63da89b633d224234bc0d43
6
+ metadata.gz: 41c7a9851b9d974303bd5b9704c8c2a29a385b8b06c5f0673063a6bc1442bf1c5ab5b4533cf0fac268146125cec9a7adb0dd9071077182f35916b92fa245e1a2
7
+ data.tar.gz: 3e60ee5f8ca8dd35163b63ee98a76244b9f4de30b124d03758e10c4d11486173ac47897feb12a56bfd05a554d7c9fe41aa758fb8185c6a6a20809e972d6c9335
data/CHANGELOG.md CHANGED
@@ -1,4 +1,14 @@
1
- ## 0.1.33 (May 24, 2014)
1
+ ## 0.1.25 (July 30, 2014)
2
+
3
+ Bugfixes:
4
+ - makes fix_counts work correctly with custom primary keys
5
+
6
+ ## 0.1.24 (June 27, 2014)
7
+
8
+ Bugfixes:
9
+ - correctly uses custom primary keys when incrementing / decrementing counts
10
+
11
+ ## 0.1.23 (May 24, 2014)
2
12
 
3
13
  Bugfixes:
4
14
  - fixes problems fixing conditional counter caches with batching
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.24
1
+ 0.1.25
@@ -2,15 +2,15 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: counter_culture 0.1.24 ruby lib
5
+ # stub: counter_culture 0.1.25 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "counter_culture"
9
- s.version = "0.1.24"
9
+ s.version = "0.1.25"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.authors = ["Magnus von Koeller"]
13
- s.date = "2014-06-27"
13
+ s.date = "2014-07-30"
14
14
  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."
15
15
  s.email = "magnus@vonkoeller.de"
16
16
  s.extra_rdoc_files = [
@@ -36,6 +36,7 @@ Gem::Specification.new do |s|
36
36
  "lib/generators/counter_culture_generator.rb",
37
37
  "lib/generators/templates/counter_culture_migration.rb.erb",
38
38
  "spec/counter_culture_spec.rb",
39
+ "spec/models/categ.rb",
39
40
  "spec/models/category.rb",
40
41
  "spec/models/company.rb",
41
42
  "spec/models/conditional_dependent.rb",
@@ -43,10 +44,12 @@ Gem::Specification.new do |s|
43
44
  "spec/models/has_string_id.rb",
44
45
  "spec/models/industry.rb",
45
46
  "spec/models/post.rb",
47
+ "spec/models/post_comment.rb",
46
48
  "spec/models/product.rb",
47
49
  "spec/models/review.rb",
48
50
  "spec/models/simple_dependent.rb",
49
51
  "spec/models/simple_main.rb",
52
+ "spec/models/subcateg.rb",
50
53
  "spec/models/twitter_review.rb",
51
54
  "spec/models/user.rb",
52
55
  "spec/rails_app/.gitignore",
@@ -77,7 +77,7 @@ module CounterCulture
77
77
  query = klass
78
78
 
79
79
  # if a delta column is provided use SUM, otherwise use COUNT
80
- count_select = hash[:delta_column] ? "SUM(COALESCE(#{self.table_name}.#{hash[:delta_column]},0))" : "COUNT(#{self.table_name}.id)"
80
+ count_select = hash[:delta_column] ? "SUM(COALESCE(#{self.table_name}.#{hash[:delta_column]},0))" : "COUNT(#{self.table_name}.#{self.primary_key})"
81
81
 
82
82
  # respect the deleted_at column if it exists
83
83
  query = query.where("#{self.table_name}.deleted_at IS NULL") if self.column_names.include?('deleted_at')
@@ -92,7 +92,7 @@ module CounterCulture
92
92
  # store joins in an array so that we can later apply column-specific conditions
93
93
  joins = reverse_relation.map do |cur_relation|
94
94
  reflect = relation_reflect(cur_relation)
95
- joins_query = "LEFT JOIN #{reflect.active_record.table_name} ON #{reflect.table_name}.id = #{reflect.active_record.table_name}.#{reflect.foreign_key}"
95
+ joins_query = "LEFT JOIN #{reflect.active_record.table_name} ON #{reflect.table_name}.#{reflect.klass.primary_key} = #{reflect.active_record.table_name}.#{reflect.foreign_key}"
96
96
  # adds 'type' condition to JOIN clause if the current model is a child in a Single Table Inheritance
97
97
  joins_query = "#{joins_query} AND #{reflect.active_record.table_name}.type IN ('#{self.name}')" if self.column_names.include?('type') and not(self.descends_from_active_record?)
98
98
  joins_query
@@ -101,7 +101,7 @@ module CounterCulture
101
101
  # iterate over all the possible counter cache column names
102
102
  column_names.each do |where, column_name|
103
103
  # select id and count (from above) as well as cache column ('column_name') for later comparison
104
- counts_query = query.select("#{klass.table_name}.id, #{count_select} AS count, #{klass.table_name}.#{column_name}")
104
+ counts_query = query.select("#{klass.table_name}.#{klass.primary_key}, #{count_select} AS count, #{klass.table_name}.#{column_name}")
105
105
 
106
106
  # we need to join together tables until we get back to the table this class itself lives in
107
107
  # conditions must also be applied to the join on which we are counting
@@ -123,14 +123,14 @@ module CounterCulture
123
123
  # keep track of what we fixed, e.g. for a notification email
124
124
  fixed<< {
125
125
  :entity => klass.name,
126
- :id => model.id,
126
+ klass.primary_key.to_sym => model.send(klass.primary_key),
127
127
  :what => column_name,
128
128
  :wrong => model.send(column_name),
129
129
  :right => count
130
130
  }
131
131
  # use update_all because it's faster and because a fixed counter-cache shouldn't
132
132
  # update the timestamp
133
- klass.where(:id => model.id).update_all(column_name => count)
133
+ klass.where(klass.primary_key => model.send(klass.primary_key)).update_all(column_name => count)
134
134
  end
135
135
  end
136
136
 
@@ -371,3 +371,4 @@ module CounterCulture
371
371
  # extend ActiveRecord with our own code here
372
372
  ::ActiveRecord::Base.send :include, ActiveRecord
373
373
  end
374
+
@@ -13,6 +13,9 @@ require 'models/simple_dependent'
13
13
  require 'models/conditional_main'
14
14
  require 'models/conditional_dependent'
15
15
  require 'models/post'
16
+ require 'models/post_comment'
17
+ require 'models/categ'
18
+ require 'models/subcateg'
16
19
 
17
20
  require 'database_cleaner'
18
21
  DatabaseCleaner.strategy = :deletion
@@ -1224,10 +1227,63 @@ describe "CounterCulture" do
1224
1227
  end
1225
1228
 
1226
1229
  it "should use relation primary_key correctly", :focus => true do
1227
- category = Category.create!
1228
- post = Post.create!(:category_id => category.id)
1229
- category.reload
1230
- category.posts_count.should == 1
1230
+ subcateg = Subcateg.create :subcat_id => Subcateg::SUBCAT_1
1231
+ post = Post.new
1232
+ post.subcateg = subcateg
1233
+ post.save!
1234
+ subcateg.reload
1235
+ subcateg.posts_count.should == 1
1236
+ end
1237
+
1238
+ it "should use relation primary key on counter destination table correctly when fixing counts" do
1239
+ subcateg = Subcateg.create :subcat_id => Subcateg::SUBCAT_1
1240
+ post = Post.new
1241
+ post.subcateg = subcateg
1242
+ post.save!
1243
+
1244
+ subcateg.posts_count = -1
1245
+ subcateg.save!
1246
+
1247
+ fixed = Post.counter_culture_fix_counts :only => :subcateg
1248
+
1249
+ fixed.length.should == 1
1250
+ subcateg.reload.posts_count.should == 1
1251
+ end
1252
+
1253
+ it "should use primary key on counted records table correctly when fixing counts" do
1254
+ subcateg = Subcateg.create :subcat_id => Subcateg::SUBCAT_1
1255
+ post = Post.new
1256
+ post.subcateg = subcateg
1257
+ post.save!
1258
+
1259
+ post_comment = PostComment.create!(:post_id => post.id)
1260
+
1261
+ post.comments_count = -1
1262
+ post.save!
1263
+
1264
+ fixed = PostComment.counter_culture_fix_counts
1265
+ fixed.length.should == 1
1266
+ post.reload.comments_count.should == 1
1267
+ end
1268
+
1269
+
1270
+ it "should use multi-level relation primary key on counter destination table correctly when fixing counts" do
1271
+ categ = Categ.create :cat_id => Categ::CAT_1
1272
+ subcateg = Subcateg.new :subcat_id => Subcateg::SUBCAT_1
1273
+ subcateg.categ = categ
1274
+ subcateg.save!
1275
+
1276
+ post = Post.new
1277
+ post.subcateg = subcateg
1278
+ post.save!
1279
+
1280
+ categ.posts_count = -1
1281
+ categ.save!
1282
+
1283
+ fixed = Post.counter_culture_fix_counts :only => [[:subcateg, :categ]]
1284
+
1285
+ fixed.length.should == 1
1286
+ categ.reload.posts_count.should == 1
1231
1287
  end
1232
1288
 
1233
1289
  describe "#previous_model" do
@@ -0,0 +1,8 @@
1
+ class Categ < ActiveRecord::Base
2
+ CAT_1 = 0
3
+ CAT_2 = 1
4
+
5
+ self.primary_key = :cat_id
6
+
7
+ has_many :subcategs, :foreign_key => :fk_subcat_id
8
+ end
data/spec/models/post.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  class Post < ActiveRecord::Base
2
2
  self.primary_key = :post_id
3
3
 
4
- belongs_to :category
5
- counter_culture :category, :column_name => :posts_count
4
+ belongs_to :subcateg, :foreign_key => :fk_subcat_id
5
+
6
+ has_many :post_comments
7
+ counter_culture :subcateg, :column_name => :posts_count
8
+
9
+ counter_culture [:subcateg, :categ]
6
10
  end
@@ -0,0 +1,6 @@
1
+ class PostComment < ActiveRecord::Base
2
+ self.primary_key = :post_id
3
+
4
+ belongs_to :post, :foreign_key => 'post_id'
5
+ counter_culture :post, :column_name => :comments_count
6
+ end
@@ -0,0 +1,9 @@
1
+ class Subcateg < ActiveRecord::Base
2
+ SUBCAT_1 = 0
3
+ SUBCAT_2 = 1
4
+
5
+ self.primary_key = :subcat_id
6
+
7
+ has_many :posts
8
+ belongs_to :categ, :foreign_key => :fk_cat_id
9
+ end
data/spec/schema.rb CHANGED
@@ -114,9 +114,29 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
114
114
  t.datetime "updated_at"
115
115
  end
116
116
 
117
+ create_table "categs", :primary_key => "cat_id", :force => true do |t|
118
+ t.integer "posts_count", :default => 0, :null => false
119
+ t.datetime "created_at"
120
+ t.datetime "updated_at"
121
+ end
122
+
123
+ create_table "subcategs", :primary_key => "subcat_id", :force => true do |t|
124
+ t.integer "fk_cat_id"
125
+ t.integer "posts_count", :default => 0, :null => false
126
+ t.datetime "created_at"
127
+ t.datetime "updated_at"
128
+ end
129
+
117
130
  create_table "posts", :primary_key => "post_id", :force => true do |t|
118
131
  t.string "title"
119
- t.integer "category_id", :default => nil
132
+ t.integer "fk_subcat_id", :default => nil
133
+ t.integer "comments_count", :null => false, :default => 0
134
+ t.datetime "created_at", :null => false
135
+ t.datetime "updated_at", :null => false
136
+ end
137
+
138
+ create_table "post_comments", :primary_key => "post_id", :force => true do |t|
139
+ t.string "comment"
120
140
  t.datetime "created_at", :null => false
121
141
  t.datetime "updated_at", :null => false
122
142
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: counter_culture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.24
4
+ version: 0.1.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Magnus von Koeller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-27 00:00:00.000000000 Z
11
+ date: 2014-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -165,6 +165,7 @@ files:
165
165
  - lib/generators/counter_culture_generator.rb
166
166
  - lib/generators/templates/counter_culture_migration.rb.erb
167
167
  - spec/counter_culture_spec.rb
168
+ - spec/models/categ.rb
168
169
  - spec/models/category.rb
169
170
  - spec/models/company.rb
170
171
  - spec/models/conditional_dependent.rb
@@ -172,10 +173,12 @@ files:
172
173
  - spec/models/has_string_id.rb
173
174
  - spec/models/industry.rb
174
175
  - spec/models/post.rb
176
+ - spec/models/post_comment.rb
175
177
  - spec/models/product.rb
176
178
  - spec/models/review.rb
177
179
  - spec/models/simple_dependent.rb
178
180
  - spec/models/simple_main.rb
181
+ - spec/models/subcateg.rb
179
182
  - spec/models/twitter_review.rb
180
183
  - spec/models/user.rb
181
184
  - spec/rails_app/.gitignore