active_model_cachers 2.0.1 → 2.0.2
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 +5 -5
- data/README.md +4 -1
- data/lib/active_model_cachers/active_record/attr_model.rb +7 -5
- data/lib/active_model_cachers/active_record/cacher.rb +3 -5
- data/lib/active_model_cachers/active_record/extension.rb +1 -1
- data/lib/active_model_cachers/cache_service_factory.rb +2 -1
- data/lib/active_model_cachers/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8f1049480902351ca775b8682a51f49f580d42eb
|
4
|
+
data.tar.gz: a26eef63dd1c0a799d6d91ec81b468bc27c18cc2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f9dd545f78b1f6531ea09f340edcd4b00b15571825f325467745e00573f35f42688d86c6504d9953b029e0868b368094d460bf854f675a581398377e88e93f6
|
7
|
+
data.tar.gz: d2cd5b03a2ba48ee110e04fb30e5b4f2c5a41827af4f2a426b150ad3971967aac6c9cdc53e2f7c7417de06ff5b280daac24daeacc54ffd9caf199191c3582cfb
|
data/README.md
CHANGED
@@ -132,6 +132,9 @@ current_user.cacher.clean(:profile)
|
|
132
132
|
|
133
133
|
# Or calling the clean_* method
|
134
134
|
current_user.cacher.clean_profile
|
135
|
+
|
136
|
+
# clean the cache without loading model
|
137
|
+
User.cacher_at(user_id).clean_profile
|
135
138
|
```
|
136
139
|
|
137
140
|
## Smart Caching
|
@@ -166,7 +169,7 @@ end
|
|
166
169
|
@profile = current_user.cacher.profile
|
167
170
|
|
168
171
|
# directly get profile without loading user.
|
169
|
-
@profile = User.cacher_at(
|
172
|
+
@profile = User.cacher_at(user_id).profile
|
170
173
|
```
|
171
174
|
|
172
175
|
### Caching Polymorphic Associations
|
@@ -4,10 +4,11 @@ module ActiveModelCachers
|
|
4
4
|
class AttrModel
|
5
5
|
attr_reader :klass, :column, :reflect
|
6
6
|
|
7
|
-
def initialize(klass, column, primary_key: nil)
|
7
|
+
def initialize(klass, column, primary_key: nil, foreign_key: nil)
|
8
8
|
@klass = klass
|
9
9
|
@column = column
|
10
10
|
@primary_key = primary_key
|
11
|
+
@foreign_key = foreign_key
|
11
12
|
@reflect = klass.reflect_on_association(column)
|
12
13
|
end
|
13
14
|
|
@@ -48,6 +49,7 @@ module ActiveModelCachers
|
|
48
49
|
end
|
49
50
|
|
50
51
|
def foreign_key(reverse: false)
|
52
|
+
return @foreign_key if @foreign_key
|
51
53
|
return if not association?
|
52
54
|
# key may be symbol if specify foreign_key in association options
|
53
55
|
return @reflect.chain.last.foreign_key.to_s if reverse and join_table
|
@@ -85,10 +87,10 @@ module ActiveModelCachers
|
|
85
87
|
def query_association(binding, id)
|
86
88
|
return binding.association(@column).load_target if binding.is_a?(::ActiveRecord::Base)
|
87
89
|
id = @reflect.active_record.where(id: id).limit(1).pluck(foreign_key).first if foreign_key != 'id'
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
90
|
+
case
|
91
|
+
when collection? ; return id ? @reflect.klass.where(@reflect.foreign_key => id).to_a : []
|
92
|
+
when has_one? ; return id ? @reflect.klass.find_by(foreign_key(reverse: true) => id) : nil
|
93
|
+
else ; return id ? @reflect.klass.find_by(primary_key => id) : nil
|
92
94
|
end
|
93
95
|
end
|
94
96
|
end
|
@@ -43,11 +43,9 @@ module ActiveModelCachers
|
|
43
43
|
def exec_by(attr, primary_key, service_klasses, method)
|
44
44
|
bindings = [@model]
|
45
45
|
if @model and attr.association?
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
else
|
50
|
-
bindings << get_target.call if method != :clean_cache # no need to load binding when just cleaning cache
|
46
|
+
if attr.belongs_to? and method != :clean_cache # no need to load binding when just cleaning cache
|
47
|
+
association = @model.association(attr.column)
|
48
|
+
bindings << association.load_target if association.loaded?
|
51
49
|
end
|
52
50
|
end
|
53
51
|
data ||= (@model ? @model.send(primary_key) : nil) || @id
|
@@ -14,7 +14,7 @@ module ActiveModelCachers
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def cache_at(column, query = nil, expire_by: nil, on: nil, foreign_key: nil, primary_key: nil)
|
17
|
-
attr = AttrModel.new(self, column, primary_key: primary_key)
|
17
|
+
attr = AttrModel.new(self, column, foreign_key: foreign_key, primary_key: primary_key)
|
18
18
|
return cache_belongs_to(attr) if attr.belongs_to?
|
19
19
|
|
20
20
|
query ||= ->(id){ attr.query_model(self, id) }
|
@@ -31,7 +31,8 @@ module ActiveModelCachers
|
|
31
31
|
def get_cache_key(attr)
|
32
32
|
class_name, column = (attr.single_association? ? [attr.class_name, nil] : [attr.klass, attr.column])
|
33
33
|
return "active_model_cachers_#{class_name}_at_#{column}" if column
|
34
|
-
|
34
|
+
foreign_key = attr.foreign_key(reverse: true)
|
35
|
+
return "active_model_cachers_#{class_name}_by_#{foreign_key}" if foreign_key and foreign_key.to_s != 'id'
|
35
36
|
return "active_model_cachers_#{class_name}"
|
36
37
|
end
|
37
38
|
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.0.
|
4
|
+
version: 2.0.2
|
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-05-
|
11
|
+
date: 2018-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -151,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
151
151
|
version: '0'
|
152
152
|
requirements: []
|
153
153
|
rubyforge_project:
|
154
|
-
rubygems_version: 2.
|
154
|
+
rubygems_version: 2.6.13
|
155
155
|
signing_key:
|
156
156
|
specification_version: 4
|
157
157
|
summary: Let you cache whatever you want with ease by providing cachers to active
|