remote_record 0.7.0 → 0.7.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: d657d0ec1a8c4606cc656d2c557e592637fa50d165cd95e118a8efa3094bc7a7
4
- data.tar.gz: 6bd9609c2617e49b9701e84cd1011236978233856864791e7126c221228addf3
3
+ metadata.gz: 95e6b11dd488bf26b81544c37471498737a26598429ba8290e4e9c8e0f6d79a0
4
+ data.tar.gz: cb125226d35be488de6d72651bb1249539d4d2dec487f75e242fc949593b008c
5
5
  SHA512:
6
- metadata.gz: 4a115d1e2e29d138e67a70c574d66cb7efca0c1ab239d9f47146d8e157410c68d700c8c4bf2c19db647d45a3831f669984117db4823b16691d53af07245ccec1
7
- data.tar.gz: 7325785485edb863a7d5c91e10b80b8119c93c1ec8b5ff7b1f570ffd498ac297b4c1ba8e3e86549d19ba654e6a578dc2f705a0470fbc2c19aac9e6f5bf49d488
6
+ metadata.gz: '08872bedccc7008cbf05129c2e9e791ee73c88bdc222dbaab1ff973bf32b0e24a284690072394c4b6e9a5126f7c0c851e6a2ffbf7ef855909397fbc77d870cbd'
7
+ data.tar.gz: d86cd65b6db39f4191d39b0be6e954b56b118daa02534eb0b8c20e10fcf988d9e07ada0fef2fc418e787b1a112dfc300b31df2f7afde199df88bb3fab68d04ca
@@ -40,23 +40,49 @@ module RemoteRecord
40
40
  end
41
41
 
42
42
  def remote_all(&authz_proc)
43
+ find_or_initialize_all(remote_record_class.all(&authz_proc))
44
+ end
45
+
46
+ def remote_where(params, &authz_proc)
47
+ find_or_initialize_all(remote_record_class.where(params, &authz_proc))
48
+ end
49
+
50
+ private
51
+
52
+ def find_or_initialize_all(remote_resources)
43
53
  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
54
+ pair_remote_resources_with_records(remote_resources) do |unsaved_resources, relation|
55
+ new_resources = unsaved_resources.map do |resource|
56
+ new(remote_resource_id: resource['id']).tap { |record| record.attrs = resource }
47
57
  end
58
+ relation.to_a + new_resources
48
59
  end
49
60
  end
50
61
  end
51
62
 
52
- def remote_where(params, &authz_proc)
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
63
+ def pair_remote_resources_with_records(remote_resources)
64
+ # get resource ids
65
+ ids = remote_resource_ids(remote_resources)
66
+ # get what exists in the database
67
+ relation = where(remote_resource_id: ids)
68
+ # for each record, set its attrs
69
+ relation.map do |record|
70
+ record.attrs = remote_resources.find do |r|
71
+ r['id'].to_s == record.remote_resource_id.to_s
58
72
  end
59
73
  end
74
+ unsaved_resources = resources_without_persisted_references(remote_resources, relation)
75
+ yield(unsaved_resources, relation)
76
+ end
77
+
78
+ def remote_resource_ids(remote_resources)
79
+ remote_resources.map { |remote_resource| remote_resource['id'] }
80
+ end
81
+
82
+ def resources_without_persisted_references(remote_resources, relation)
83
+ remote_resources.reject do |resource|
84
+ relation.pluck(:remote_resource_id).include? resource['id']
85
+ end
60
86
  end
61
87
  end
62
88
 
@@ -102,8 +128,6 @@ module RemoteRecord
102
128
  self
103
129
  end
104
130
 
105
- private
106
-
107
131
  delegate :attrs=, to: :@instance
108
132
 
109
133
  def instance
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RemoteRecord
4
- VERSION = '0.7.0'
4
+ VERSION = '0.7.1'
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.7.0
4
+ version: 0.7.1
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-10 00:00:00.000000000 Z
12
+ date: 2021-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord