counter_culture 0.1.29 → 0.1.30
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +1 -0
- data/VERSION +1 -1
- data/counter_culture.gemspec +8 -3
- data/lib/counter_culture.rb +19 -6
- data/spec/counter_culture_spec.rb +31 -0
- data/spec/models/another_post.rb +13 -0
- data/spec/models/another_post_comment.rb +4 -0
- data/spec/schema.rb +17 -0
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e50039fb9bcff88c135a812d83e6ac9779fe740a
|
4
|
+
data.tar.gz: 683b72e7070a3746d8a15d86774ac5f94ca4cd0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5452993949dfa0558e374dd9f463ec3323a595de7112efba085b834fe1c778a6af1044cbd8c74949f33d7c8eedae122f18b56e62a239b579f50fa661819e09a4
|
7
|
+
data.tar.gz: 354e8bb001d985e03be663ad4ac0fb1053b91c607695e1188954439a7e967976f4ef60edafe37061bbe9da25af0a71ef0bf2ec02a54b4479ab48c1832bd842dc
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.30
|
data/counter_culture.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
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.
|
5
|
+
# stub: counter_culture 0.1.30 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "counter_culture"
|
9
|
-
s.version = "0.1.
|
9
|
+
s.version = "0.1.30"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Magnus von Koeller"]
|
14
|
-
s.date = "
|
14
|
+
s.date = "2015-02-10"
|
15
15
|
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."
|
16
16
|
s.email = "magnus@vonkoeller.de"
|
17
17
|
s.extra_rdoc_files = [
|
@@ -37,6 +37,8 @@ Gem::Specification.new do |s|
|
|
37
37
|
"lib/generators/counter_culture_generator.rb",
|
38
38
|
"lib/generators/templates/counter_culture_migration.rb.erb",
|
39
39
|
"spec/counter_culture_spec.rb",
|
40
|
+
"spec/models/another_post.rb",
|
41
|
+
"spec/models/another_post_comment.rb",
|
40
42
|
"spec/models/categ.rb",
|
41
43
|
"spec/models/category.rb",
|
42
44
|
"spec/models/company.rb",
|
@@ -114,6 +116,7 @@ Gem::Specification.new do |s|
|
|
114
116
|
|
115
117
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
116
118
|
s.add_runtime_dependency(%q<after_commit_action>, ["~> 0.1.3"])
|
119
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
|
117
120
|
s.add_development_dependency(%q<rake>, [">= 0"])
|
118
121
|
s.add_development_dependency(%q<rails>, [">= 3.1.0"])
|
119
122
|
s.add_development_dependency(%q<rspec>, ["~> 2.10.0"])
|
@@ -123,6 +126,7 @@ Gem::Specification.new do |s|
|
|
123
126
|
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
124
127
|
else
|
125
128
|
s.add_dependency(%q<after_commit_action>, ["~> 0.1.3"])
|
129
|
+
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
126
130
|
s.add_dependency(%q<rake>, [">= 0"])
|
127
131
|
s.add_dependency(%q<rails>, [">= 3.1.0"])
|
128
132
|
s.add_dependency(%q<rspec>, ["~> 2.10.0"])
|
@@ -133,6 +137,7 @@ Gem::Specification.new do |s|
|
|
133
137
|
end
|
134
138
|
else
|
135
139
|
s.add_dependency(%q<after_commit_action>, ["~> 0.1.3"])
|
140
|
+
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
136
141
|
s.add_dependency(%q<rake>, [">= 0"])
|
137
142
|
s.add_dependency(%q<rails>, [">= 3.1.0"])
|
138
143
|
s.add_dependency(%q<rspec>, ["~> 2.10.0"])
|
data/lib/counter_culture.rb
CHANGED
@@ -104,7 +104,7 @@ module CounterCulture
|
|
104
104
|
join_table_name = reflect.active_record.table_name
|
105
105
|
end
|
106
106
|
# join with alias to avoid ambiguous table name with self-referential models:
|
107
|
-
joins_query = "LEFT JOIN #{reflect.active_record.table_name} AS #{join_table_name} ON #{reflect.table_name}.#{reflect.
|
107
|
+
joins_query = "LEFT JOIN #{reflect.active_record.table_name} AS #{join_table_name} ON #{reflect.table_name}.#{reflect.association_primary_key} = #{join_table_name}.#{reflect.foreign_key}"
|
108
108
|
# adds 'type' condition to JOIN clause if the current model is a child in a Single Table Inheritance
|
109
109
|
joins_query = "#{joins_query} AND #{reflect.active_record.table_name}.type IN ('#{self.name}')" if reflect.active_record.column_names.include?('type') and not(self.descends_from_active_record?)
|
110
110
|
joins_query
|
@@ -112,8 +112,8 @@ module CounterCulture
|
|
112
112
|
|
113
113
|
# iterate over all the possible counter cache column names
|
114
114
|
column_names.each do |where, column_name|
|
115
|
-
# select
|
116
|
-
counts_query = query.select("#{klass.table_name}.#{klass.primary_key}, #{count_select} AS count, #{klass.table_name}.#{column_name}")
|
115
|
+
# select join column and count (from above) as well as cache column ('column_name') for later comparison
|
116
|
+
counts_query = query.select("#{klass.table_name}.#{klass.primary_key}, #{klass.table_name}.#{relation_reflect(hash[:relation]).association_primary_key}, #{count_select} AS count, #{klass.table_name}.#{column_name}")
|
117
117
|
|
118
118
|
# we need to join together tables until we get back to the table this class itself lives in
|
119
119
|
# conditions must also be applied to the join on which we are counting
|
@@ -195,6 +195,14 @@ module CounterCulture
|
|
195
195
|
relation_reflect(relation).foreign_key
|
196
196
|
end
|
197
197
|
|
198
|
+
# gets the primary key name of the given relation
|
199
|
+
#
|
200
|
+
# relation: a symbol or array of symbols; specifies the relation
|
201
|
+
# that has the counter cache column
|
202
|
+
def relation_primary_key(relation)
|
203
|
+
relation_reflect(relation).association_primary_key
|
204
|
+
end
|
205
|
+
|
198
206
|
# gets the foreign key name of the relation. will look at the first
|
199
207
|
# level only -- i.e., if passed an array will consider only its
|
200
208
|
# first element
|
@@ -309,7 +317,7 @@ module CounterCulture
|
|
309
317
|
end
|
310
318
|
|
311
319
|
klass = relation_klass(options[:relation])
|
312
|
-
klass.where(
|
320
|
+
klass.where(relation_primary_key(options[:relation]) => id_to_change).update_all updates.join(', ')
|
313
321
|
end
|
314
322
|
end
|
315
323
|
end
|
@@ -349,17 +357,18 @@ module CounterCulture
|
|
349
357
|
# current value
|
350
358
|
def foreign_key_value(relation, was = false)
|
351
359
|
relation = relation.is_a?(Enumerable) ? relation.dup : [relation]
|
360
|
+
first_relation = relation.first
|
352
361
|
if was
|
353
362
|
first = relation.shift
|
354
363
|
foreign_key_value = send("#{relation_foreign_key(first)}_was")
|
355
|
-
value = relation_klass(first).
|
364
|
+
value = relation_klass(first).find_by(relation_primary_key(first) => foreign_key_value) if foreign_key_value
|
356
365
|
else
|
357
366
|
value = self
|
358
367
|
end
|
359
368
|
while !value.nil? && relation.size > 0
|
360
369
|
value = value.send(relation.shift)
|
361
370
|
end
|
362
|
-
return value.try(
|
371
|
+
return value.try(relation_primary_key(first_relation).to_sym)
|
363
372
|
end
|
364
373
|
|
365
374
|
def relation_klass(relation)
|
@@ -374,6 +383,10 @@ module CounterCulture
|
|
374
383
|
self.class.send :relation_foreign_key, relation
|
375
384
|
end
|
376
385
|
|
386
|
+
def relation_primary_key(relation)
|
387
|
+
self.class.send :relation_primary_key, relation
|
388
|
+
end
|
389
|
+
|
377
390
|
def first_level_relation_foreign_key(relation)
|
378
391
|
self.class.send :first_level_relation_foreign_key, relation
|
379
392
|
end
|
@@ -16,6 +16,8 @@ require 'models/post'
|
|
16
16
|
require 'models/post_comment'
|
17
17
|
require 'models/categ'
|
18
18
|
require 'models/subcateg'
|
19
|
+
require 'models/another_post'
|
20
|
+
require 'models/another_post_comment'
|
19
21
|
|
20
22
|
require 'database_cleaner'
|
21
23
|
DatabaseCleaner.strategy = :deletion
|
@@ -25,6 +27,35 @@ describe "CounterCulture" do
|
|
25
27
|
DatabaseCleaner.clean
|
26
28
|
end
|
27
29
|
|
30
|
+
|
31
|
+
it "should use relation foreign_key correctly", :focus => true do
|
32
|
+
post = AnotherPost.new
|
33
|
+
comment = post.comments.build
|
34
|
+
comment.comment = 'Comment'
|
35
|
+
post.save!
|
36
|
+
post.reload
|
37
|
+
post.another_post_comments_count.should == 1
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should fix counts using relation foreign_key correctly", :focus => true do
|
41
|
+
post = AnotherPost.new
|
42
|
+
comment = post.comments.build
|
43
|
+
comment.comment = 'Comment'
|
44
|
+
post.save!
|
45
|
+
post.reload
|
46
|
+
post.another_post_comments_count.should == 1
|
47
|
+
post.comments.size.should == 1
|
48
|
+
|
49
|
+
post.another_post_comments_count = 2
|
50
|
+
post.save!
|
51
|
+
|
52
|
+
fixed = AnotherPostComment.counter_culture_fix_counts
|
53
|
+
fixed.length.should == 1
|
54
|
+
|
55
|
+
post.reload
|
56
|
+
post.another_post_comments_count.should == 1
|
57
|
+
end
|
58
|
+
|
28
59
|
it "increments counter cache on create" do
|
29
60
|
user = User.create
|
30
61
|
product = Product.create
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class AnotherPost < ActiveRecord::Base
|
2
|
+
has_many :comments, class_name: 'AnotherPostComment', foreign_key: 'another_post_id', primary_key: 'another_id'
|
3
|
+
|
4
|
+
before_save :assign_another_id, :if => :new_record?
|
5
|
+
|
6
|
+
private
|
7
|
+
def assign_another_id
|
8
|
+
loop do
|
9
|
+
self.another_id = Random.rand(65535)
|
10
|
+
break unless self.class.where(another_id: another_id).exists?
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/spec/schema.rb
CHANGED
@@ -143,4 +143,21 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
|
|
143
143
|
t.datetime "created_at", :null => false
|
144
144
|
t.datetime "updated_at", :null => false
|
145
145
|
end
|
146
|
+
|
147
|
+
create_table "another_posts", :force => true do |t|
|
148
|
+
t.string "title"
|
149
|
+
t.integer "another_id", :null => false
|
150
|
+
t.integer "another_post_comments_count", :null => false, :default => 0
|
151
|
+
t.datetime "created_at", :null => false
|
152
|
+
t.datetime "updated_at", :null => false
|
153
|
+
end
|
154
|
+
add_index "another_posts", :another_id, :unique => true
|
155
|
+
|
156
|
+
create_table "another_post_comments", :force => true do |t|
|
157
|
+
t.integer "another_post_id"
|
158
|
+
t.string "comment"
|
159
|
+
t.datetime "created_at", :null => false
|
160
|
+
t.datetime "updated_at", :null => false
|
161
|
+
end
|
162
|
+
|
146
163
|
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.
|
4
|
+
version: 0.1.30
|
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:
|
11
|
+
date: 2015-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: after_commit_action
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.1.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.0
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -151,6 +165,8 @@ files:
|
|
151
165
|
- lib/generators/counter_culture_generator.rb
|
152
166
|
- lib/generators/templates/counter_culture_migration.rb.erb
|
153
167
|
- spec/counter_culture_spec.rb
|
168
|
+
- spec/models/another_post.rb
|
169
|
+
- spec/models/another_post_comment.rb
|
154
170
|
- spec/models/categ.rb
|
155
171
|
- spec/models/category.rb
|
156
172
|
- spec/models/company.rb
|