counter_culture 0.1.15 → 0.1.16
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/README.md +10 -1
- data/VERSION +1 -1
- data/counter_culture.gemspec +1 -1
- data/lib/counter_culture.rb +18 -4
- data/spec/counter_culture_spec.rb +14 -0
- data/spec/models/review.rb +1 -1
- data/spec/schema.rb +18 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d255dba9072e3f8fee1a78ac3bbd9112cbe449f6
|
4
|
+
data.tar.gz: 69b380ba4f103f791e2bd307d45dd9a0fb2dd3f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ad44a5e6be12700b283205746f62557b84e79aacddaa310c5132d15d12c799d4ad3ddbe9cfed39f8af49e877e128db6602c3381cb08a953df3abe3406e49e10
|
7
|
+
data.tar.gz: aae6886bea436036391dad5370ca83f69590fe88e0cdbd22c4dfcfac2c57eaab8b5fce2b999ca4688113c610fcf4fe5ce0a0a90a4f308e13026de97945f4599c
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -170,6 +170,15 @@ end
|
|
170
170
|
|
171
171
|
Now, the ```Category``` model will keep an up-to-date counter-cache in the ```products_count``` column of the ```categories``` table. Each product will affect the counts of both its immediate category and that category's parent. This will work with any number of levels.
|
172
172
|
|
173
|
+
### Updating timestamps when counts change
|
174
|
+
|
175
|
+
By default, counter_culture does not update the timestamp of models when it updates their counter caches. If you would like every change in the counter cache column to result in an updated timestamp, simply set the touch option to true like so:
|
176
|
+
```ruby
|
177
|
+
counter_culture :category, :touch => true
|
178
|
+
```
|
179
|
+
|
180
|
+
This can be useful when you use Rails' caching mechanism and display a counter cache's value in the cached fragment.
|
181
|
+
|
173
182
|
### Manually populating counter cache values
|
174
183
|
|
175
184
|
You will sometimes want to populate counter-cache values from primary data. This is required when adding counter-caches to existing data. It is also recommended to run this regularly (at BestVendor, we run it once a week) to catch any incorrect values in the counter caches.
|
@@ -224,7 +233,7 @@ end
|
|
224
233
|
|
225
234
|
#### Handling over-written, dynamic foreign keys
|
226
235
|
|
227
|
-
Manually populating counter caches with
|
236
|
+
Manually populating counter caches with dynamically over-written foreign keys (```:foreign_key_values``` option) is not supported. You will have to write code to handle this case yourself.
|
228
237
|
|
229
238
|
#### Polymorphic associations
|
230
239
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.16
|
data/counter_culture.gemspec
CHANGED
data/lib/counter_culture.rb
CHANGED
@@ -31,7 +31,8 @@ module CounterCulture
|
|
31
31
|
:counter_cache_name => (options[:column_name] || "#{name.tableize}_count"),
|
32
32
|
:column_names => options[:column_names],
|
33
33
|
:delta_column => options[:delta_column],
|
34
|
-
:foreign_key_values => options[:foreign_key_values]
|
34
|
+
:foreign_key_values => options[:foreign_key_values],
|
35
|
+
:touch => options[:touch]
|
35
36
|
}
|
36
37
|
end
|
37
38
|
|
@@ -275,10 +276,23 @@ module CounterCulture
|
|
275
276
|
end
|
276
277
|
execute_after_commit do
|
277
278
|
# increment or decrement?
|
278
|
-
|
279
|
+
operator = options[:increment] ? '+' : '-'
|
280
|
+
|
281
|
+
# we don't use Rails' update_counters because we support changing the timestamp
|
282
|
+
quoted_column = connection.quote_column_name(options[:counter_column])
|
283
|
+
|
284
|
+
updates = []
|
285
|
+
# this updates the actual counter
|
286
|
+
updates << "#{quoted_column} = COALESCE(#{quoted_column}, 0) #{operator} #{delta_magnitude}"
|
287
|
+
# and here we update the timestamp, if so desired
|
288
|
+
if options[:touch]
|
289
|
+
current_time = current_time_from_proper_timezone
|
290
|
+
timestamp_attributes_for_update_in_model.each do |timestamp_column|
|
291
|
+
updates << "#{timestamp_column} = '#{current_time.to_formatted_s(:sql)}'"
|
292
|
+
end
|
293
|
+
end
|
279
294
|
|
280
|
-
|
281
|
-
relation_klass(options[:relation]).update_counters(id_to_change, options[:counter_column] => delta)
|
295
|
+
relation_klass(options[:relation]).where(self.class.primary_key => id_to_change).update_all updates.join(', ')
|
282
296
|
end
|
283
297
|
end
|
284
298
|
end
|
@@ -1098,6 +1098,20 @@ describe "CounterCulture" do
|
|
1098
1098
|
user.reload
|
1099
1099
|
user.review_value_sum.round(1).should == 0
|
1100
1100
|
end
|
1101
|
+
|
1102
|
+
it "should update the timestamp if touch: true is set" do
|
1103
|
+
user = User.create
|
1104
|
+
product = Product.create
|
1105
|
+
|
1106
|
+
sleep 1
|
1107
|
+
|
1108
|
+
review = Review.create :user_id => user.id, :product_id => product.id
|
1109
|
+
|
1110
|
+
user.reload; product.reload
|
1111
|
+
|
1112
|
+
user.created_at.to_i.should == user.updated_at.to_i
|
1113
|
+
product.created_at.to_i.should < product.updated_at.to_i
|
1114
|
+
end
|
1101
1115
|
|
1102
1116
|
describe "#previous_model" do
|
1103
1117
|
let(:user){User.create :name => "John Smith", :manages_company_id => 1}
|
data/spec/models/review.rb
CHANGED
@@ -2,7 +2,7 @@ class Review < ActiveRecord::Base
|
|
2
2
|
belongs_to :user
|
3
3
|
belongs_to :product
|
4
4
|
|
5
|
-
counter_culture :product
|
5
|
+
counter_culture :product, :touch => true
|
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'}
|
data/spec/schema.rb
CHANGED
@@ -21,6 +21,8 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
|
|
21
21
|
t.integer "tried_count", :default => 0, :null => false
|
22
22
|
t.integer "managers_count", :default => 0, :null => false
|
23
23
|
t.integer "review_approvals_count", :default => 0, :null => false
|
24
|
+
t.datetime "created_at"
|
25
|
+
t.datetime "updated_at"
|
24
26
|
end
|
25
27
|
|
26
28
|
create_table "industries", :force => true do |t|
|
@@ -30,6 +32,8 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
|
|
30
32
|
t.integer "using_count", :default => 0, :null => false
|
31
33
|
t.integer "tried_count", :default => 0, :null => false
|
32
34
|
t.integer "review_approvals_count", :default => 0, :null => false
|
35
|
+
t.datetime "created_at"
|
36
|
+
t.datetime "updated_at"
|
33
37
|
end
|
34
38
|
|
35
39
|
create_table "products", :force => true do |t|
|
@@ -37,6 +41,8 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
|
|
37
41
|
t.integer "reviews_count", :default => 0, :null => false
|
38
42
|
t.integer "rexiews_count", :default => 0, :null => false
|
39
43
|
t.integer "category_id"
|
44
|
+
t.datetime "created_at"
|
45
|
+
t.datetime "updated_at"
|
40
46
|
end
|
41
47
|
|
42
48
|
create_table "reviews", :force => true do |t|
|
@@ -46,6 +52,8 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
|
|
46
52
|
t.integer "product_id"
|
47
53
|
t.integer "approvals"
|
48
54
|
t.float "value"
|
55
|
+
t.datetime "created_at"
|
56
|
+
t.datetime "updated_at"
|
49
57
|
end
|
50
58
|
|
51
59
|
create_table "users", :force => true do |t|
|
@@ -58,26 +66,36 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
|
|
58
66
|
t.integer "review_approvals_count", :default => 0, :null => false
|
59
67
|
t.string "has_string_id_id"
|
60
68
|
t.float "review_value_sum", :default => 0.0, :null => false
|
69
|
+
t.datetime "created_at"
|
70
|
+
t.datetime "updated_at"
|
61
71
|
end
|
62
72
|
|
63
73
|
create_table "categories", :force => true do |t|
|
64
74
|
t.string "name"
|
65
75
|
t.integer "products_count", :default => 0, :null => false
|
76
|
+
t.datetime "created_at"
|
77
|
+
t.datetime "updated_at"
|
66
78
|
end
|
67
79
|
|
68
80
|
create_table "has_string_ids", :force => true, :id => false do |t|
|
69
81
|
t.string "id", :default => '', :null => false
|
70
82
|
t.string "something"
|
71
83
|
t.integer "users_count", :null => false, :default => 0
|
84
|
+
t.datetime "created_at"
|
85
|
+
t.datetime "updated_at"
|
72
86
|
end
|
73
87
|
add_index "has_string_ids", :id, :unique => true
|
74
88
|
|
75
89
|
create_table "simple_mains", :force => true do |t|
|
76
90
|
t.integer "simple_dependents_count", :null => false, :default => 0
|
91
|
+
t.datetime "created_at"
|
92
|
+
t.datetime "updated_at"
|
77
93
|
end
|
78
94
|
|
79
95
|
create_table "simple_dependents", :force => true do |t|
|
80
96
|
t.integer "simple_main_id"
|
97
|
+
t.datetime "created_at"
|
98
|
+
t.datetime "updated_at"
|
81
99
|
end
|
82
100
|
|
83
101
|
end
|