remote_record 0.6.0 → 0.7.0

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: 8b0ea2bc5ed71919334a2a22d53f70a38ad06b116c7e76679e76bb0aeeb9ec9c
4
- data.tar.gz: 4874762ea7e61fc354e99faa8d4cf6e79ba9b1451dcf5eefb029e2c173911b13
3
+ metadata.gz: d657d0ec1a8c4606cc656d2c557e592637fa50d165cd95e118a8efa3094bc7a7
4
+ data.tar.gz: 6bd9609c2617e49b9701e84cd1011236978233856864791e7126c221228addf3
5
5
  SHA512:
6
- metadata.gz: 603cbf7ecd366f2e3ba00c0be7d1ef793a484948a4940ddae511ab7e99583620d405cee4068958099dcf44aec52bdcc0df612771fc9b9243db530b643d5f1220
7
- data.tar.gz: d5edee74efd6afd30db89d237674c5deb6953279636accc3e22b28b95e866f769631dc4a13fa70b30982e8f1422f2707bd9ccac2b0b1bdca2de5824e21e9c732
6
+ metadata.gz: 4a115d1e2e29d138e67a70c574d66cb7efca0c1ab239d9f47146d8e157410c68d700c8c4bf2c19db647d45a3831f669984117db4823b16691d53af07245ccec1
7
+ data.tar.gz: 7325785485edb863a7d5c91e10b80b8119c93c1ec8b5ff7b1f570ffd498ac297b4c1ba8e3e86549d19ba654e6a578dc2f705a0470fbc2c19aac9e6f5bf49d488
data/README.md CHANGED
@@ -231,5 +231,19 @@ You might want to force a fresh request in some instances, even if you're using
231
231
  ### Skip fetching
232
232
 
233
233
  You might not want to make a request on initialize sometimes. In this case, pass
234
- `fetching: false` to your query or `new` to make sure the resource isn't
235
- fetched.
234
+ `fetching: false` when creating or initializing references to make sure the
235
+ resource isn't fetched.
236
+
237
+ When querying for records using ActiveRecord alone, you might want to do so
238
+ within a `no_fetching` context:
239
+
240
+ ```ruby
241
+ TodoReference.no_fetching { |model| model.where(remote_resource_id: 1) }
242
+ ```
243
+
244
+ Any records initialized within a `no_fetching` context won't be requested. It's
245
+ sort of like a `Faraday` cage, pun entirely intended.
246
+
247
+ If you're using `remote_all` or `remote_where` to fetch using your API, that'll
248
+ automatically use this behind the scenes, then set `attrs` to the response
249
+ value.
@@ -41,6 +41,10 @@ module RemoteRecord
41
41
  @attrs.update(get)
42
42
  end
43
43
 
44
+ def attrs=(new_attrs)
45
+ @attrs.update(new_attrs)
46
+ end
47
+
44
48
  private
45
49
 
46
50
  def transform(data)
@@ -9,7 +9,9 @@ module RemoteRecord
9
9
  module Reference
10
10
  extend ActiveSupport::Concern
11
11
 
12
- class_methods do
12
+ class_methods do # rubocop:disable Metrics/BlockLength
13
+ attr_accessor :fetching
14
+
13
15
  def remote_record_class
14
16
  ClassLookup.new(self).remote_record_class(
15
17
  remote_record_config.to_h[:remote_record_class]&.to_s
@@ -24,15 +26,36 @@ module RemoteRecord
24
26
  Config.new
25
27
  end
26
28
 
29
+ def fetching
30
+ @fetching = true if @fetching.nil?
31
+ @fetching
32
+ end
33
+
34
+ # Disable fetching for all records initialized in the block.
35
+ def no_fetching
36
+ self.fetching = false
37
+ block_return_value = yield(self)
38
+ self.fetching = true
39
+ block_return_value
40
+ end
41
+
27
42
  def remote_all(&authz_proc)
28
- remote_record_class.all(&authz_proc).map do |remote_resource|
29
- where(remote_resource_id: remote_resource['id']).first_or_initialize(initial_attrs: remote_resource)
43
+ no_fetching do
44
+ remote_record_class.all(&authz_proc).map do |remote_resource|
45
+ where(remote_resource_id: remote_resource['id']).first_or_initialize.tap do |record|
46
+ record.attrs = remote_resource
47
+ end
48
+ end
30
49
  end
31
50
  end
32
51
 
33
52
  def remote_where(params, &authz_proc)
34
- remote_record_class.where(params, &authz_proc).map do |remote_resource|
35
- where(remote_resource_id: remote_resource['id']).first_or_initialize(initial_attrs: remote_resource)
53
+ no_fetching do
54
+ remote_record_class.where(params, &authz_proc).map do |remote_resource|
55
+ where(remote_resource_id: remote_resource['id']).first_or_initialize.tap do |record|
56
+ record.attrs = remote_resource
57
+ end
58
+ end
36
59
  end
37
60
  end
38
61
  end
@@ -40,11 +63,10 @@ module RemoteRecord
40
63
  # rubocop:disable Metrics/BlockLength
41
64
  included do
42
65
  include ActiveSupport::Rescuable
43
- attr_accessor :fetching
66
+ attribute :fetching, :boolean, default: -> { fetching }
44
67
  attr_accessor :initial_attrs
45
68
 
46
69
  after_initialize do |reference|
47
- reference.fetching = true if reference.fetching.nil?
48
70
  reference.fetching = false if reference.initial_attrs.present?
49
71
  config = reference.class.remote_record_class.default_config.merge(
50
72
  reference.class.remote_record_config.to_h
@@ -82,6 +104,8 @@ module RemoteRecord
82
104
 
83
105
  private
84
106
 
107
+ delegate :attrs=, to: :@instance
108
+
85
109
  def instance
86
110
  @instance ||= @remote_record_config.remote_record_class.new(self, @remote_record_config)
87
111
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RemoteRecord
4
- VERSION = '0.6.0'
4
+ VERSION = '0.7.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remote_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Fish
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-02-04 00:00:00.000000000 Z
12
+ date: 2021-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord