counter_cache-rails 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a0177b69e46bfbf965db0bcada7ad01d3a28dc21
4
- data.tar.gz: 5012ff36ad9de6c329754120b63ac3766793e352
3
+ metadata.gz: 4e366d2e9ba9c2ea381ea543695f11ff9aac9651
4
+ data.tar.gz: 30a079055c0aea510cf06d2b62a7862fc67ee5d9
5
5
  SHA512:
6
- metadata.gz: b0f2fe3906b8fc859bc86760b321b8ffd2af53487930229ff5b1838b6cc971f95239f0514d9134ddbe0426185f6d2d9d7a8876ac034e2475603064bfb3fbfe59
7
- data.tar.gz: 526ada510c6987f8b7a177e8444c7c58af18eddd193b7c37da2e8cf92b9946aad356e2dac0f92f0d076b06caabdc03a4b77705515c28ddfb79eec11e64085c5a
6
+ metadata.gz: ac4740a77360d9976cff2745658687a7e0ea80b975e281405b310b24453dbcdbea6ff637c787f2e3f4bfff7a38b95c13c23399ff3720d31727e765875ae68684
7
+ data.tar.gz: 14ada42e851b55324d5cce8c908884c6245b40470c51e81d04fe02da0a3f75afdad6e04b22dce1b5c40317b2bbd6c92589681ce0cec862dad0e4d2e6cb3c7820
data/README.md CHANGED
@@ -23,6 +23,18 @@ class Post
23
23
  after_update_comments_count do
24
24
  # you can use callbacks on update counter cache
25
25
  end
26
+
27
+ after_increment_comments_count do
28
+ Redis.current.increment('service:total:comments')
29
+ end
30
+
31
+ after_decrement_comments_count do
32
+ Redis.current.decrement('service:total:comments')
33
+ end
34
+
35
+ after_cache_comments_count do
36
+ update(comments_count: comments_count)
37
+ end
26
38
  end
27
39
 
28
40
  class Comment
@@ -17,37 +17,46 @@ module CounterCacheRails
17
17
  child_model_class = tableized_model.to_s.classify.constantize
18
18
  tableized_child_model = tableized_model.to_sym
19
19
  primary_key = self.primary_key.to_sym
20
- callback_name = "update_#{tableized_child_model}_count".to_sym
21
20
 
22
- define_model_callbacks callback_name
21
+ callback_names = %i(update increment decrement cache).each_with_object({}) do |name, hash|
22
+ callback_name = "#{name}_#{tableized_child_model}_count".to_sym
23
+ hash[name] = callback_name
24
+ define_model_callbacks callback_name
25
+ end
23
26
 
24
27
  define_method "#{tableized_child_model}_count" do |force: false|
25
28
  count = Rails.cache.read(_counter_cache_key(class_name, primary_key, tableized_child_model), raw: true)
26
29
 
27
30
  if count.nil? || force
28
- scope = options[:scope] ? options[:scope].call(self.send(tableized_child_model)) : self.send(tableized_child_model)
29
- count = scope.count
30
-
31
- Rails.cache.write(
32
- self._counter_cache_key(class_name, primary_key, tableized_child_model),
33
- count,
34
- raw: true,
35
- expires_in: CounterCacheRails.configuration.expires_in,
36
- )
31
+ run_callbacks callback_names[:cache] do
32
+ scope = options[:scope] ? options[:scope].call(self.send(tableized_child_model)) : self.send(tableized_child_model)
33
+ count = scope.count
34
+
35
+ Rails.cache.write(
36
+ self._counter_cache_key(class_name, primary_key, tableized_child_model),
37
+ count,
38
+ raw: true,
39
+ expires_in: CounterCacheRails.configuration.expires_in,
40
+ )
41
+ end
37
42
  end
38
43
 
39
44
  count.to_i
40
45
  end
41
46
 
42
47
  define_method "_#{tableized_child_model}_count_incr" do
43
- run_callbacks callback_name do
44
- Rails.cache.increment(_counter_cache_key(class_name, primary_key, tableized_child_model))
48
+ run_callbacks callback_names[:update] do
49
+ run_callbacks callback_names[:increment] do
50
+ Rails.cache.increment(_counter_cache_key(class_name, primary_key, tableized_child_model))
51
+ end
45
52
  end
46
53
  end
47
54
 
48
55
  define_method "_#{tableized_child_model}_count_decr" do
49
- run_callbacks callback_name do
50
- Rails.cache.decrement(_counter_cache_key(class_name, primary_key, tableized_child_model))
56
+ run_callbacks callback_names[:update] do
57
+ run_callbacks callback_names[:decrement] do
58
+ Rails.cache.decrement(_counter_cache_key(class_name, primary_key, tableized_child_model))
59
+ end
51
60
  end
52
61
  end
53
62
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CounterCacheRails
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: counter_cache-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shota Iguchi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-16 00:00:00.000000000 Z
11
+ date: 2018-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -256,7 +256,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
256
256
  version: '0'
257
257
  requirements: []
258
258
  rubyforge_project:
259
- rubygems_version: 2.6.11
259
+ rubygems_version: 2.6.14
260
260
  signing_key:
261
261
  specification_version: 4
262
262
  summary: ActiveRecord extentions for counter_cache.