counter_cache-rails 0.1.1 → 0.2.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: 03cf0fd33208fa49fc9fa623abb5230f24108576
4
- data.tar.gz: 5fc3c2a22b8df101664830c5ca31df0952803904
3
+ metadata.gz: 0a301bec859d9975f7f3ae56d2cb8cdec25eaf88
4
+ data.tar.gz: 1c6fd54aaeb967af245952fda02c38664f9f389e
5
5
  SHA512:
6
- metadata.gz: 683493a27287595e7107a715f2e800d4bfcdc6826983ab3edef454522f49fd4c6834f535acbfd005cade2c2d62f8a59403695344c3828b45b575834816ba1a2f
7
- data.tar.gz: 9bcc412315e32a7f6d3f2728a475840996fb1702f4148015c1a203cee832218726fdf4dd91b69c0d126c3f5138b08dc4eaf5d042fb360552813a1c92fd13f1ed
6
+ metadata.gz: 8b3f71843d81520b28fa6c181896b59bf92dd74bff620e84f88a5d762c127d1c519f0c59d4fee2752075ba592dd31aed03ccf2b65cc75077433ad40f58adfa42
7
+ data.tar.gz: da0a571830ded5a810330aab215c9010bace1d5201a052041081bf7a0e8df1895fa200223de9b7e811e1fd15b40bad10fdd576a3cc05380446f3b7bbbfc8ba19
data/README.md CHANGED
@@ -16,6 +16,10 @@ gem 'counter_cache-rails'
16
16
  class Post
17
17
  has_many :comments
18
18
  counter_cache :comments
19
+
20
+ after_update_comments_count do
21
+ # you can use callbacks on update counter cache
22
+ end
19
23
  end
20
24
 
21
25
  class Comment
@@ -27,6 +31,8 @@ post.comments_count # => 0
27
31
 
28
32
  post.comments.create(body: 'comment body')
29
33
  post.comments_count # => 1
34
+
35
+ post.comments_count(force: true) # force reload counter cache
30
36
  ```
31
37
 
32
38
  ## License
@@ -6,14 +6,18 @@ module CounterCacheRails
6
6
  included do
7
7
  def self.counter_cache(tableized_model)
8
8
  class_name = self.to_s.downcase
9
- child_model_class = eval tableized_model.to_s.classify
9
+ child_model_class = tableized_model.to_s.classify.constantize
10
10
  tableized_child_model = tableized_model.to_sym
11
11
  primary_key = self.primary_key.to_sym
12
+ callback_name = "update_#{tableized_child_model}_count".to_sym
13
+
14
+ define_model_callbacks callback_name
15
+
16
+ define_method "#{tableized_child_model}_count" do |force: false|
12
17
 
13
- define_method "#{tableized_child_model}_count" do
14
18
  count = Rails.cache.read(_counter_cache_key(class_name, primary_key, tableized_child_model), raw: true)
15
19
 
16
- if count.nil?
20
+ if count.nil? || force
17
21
  count = self.send(tableized_child_model).count
18
22
  Rails.cache.write(
19
23
  self._counter_cache_key(class_name, primary_key, tableized_child_model),
@@ -26,11 +30,15 @@ module CounterCacheRails
26
30
  end
27
31
 
28
32
  define_method "_#{tableized_child_model}_count_incr" do
29
- Rails.cache.increment(_counter_cache_key(class_name, primary_key, tableized_child_model))
33
+ run_callbacks callback_name do
34
+ Rails.cache.increment(_counter_cache_key(class_name, primary_key, tableized_child_model))
35
+ end
30
36
  end
31
37
 
32
38
  define_method "_#{tableized_child_model}_count_decr" do
33
- Rails.cache.decrement(_counter_cache_key(class_name, primary_key, tableized_child_model))
39
+ run_callbacks callback_name do
40
+ Rails.cache.decrement(_counter_cache_key(class_name, primary_key, tableized_child_model))
41
+ end
34
42
  end
35
43
 
36
44
  after_create do
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module CounterCacheRails
3
- VERSION = '0.1.1'
3
+ VERSION = '0.2.0'
4
4
  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.1.1
4
+ version: 0.2.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: 2017-11-22 00:00:00.000000000 Z
11
+ date: 2018-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -235,7 +235,7 @@ files:
235
235
  - lib/counter_cache_rails/active_record_extention.rb
236
236
  - lib/counter_cache_rails/railtie.rb
237
237
  - lib/counter_cache_rails/version.rb
238
- homepage: https://github.com/iguchi1124
238
+ homepage: https://github.com/iguchi1124/counter_cache-rails
239
239
  licenses:
240
240
  - MIT
241
241
  metadata: {}