counter_cache-rails 0.2.0 → 0.3.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 +4 -4
- data/README.md +4 -1
- data/lib/counter_cache-rails.rb +1 -0
- data/lib/counter_cache_rails.rb +14 -0
- data/lib/counter_cache_rails/active_record_extention.rb +27 -9
- data/lib/counter_cache_rails/configuration.rb +11 -0
- data/lib/counter_cache_rails/railtie.rb +1 -0
- data/lib/counter_cache_rails/version.rb +2 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0177b69e46bfbf965db0bcada7ad01d3a28dc21
|
4
|
+
data.tar.gz: 5012ff36ad9de6c329754120b63ac3766793e352
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0f2fe3906b8fc859bc86760b321b8ffd2af53487930229ff5b1838b6cc971f95239f0514d9134ddbe0426185f6d2d9d7a8876ac034e2475603064bfb3fbfe59
|
7
|
+
data.tar.gz: 526ada510c6987f8b7a177e8444c7c58af18eddd193b7c37da2e8cf92b9946aad356e2dac0f92f0d076b06caabdc03a4b77705515c28ddfb79eec11e64085c5a
|
data/README.md
CHANGED
@@ -15,7 +15,10 @@ gem 'counter_cache-rails'
|
|
15
15
|
```rb
|
16
16
|
class Post
|
17
17
|
has_many :comments
|
18
|
-
|
18
|
+
|
19
|
+
counter_cache :comments,
|
20
|
+
if: ->(comment) { comment.visible? },
|
21
|
+
scope: ->(comments) { comments.visible }
|
19
22
|
|
20
23
|
after_update_comments_count do
|
21
24
|
# you can use callbacks on update counter cache
|
data/lib/counter_cache-rails.rb
CHANGED
data/lib/counter_cache_rails.rb
CHANGED
@@ -1,2 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'counter_cache_rails/configuration'
|
2
4
|
require 'counter_cache_rails/railtie'
|
5
|
+
|
6
|
+
module CounterCacheRails
|
7
|
+
class << self
|
8
|
+
def configuration
|
9
|
+
@configuration ||= Configuration.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def configure
|
13
|
+
yield configuration
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -1,28 +1,38 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module CounterCacheRails
|
3
4
|
module ActiveRecordExtention
|
4
5
|
extend ActiveSupport::Concern
|
5
6
|
|
6
7
|
included do
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
# Enable counter cache for a child association
|
9
|
+
#
|
10
|
+
# @param tableized_model [Symbol] Name of the child association
|
11
|
+
# @param options [Hash]
|
12
|
+
# @option options [Proc] :if
|
13
|
+
# @option options [Proc] :unless
|
14
|
+
# @option options [Proc] :scope
|
15
|
+
def self.counter_cache(tableized_model, options = {})
|
16
|
+
class_name = self.to_s.downcase
|
17
|
+
child_model_class = tableized_model.to_s.classify.constantize
|
10
18
|
tableized_child_model = tableized_model.to_sym
|
11
|
-
primary_key
|
12
|
-
callback_name
|
19
|
+
primary_key = self.primary_key.to_sym
|
20
|
+
callback_name = "update_#{tableized_child_model}_count".to_sym
|
13
21
|
|
14
22
|
define_model_callbacks callback_name
|
15
23
|
|
16
24
|
define_method "#{tableized_child_model}_count" do |force: false|
|
17
|
-
|
18
25
|
count = Rails.cache.read(_counter_cache_key(class_name, primary_key, tableized_child_model), raw: true)
|
19
26
|
|
20
27
|
if count.nil? || force
|
21
|
-
|
28
|
+
scope = options[:scope] ? options[:scope].call(self.send(tableized_child_model)) : self.send(tableized_child_model)
|
29
|
+
count = scope.count
|
30
|
+
|
22
31
|
Rails.cache.write(
|
23
32
|
self._counter_cache_key(class_name, primary_key, tableized_child_model),
|
24
33
|
count,
|
25
|
-
raw: true
|
34
|
+
raw: true,
|
35
|
+
expires_in: CounterCacheRails.configuration.expires_in,
|
26
36
|
)
|
27
37
|
end
|
28
38
|
|
@@ -42,19 +52,27 @@ module CounterCacheRails
|
|
42
52
|
end
|
43
53
|
|
44
54
|
after_create do
|
55
|
+
scope = options[:scope] ? options[:scope].call(self.send(tableized_child_model)) : self.send(tableized_child_model)
|
56
|
+
|
45
57
|
Rails.cache.write(
|
46
58
|
self._counter_cache_key(class_name, primary_key, tableized_child_model),
|
47
|
-
|
59
|
+
scope.count,
|
48
60
|
raw: true
|
49
61
|
)
|
50
62
|
end
|
51
63
|
|
52
64
|
child_model_class.class_eval do
|
53
65
|
after_create do
|
66
|
+
next unless options[:if].nil? || options[:if].call(self)
|
67
|
+
next if !options[:unless].nil? && options[:unless].call(self)
|
68
|
+
|
54
69
|
self.send(class_name.to_sym).send("_#{tableized_child_model}_count_incr")
|
55
70
|
end
|
56
71
|
|
57
72
|
after_destroy do
|
73
|
+
next unless options[:if].nil? || options[:if].call(self)
|
74
|
+
next if !options[:unless].nil? && options[:unless].call(self)
|
75
|
+
|
58
76
|
self.send(class_name.to_sym).send("_#{tableized_child_model}_count_decr")
|
59
77
|
end
|
60
78
|
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.
|
4
|
+
version: 0.3.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-
|
11
|
+
date: 2018-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -233,6 +233,7 @@ files:
|
|
233
233
|
- lib/counter_cache-rails.rb
|
234
234
|
- lib/counter_cache_rails.rb
|
235
235
|
- lib/counter_cache_rails/active_record_extention.rb
|
236
|
+
- lib/counter_cache_rails/configuration.rb
|
236
237
|
- lib/counter_cache_rails/railtie.rb
|
237
238
|
- lib/counter_cache_rails/version.rb
|
238
239
|
homepage: https://github.com/iguchi1124/counter_cache-rails
|