active_model_cachers 2.0.1 → 2.0.2

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
- SHA256:
3
- metadata.gz: 86a87d0163261f63b8c5e0275032146d7fd493afd8bdeb189a0a494f4a8c17d6
4
- data.tar.gz: 48f0a91d6dbd4694fdcc6a89560b03f11d40a72e41d65318dba9e4d394b62ea0
2
+ SHA1:
3
+ metadata.gz: 8f1049480902351ca775b8682a51f49f580d42eb
4
+ data.tar.gz: a26eef63dd1c0a799d6d91ec81b468bc27c18cc2
5
5
  SHA512:
6
- metadata.gz: d6b80679ae29bd12fa299c3bd88785489c310ed82130ecae9bbe5e8850ecd5050154651a8428eca9b4cd22d8da815b919cd803920cd1aa007a432a60ff359605
7
- data.tar.gz: b9d588a0cafdabdb49207a81fbc63f8e915570a17196e5fcecbc96924cf1cefcf661ed3bb1f90d68963e91ea768104a44bb36d02a59d2624adaf400303c65466
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(profile_id).profile
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
- if @reflect.collection?
89
- return id ? @reflect.klass.where(@reflect.foreign_key => id).to_a : []
90
- else
91
- return id ? @reflect.klass.find_by(primary_key => id) : nil
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
- get_target = ->{ @model.association(attr.column).load_target }
47
- if attr.has_one?
48
- data = get_target.call.try(primary_key)
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
- return "active_model_cachers_#{class_name}_by_#{attr.primary_key}" if attr.primary_key and attr.primary_key.to_s != 'id'
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
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ActiveModelCachers
3
- VERSION = "2.0.1"
3
+ VERSION = "2.0.2"
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.0.1
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-13 00:00:00.000000000 Z
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.7.6
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