active_model_cachers 2.1.2 → 2.1.3

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
  SHA256:
3
- metadata.gz: 26cbc738077c571fd681dba6f83be1faf69672e52f6d21ef242ff1bec6d7b8d1
4
- data.tar.gz: efa2374d6689b0c2fc1c2738293053ecfcc67720840f53ec7ef789c6a84543b3
3
+ metadata.gz: 22fbf696d9d01bea8d59d1c885ef7e0724bc46894d263ae0bd59023fcbf3d034
4
+ data.tar.gz: f4b2166e69e896992e3fe0352201473faec0050f40253ace399a64af35ff8e71
5
5
  SHA512:
6
- metadata.gz: 179179ebf0b2b926963bdfc64392b65c952c5246cfdf4475196bfb9f5af6d2e8996f2c686bc33d031b3ef579c3736a68055e558363df991bb9708275f129f187
7
- data.tar.gz: 21bac3df913b18a496c926816d2553c6fb84248d912b6a7861e41f03a37a82059ae86e14264f3f67fbc6bb23894e8bfd3514021d3b14d4d1d1af03c28907eafd
6
+ metadata.gz: 2c397c8098e6190ee501b91794bdadde52d561560cb1916c45a1da3ae60b24c1b7137b7cb0ebfe49ca5d9dc760e7052345dc4d9930e53face0be7e57bc831ad4
7
+ data.tar.gz: 2ddab829f8dbe887e5757d3a674a9468b9811fe0f363a27bf548d55a7b8b5ffd22c0e05e41f49e0e6a41e6277fd6480f8c22fd1627ed7e4f7c8d7cf08163e986
@@ -1,5 +1,8 @@
1
1
  ## Change Log
2
2
 
