active_model_cachers 2.1.3 → 2.1.4

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
  SHA256:
3
- metadata.gz: 22fbf696d9d01bea8d59d1c885ef7e0724bc46894d263ae0bd59023fcbf3d034
4
- data.tar.gz: f4b2166e69e896992e3fe0352201473faec0050f40253ace399a64af35ff8e71
3
+ metadata.gz: d62a0b3c3456a59bd79c3df098c46c6259d6683576714d0744671ab397f6ae27
4
+ data.tar.gz: e8608849cdb7f0eb50f83c8a19a95c106c88e3d4a5e3ea1155bcedbcde6c4770
5
5
  SHA512:
6
- metadata.gz: 2c397c8098e6190ee501b91794bdadde52d561560cb1916c45a1da3ae60b24c1b7137b7cb0ebfe49ca5d9dc760e7052345dc4d9930e53face0be7e57bc831ad4
7
- data.tar.gz: 2ddab829f8dbe887e5757d3a674a9468b9811fe0f363a27bf548d55a7b8b5ffd22c0e05e41f49e0e6a41e6277fd6480f8c22fd1627ed7e4f7c8d7cf08163e986
6
+ metadata.gz: 0ea2eaaba63a07e27354f60f8a2790269da1fb6b2208eb6a05f1c7b0b24cffc51dcd0c593c197b9c20af26c041fbfb0c8eca8e18600468daad90ee47b6ea3b10
7
+ data.tar.gz: 8ac03f042b17d41c265fb861f7c602651df02691780c1779d9441eb906efb9fac2d89791b83e3f1daf33717aab8b35c9b4223ffa3ecc076ecfe1cc356953e56e
@@ -1,5 +1,8 @@
1
1
  ## Change Log
2
2
 
3
+ ### [v2.1.3](https://github.com/khiav223577/active_model_cachers/compare/v2.1.2...v2.1.3) 2018/06/07
4
+ - [#38](https://github.com/khiav223577/active_model_cachers/pull/38) Fix: Eager-loaded models will not register `after_commit` callback (@khiav223577)
5
+
3
6
  ### [v2.1.2](https://github.com/khiav223577/active_model_cachers/compare/v2.1.1...v2.1.2) 2018/06/01
4
7
  - [#37](https://github.com/khiav223577/active_model_cachers/pull/37) Fix: ModelName cant be referred to in development (@khiav223577)
5
8
 
@@ -78,6 +78,7 @@ module ActiveModelCachers
78
78
 
79
79
  def exec_by(attr, primary_key, service_klasses, method, data: nil)
80
80
  bindings = [@model]
81
+ reflects = (attr.belongs_to? ? [] : [attr.reflect])
81
82
  if @model and attr.association?
82
83
  if attr.belongs_to? and method != :clean_cache # no need to load binding when just cleaning cache
83
84
  association = @model.association(attr.column)
@@ -86,7 +87,7 @@ module ActiveModelCachers
86
87
  end
87
88
  data ||= (@model ? @model.send(primary_key) : nil) || @id
88
89
  service_klasses.each_with_index do |service_klass, index|
89
- data = service_klass.instance(data).send(method, binding: bindings[index])
90
+ data = service_klass.instance(data).send(method, binding: bindings[index], reflect: reflects[index])
90
91
  return if data == nil
91
92
  end
92
93
  return data
@@ -7,7 +7,7 @@ module ActiveModelCachers
7
7
  class CacheService
8
8
  class << self
9
9
  attr_accessor :cache_key
10
- attr_accessor :query
10
+ attr_accessor :query_mapping
11
11
 
12
12
  def instance(id)
13
13
  hash = (RequestStore.store[self] ||= {})
@@ -65,17 +65,17 @@ module ActiveModelCachers
65
65
  @id = id
66
66
  end
67
67
 
68
- def get(binding: nil)
69
- @cached_data ||= fetch_from_cache(binding: binding)
68
+ def get(binding: nil, reflect: nil)
69
+ @cached_data ||= fetch_from_cache(binding: binding, reflect: reflect)
70
70
  return cache_to_raw_data(@cached_data)
71
71
  end
72
72
 
73
- def peek(binding: nil)
73
+ def peek(binding: nil, reflect: nil)
74
74
  @cached_data ||= get_from_cache
75
75
  return cache_to_raw_data(@cached_data)
76
76
  end
77
77
 
78
- def clean_cache(binding: nil)
78
+ def clean_cache(binding: nil, reflect: nil)
79
79
  @cached_data = nil
80
80
  Rails.cache.delete(cache_key)
81
81
  return nil
@@ -88,8 +88,15 @@ module ActiveModelCachers
88
88
  return @id ? "#{key}_#{@id}" : key
89
89
  end
90
90
 
91
- def get_without_cache(binding)
92
- query = self.class.query
91
+ def get_query(binding, reflect)
92
+ self.class.query_mapping[reflect] || begin
93
+ puts "Warning: cannot find query. possible reflects: #{self.class.query_mapping.keys}, reflect: #{reflect}"
94
+ self.class.query_mapping.values.first
95
+ end
96
+ end
97
+
98
+ def get_without_cache(binding, attr)
99
+ query = get_query(binding, attr)
93
100
  return binding ? binding.instance_exec(@id, &query) : query.call(@id) if @id and query.parameters.size == 1
94
101
  return binding ? binding.instance_exec(&query) : query.call
95
102
  end
@@ -111,9 +118,9 @@ module ActiveModelCachers
111
118
  ActiveModelCachers.config.store.read(cache_key)
112
119
  end
113
120
 
114
- def fetch_from_cache(binding: nil)
121
+ def fetch_from_cache(binding: nil, reflect: nil)
115
122
  ActiveModelCachers.config.store.fetch(cache_key, expires_in: 30.minutes) do
116
- raw_to_cache_data(get_without_cache(binding))
123
+ raw_to_cache_data(get_without_cache(binding, reflect))
117
124
  end
118
125
  end
119
126
 
@@ -13,22 +13,22 @@ module ActiveModelCachers
13
13
  end
14
14
 
15
15
  def create_for_active_model(attr, query)
16
- create(get_cache_key(attr), query)
17
- end
16
+ cache_key = get_cache_key(attr)
18
17
 
19
- def create(cache_key, query)
20
- @key_class_mapping[cache_key] ||= ->{
18
+ klass = @key_class_mapping[cache_key] ||= ->{
21
19
  klass = Class.new(CacheService)
22
20
  klass.cache_key = cache_key
23
- klass.query = query
21
+ klass.query_mapping = {}
24
22
  klass.instance_variable_set(:@callbacks_defined, false) # to remove warning: instance variable @callbacks_defined not initialized
25
23
  next klass
26
24
  }[]
25
+
26
+ klass.query_mapping[attr.reflect] = query
27
+ return klass
27
28
  end
28
29
 
29
30
  def set_klass_to_mapping(attr, current_klass)
30
31
  cache_key = get_cache_key(attr)
31
- reflect = attr.klass.reflect_on_association(:posts)
32
32
  changed = clean_klass_cache_if_reloaded!(cache_key, current_klass, attr)
33
33
  @cache_key_klass_mapping[cache_key] = current_klass
34
34
  return changed
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ActiveModelCachers
3
- VERSION = '2.1.3'
3
+ VERSION = '2.1.4'
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.3
4
+ version: 2.1.4
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-07 00:00:00.000000000 Z
11
+ date: 2018-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler