lhs 6.0.0 → 6.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lhs/concerns/record/includes.rb +6 -0
- data/lib/lhs/concerns/record/request.rb +43 -10
- data/lib/lhs/version.rb +1 -1
- data/spec/record/includes_spec.rb +29 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d7653bf52ccc6c331969c2b7f153014c05e30f3
|
4
|
+
data.tar.gz: 99ac462ec3a706a538693846ae0f7af83a49ba38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49afc23aef8a94f6ebe4d3da147f2275ec7e615712d426b927c01d49fbcf81f8454714e1615b1bf7fbf038dd651c7aab28b89420a8479634217aeedd7dfdc730
|
7
|
+
data.tar.gz: 471d79108df5e5c4e6fed9f05fa2d1db3befbf6038e6d866187bc1e6e850627d37d20475ea141b597828c771c80e5f9a7e8cfb1fa23d6c1cb3f62700148efffb
|
@@ -75,7 +75,18 @@ class LHS::Record
|
|
75
75
|
if target._raw.is_a? Array
|
76
76
|
data[key] = addition.map(&:_raw)
|
77
77
|
else # hash with items
|
78
|
+
extend_base_item_with_hash_of_items!(target, addition)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def extend_base_item_with_hash_of_items!(target, addition)
|
83
|
+
target._raw[items_key] ||= []
|
84
|
+
if target._raw[items_key].empty?
|
78
85
|
target._raw[items_key] = addition.map(&:_raw)
|
86
|
+
else
|
87
|
+
target._raw[items_key].each_with_index do |item, index|
|
88
|
+
item.merge!(addition[index])
|
89
|
+
end
|
79
90
|
end
|
80
91
|
end
|
81
92
|
|
@@ -92,17 +103,39 @@ class LHS::Record
|
|
92
103
|
|
93
104
|
def handle_include(included, data, sub_includes = nil, references = nil)
|
94
105
|
return if data.blank? || skip_loading_includes?(data, included)
|
95
|
-
options =
|
96
|
-
if data.collection?
|
97
|
-
options_for_multiple(data, included)
|
98
|
-
elsif data[included].collection?
|
99
|
-
options_for_nested_items(data, included)
|
100
|
-
else
|
101
|
-
url_option_for(data, included)
|
102
|
-
end
|
106
|
+
options = options_for_data(data, included)
|
103
107
|
options = extend_with_references(options, references)
|
104
108
|
addition = load_include(options, data, sub_includes)
|
105
109
|
extend_raw_data!(data, addition, included)
|
110
|
+
expand_addition!(data, included) if no_expanded_data?(addition)
|
111
|
+
end
|
112
|
+
|
113
|
+
def options_for_data(data, included = nil)
|
114
|
+
return options_for_multiple(data, included) if data.collection?
|
115
|
+
return options_for_nested_items(data, included) if included && data[included].collection?
|
116
|
+
url_option_for(data, included)
|
117
|
+
end
|
118
|
+
|
119
|
+
def expand_addition!(data, included)
|
120
|
+
addition = data[included]
|
121
|
+
options = options_for_data(addition)
|
122
|
+
record = record_for_options(options) || self
|
123
|
+
options = convert_options_to_endpoints(options) if record_for_options(options)
|
124
|
+
expanded_data = begin
|
125
|
+
record.without_including.request(options)
|
126
|
+
rescue LHC::NotFound
|
127
|
+
LHS::Data.new({}, data, record)
|
128
|
+
end
|
129
|
+
extend_raw_data!(data, expanded_data, included)
|
130
|
+
end
|
131
|
+
|
132
|
+
def no_expanded_data?(addition)
|
133
|
+
return false if addition.blank?
|
134
|
+
if addition.item?
|
135
|
+
(addition._raw.keys - [:href]).empty?
|
136
|
+
elsif addition.collection?
|
137
|
+
addition.all? { |item| item && (item._raw.keys - [:href]).empty? }
|
138
|
+
end
|
106
139
|
end
|
107
140
|
|
108
141
|
# Extends request options with options provided for this reference
|
@@ -169,13 +202,13 @@ class LHS::Record
|
|
169
202
|
array
|
170
203
|
end
|
171
204
|
|
172
|
-
def options_for_multiple(data, key)
|
205
|
+
def options_for_multiple(data, key = nil)
|
173
206
|
data.map do |item|
|
174
207
|
url_option_for(item, key)
|
175
208
|
end
|
176
209
|
end
|
177
210
|
|
178
|
-
def options_for_nested_items(data, key)
|
211
|
+
def options_for_nested_items(data, key = nil)
|
179
212
|
data[key].map do |item|
|
180
213
|
url_option_for(item)
|
181
214
|
end
|
data/lib/lhs/version.rb
CHANGED
@@ -337,6 +337,35 @@ describe LHS::Record do
|
|
337
337
|
end
|
338
338
|
end
|
339
339
|
|
340
|
+
context 'unexpanded response when requesting the included collection' do
|
341
|
+
it 'loads the collection and the single items, if not already expanded' do
|
342
|
+
class Customer < LHS::Record
|
343
|
+
endpoint ':datastore/customer/:id'
|
344
|
+
end
|
345
|
+
|
346
|
+
stub_request(:get, "#{datastore}/customer/1")
|
347
|
+
.to_return(body: {
|
348
|
+
places: {
|
349
|
+
href: "#{datastore}/places"
|
350
|
+
}
|
351
|
+
}.to_json)
|
352
|
+
|
353
|
+
stub_request(:get, "#{datastore}/places")
|
354
|
+
.to_return(body: {
|
355
|
+
items: [{ href: "#{datastore}/places/1" }]
|
356
|
+
}.to_json)
|
357
|
+
|
358
|
+
item_request = stub_request(:get, "#{datastore}/places/1")
|
359
|
+
.to_return(body: {
|
360
|
+
name: 'Casa Ferlin'
|
361
|
+
}.to_json)
|
362
|
+
|
363
|
+
place = Customer.includes(:places).find(1).places.first
|
364
|
+
assert_requested(item_request)
|
365
|
+
expect(place.name).to eq 'Casa Ferlin'
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
340
369
|
context 'includes with options' do
|
341
370
|
before(:each) do
|
342
371
|
class Customer < LHS::Record
|