3
+ ### [v2.1.2](https://github.com/khiav223577/active_model_cachers/compare/v2.1.1...v2.1.2) 2018/06/01
4
+ - [#37](https://github.com/khiav223577/active_model_cachers/pull/37) Fix: ModelName cant be referred to in development (@khiav223577)
5
+
3
6
  ### [v2.1.1](https://github.com/khiav223577/active_model_cachers/compare/v2.1.0...v2.1.1) 2018/05/25
4
7
  - [#35](https://github.com/khiav223577/active_model_cachers/pull/35) Preventing registering same callbacks in some cases (@khiav223577)
5
8
  - [#34](https://github.com/khiav223577/active_model_cachers/pull/34) Enhance - automatically clean cache when `#touch` (@ff2248)
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ require 'active_model_cachers/active_record/global_callbacks'
2
3
  require 'active_model_cachers/active_record/attr_model'
3
4
  require 'active_model_cachers/active_record/cacher'
4
5
  require 'active_model_cachers/hook/dependencies'
@@ -83,6 +84,19 @@ module ActiveModelCachers
83
84
  service_klasses << cache_self
84
85
  end
85
86
  end
87
+
88
+ @global_callbacks = GlobalCallbacks.new
89
+ def self.global_callbacks
90
+ @global_callbacks
91
+ end
92
+
93
+ def self.extended(base)
94
+ global_callbacks = @global_callbacks
95
+ base.instance_exec do
96
+ after_commit ->{ global_callbacks.after_commit.exec(self, self.class) }
97
+ after_touch ->{ global_callbacks.after_touch.exec(self, self.class) }
98
+ end
99
+ end
86
100
  end
87
101
  end
88
102
  end
@@ -0,0 +1,59 @@
1
+ module ActiveModelCachers
2
+ module ActiveRecord
3
+ class GlobalCallbacks
4
+ def initialize
5
+ @type_callbacks = {}
6
+ end
7
+
8
+ def pre_before_delete(class_name = nil, &block)
9
+ define_callbacks(:pre_before_delete, class_name, &block)
10
+ end
11
+
12
+ def before_delete(class_name = nil, &block)
13
+ define_callbacks(:before_delete, class_name, &block)
14
+ end
15
+
16
+ def after_delete(class_name = nil, &block)
17
+ define_callbacks(:after_delete, class_name, &block)
18
+ end
19
+
20
+ def on_nullify(class_name = nil, &block)
21
+ define_callbacks(:on_nullify, class_name, &block)
22
+ end
23
+
24
+ def after_commit(class_name = nil, &block)
25
+ define_callbacks(:after_commit, class_name, &block)
26
+ end
27
+
28
+ def after_touch(class_name = nil, &block)
29
+ define_callbacks(:after_touch, class_name, &block)
30
+ end
31
+
32
+ private
33
+
34
+ def define_callbacks(type, class_name, &block)
35
+ (@type_callbacks[type] ||= ClassCallbacks.new).tap do |s|
36
+ s.add_callback(class_name, &block) if class_name
37
+ end
38
+ end
39
+ end
40
+
41
+ class ClassCallbacks
42
+ def initialize
43
+ @class_callbacks = Hash.new{|h, k| h[k] = [] }
44
+ end
45
+
46
+ def callbacks_at(class_name)
47
+ @class_callbacks[class_name]
48
+ end
49
+
50
+ def add_callback(class_name, &block)
51
+ callbacks_at(class_name) << block
52
+ end
53
+
54
+ def exec(scope, klass, *args)
55
+ callbacks_at(klass.name).each{|s| scope.instance_exec(*args, &s) }
56
+ end
57
+ end
58
+ end
59
+ end
@@ -6,22 +6,15 @@ module ActiveModelCachers
6
6
  def find_by(*args)
7
7
  where(*args).order('').first
8
8
  end
9
+ end
10
+ end
11
+ end
9
12
 
10
- # after_commit in Rails 3 cannot specify multiple :on
11
- # EX:
12
- # after_commit ->{ ... }, on: [:create, :destroy]
13
- #
14
- # Should rewrite it as:
15
- # after_commit ->{ ... }, on: :create
16
- # after_commit ->{ ... }, on: :destroy
17
-
18
- def after_commit(*args, &block) # mass-assign protected attributes `id` In Rails 3
19
- if args.last.is_a?(Hash)
20
- if (on = args.last[:on]).is_a?(Array)
21
- return on.each{|s| after_commit(*[*args[0...-1], { **args[-1], on: s }], &block) }
22
- end
23
- end
24
- super
13
+ module ActiveRecord
14
+ module Transactions
15
+ if not method_defined?(:transaction_include_any_action?)
16
+ def transaction_include_any_action?(fire_on)
17
+ fire_on.any?{|s| transaction_include_action?(s) }
25
18
  end
26
19
  end
27
20
  end
@@ -24,31 +24,36 @@ module ActiveModelCachers
24
24
  @callbacks_defined = true
25
25
 
26
26
  clean = ->(id){ clean_at(with_id ? id : nil) }
27
+ clean_ids = []
28
+ fire_on = Array(on) if on
27
29
 
28
- ActiveSupport::Dependencies.onload(class_name) do
29
- clean_ids = []
30
+ ActiveRecord::Extension.global_callbacks.instance_exec do
31
+ on_nullify(class_name) do |nullified_column, get_ids|
32
+ get_ids.call.each{|s| clean.call(s) } if nullified_column == column
33
+ end
34
+
35
+ after_touch(class_name) do
36
+ clean.call(send(foreign_key))
37
+ end
38
+
39
+ after_commit(class_name) do # TODO: on
40
+ next if fire_on and not transaction_include_any_action?(fire_on)
41
+ changed = column ? previous_changes.key?(column) : previous_changes.present?
42
+ clean.call(send(foreign_key)) if changed || destroyed?
43
+ end
30
44
 
31
- prepend_before_delete do |id, model|
45
+ pre_before_delete(class_name) do |id, model|
32
46
  clean_ids << @@column_value_cache.add(self, class_name, id, foreign_key, model)
33
47
  end
34
48
 
35
- before_delete do |_, model|
49
+ before_delete(class_name) do |_, model|
36
50
  clean_ids.each{|s| clean.call(s.call) }
37
51
  clean_ids = []
38
52
  end
39
53
 
40
- after_delete do
54
+ after_delete(class_name) do
41
55
  @@column_value_cache.clean_cache
42
56
  end
43
-
44
- on_nullify(column){|ids| ids.each{|s| clean.call(s) }}
45
-
46
- after_commit ->{
47
- changed = column ? previous_changes.key?(column) : previous_changes.present?
48
- clean.call(send(foreign_key)) if changed || destroyed?
49
- }, on: on
50
-
51
- after_touch ->{ clean.call(send(foreign_key)) }
52
57
  end
53
58
  end
54
59
  end
@@ -27,10 +27,15 @@ module ActiveModelCachers::Hook
27
27
 
28
28
  private
29
29
 
30
- def call_hooks
31
- return if (hooks = reflection.klass.nullify_hooks_at(reflection.foreign_key)).blank?
32
- ids = yield
33
- hooks.each{|s| s.call(ids) }
30
+ def call_hooks(&get_ids)
31
+ ids = nil
32
+ get_ids_with_cache = ->{ ids ||= get_ids.call }
33
+ ActiveModelCachers::ActiveRecord::Extension.global_callbacks.on_nullify.exec(
34
+ self,
35
+ reflection.klass,
36
+ reflection.foreign_key,
37
+ get_ids_with_cache,
38
+ )
34
39
  end
35
40
  end
36
41
  end
@@ -12,40 +12,14 @@ module ActiveModelCachers::Hook
12
12
  end
13
13
 
14
14
  module ClassMethods
15
- def prepend_before_delete(&callback)
16
- before_delete_hooks.unshift(callback)
17
- end
18
-
19
- def before_delete(&callback)
20
- before_delete_hooks << callback
21
- end
22
-
23
- def after_delete(&callback)
24
- after_delete_hooks << callback
25
- end
26
-
27
- def before_delete_hooks
28
- @before_delete_hooks ||= []
29
- end
30
-
31
- def after_delete_hooks
32
- @after_delete_hooks ||= []
33
- end
34
-
35
15
  def delete(id, model = nil)
36
- before_delete_hooks.each{|s| s.call(id, model) }
37
- result = super(id)
38
- after_delete_hooks.each{|s| s.call(id, model) }
39
- return result
40
- end
16
+ ActiveModelCachers::ActiveRecord::Extension.global_callbacks.pre_before_delete.exec(self, self, id, model)
17
+ ActiveModelCachers::ActiveRecord::Extension.global_callbacks.before_delete.exec(self, self, id, model)
41
18
 
42
- def nullify_hooks_at(column)
43
- @nullify_hooks ||= Hash.new{|h, k| h[k] = [] }
44
- return @nullify_hooks[column]
45
- end
19
+ result = super(id)
46
20
 
47
- def on_nullify(column, &callback)
48
- nullify_hooks_at(column) << callback
21
+ ActiveModelCachers::ActiveRecord::Extension.global_callbacks.after_delete.exec(self, self, id, model)
22
+ return result
49
23
  end
50
24
  end
51
25
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ActiveModelCachers
3
- VERSION = '2.1.2'
3
+ VERSION = '2.1.3'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_cachers
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - khiav reoy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-01 00:00:00.000000000 Z
11
+ date: 2018-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -121,6 +121,7 @@ files:
121
121
  - lib/active_model_cachers/active_record/attr_model.rb
122
122
  - lib/active_model_cachers/active_record/cacher.rb
123
123
  - lib/active_model_cachers/active_record/extension.rb
124
+ - lib/active_model_cachers/active_record/global_callbacks.rb
124
125
  - lib/active_model_cachers/active_record/patch_rails_3.rb
125
126
  - lib/active_model_cachers/cache_service.rb
126
127
  - lib/active_model_cachers/cache_service_factory.rb