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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 35e696be357c398d2c9bcb23bde8ea01203ddbf8
4
- data.tar.gz: 9d11a323ccdcd5e09f18e086e5cd948b0fd7bd36
3
+ metadata.gz: d255dba9072e3f8fee1a78ac3bbd9112cbe449f6
4
+ data.tar.gz: 69b380ba4f103f791e2bd307d45dd9a0fb2dd3f2
5
5
  SHA512:
6
- metadata.gz: d87125c4933ac012ac93e5084079c86b85d1ad2420e04245ecb414f3cd4bb56d9e37b38404270b77061028b067545c0ec15cbf5dbb05ea9ed45942890eabdafe
7
- data.tar.gz: a72a586a7ddd60204413200b3897b64bd9ec263dcfe5a0ddd4be525aa68404775e59a3a747724757682f29caf00e3bc304233b9c143b9fc5073f4e288a8e650d
6
+ metadata.gz: 7ad44a5e6be12700b283205746f62557b84e79aacddaa310c5132d15d12c799d4ad3ddbe9cfed39f8af49e877e128db6602c3381cb08a953df3abe3406e49e10
7
+ data.tar.gz: aae6886bea436036391dad5370ca83f69590fe88e0cdbd22c4dfcfac2c57eaab8b5fce2b999ca4688113c610fcf4fe5ce0a0a90a4f308e13026de97945f4599c
@@ -1,3 +1,8 @@
1
+ ## 0.1.16 (October 5, 2013)
2
+
3
+ Features:
4
+ - Added support for touch option that updates timestamps when updating counter caches
5
+
1
6
  ## 0.1.15 (October 5, 2013)
2
7
 
3
8
  Features:
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 dynamicall over-written foreign keys (```:foreign_key_values``` option) is not supported. You will have to write code to handle this case yourself.
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.15
1
+ 0.1.16
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "counter_culture"
8
- s.version = "0.1.15"
8
+ s.version = "0.1.16"
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"]
@@ -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
- delta = options[:increment] ? delta_magnitude : -delta_magnitude
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
- # do it!
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}
@@ -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'}
@@ -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
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.15
4
+ version: 0.1.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Magnus von Koeller