support_table_cache 1.0.0 → 1.0.1

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: 6c7814572a411ad62d5077cf255ff24059d2ab9292be81ded041de7ecff8a6ac
4
- data.tar.gz: 015ea25fb5083ce1a80579a143486bf44a091fa375bf0d122b0889763321852c
3
+ metadata.gz: f7874d2e1669bb98537be4230846cc44862ebce2df16201d319926607a478c4b
4
+ data.tar.gz: 6f9c7bd2737b23689f487bbee803a0ce7aa592a3bde8357dab98bcd8fafa5612
5
5
  SHA512:
6
- metadata.gz: c4a1f00fa0873f0e28501119c5b588a6df166f5f9bcee84fcaf3dc4ffe0985251a6d6e6ee24f00d2ee740bd02b1dfe74db425a7a8170d8d5ddd98fbed0c8ecde
7
- data.tar.gz: 81de8cda7452731a341e82752f3a25ab793c1b15e518c41c78118348841b41f5f8c56af13304e2565e9bfeffc00210764bf0351e4920d56af0e4e49704fdfde7
6
+ metadata.gz: f322815aac9b0074d75a148c9d6b05ddb2ecb044fdae5d80b80bfca1bc409b70820b754fbecc83ddfe368da7f01872fde0b2eec724c328d1077759b2587aec70
7
+ data.tar.gz: 14733e659226be252601d4705e92248ac8377b21b0e301b6cb4b621f56731aad173c2ad01febb64a51f7051690de31b83ecc36fe69cfa4545806aebe94b38c52
data/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## 1.0.1
8
+
9
+ ### Added
10
+ - Preserve scope on relations terminated with a `find_by`.
11
+
7
12
  ## 1.0.0
8
13
 
9
14
  ### Added
data/README.md CHANGED
@@ -36,6 +36,9 @@ To use the gem, you need to include it in you models and then specify which attr
36
36
  # Uses cache on a composite key
37
37
  MyModel.find_by(group: "first", name: "One")
38
38
 
39
+ # Uses cache on a composite key with scoping
40
+ MyModel.where(group: "first").find_by(name: "One")
41
+
39
42
  # Does not use cache since value is not defined as a cacheable key
40
43
  MyModel.find_by(value: 1)
41
44
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
@@ -10,6 +10,10 @@ module SupportTableCache
10
10
  class_attribute :support_table_cache_by_attributes, instance_accessor: false
11
11
  class_attribute :support_table_cache_ttl, instance_accessor: false
12
12
 
13
+ unless ActiveRecord::Relation.include?(RelationOverride)
14
+ ActiveRecord::Relation.prepend(RelationOverride)
15
+ end
16
+
13
17
  class << self
14
18
  prepend FindByOverride unless include?(FindByOverride)
15
19
  private :support_table_cache_by_attributes=
@@ -105,7 +109,12 @@ module SupportTableCache
105
109
 
106
110
  cache_key = nil
107
111
  attributes = args.first if args.size == 1 && args.first.is_a?(Hash)
108
- if attributes
112
+
113
+ if respond_to?(:scope_attributes) && scope_attributes.present?
114
+ attributes = scope_attributes.merge(attributes || {})
115
+ end
116
+
117
+ if attributes.present?
109
118
  support_table_cache_by_attributes.each do |attribute_names, case_sensitive|
110
119
  cache_key = SupportTableCache.cache_key(self, attributes, attribute_names, case_sensitive)
111
120
  break if cache_key
@@ -120,6 +129,35 @@ module SupportTableCache
120
129
  end
121
130
  end
122
131
 
132
+ module RelationOverride
133
+ # Override for the find_by method that looks in the cache first.
134
+ def find_by(*args)
135
+ return super unless klass.include?(SupportTableCache)
136
+ return super if SupportTableCache.cache.nil? || SupportTableCache.disabled?
137
+
138
+ cache_key = nil
139
+ attributes = args.first if args.size == 1 && args.first.is_a?(Hash)
140
+
141
+ # Apply any attributes from the current relation chain
142
+ if scope_attributes.present?
143
+ attributes = scope_attributes.merge(attributes || {})
144
+ end
145
+
146
+ if attributes.present?
147
+ support_table_cache_by_attributes.each do |attribute_names, case_sensitive|
148
+ cache_key = SupportTableCache.cache_key(klass, attributes, attribute_names, case_sensitive)
149
+ break if cache_key
150
+ end
151
+ end
152
+
153
+ if cache_key
154
+ SupportTableCache.cache.fetch(cache_key, expires_in: support_table_cache_ttl) { super }
155
+ else
156
+ super
157
+ end
158
+ end
159
+ end
160
+
123
161
  # Remove the cache entry for this record.
124
162
  # @return [void]
125
163
  def uncache
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: support_table_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-16 00:00:00.000000000 Z
11
+ date: 2022-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord