ddy_remote_resource 1.0.0.rc2 → 1.0.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +125 -114
- data/lib/remote_resource/base.rb +11 -11
- data/lib/remote_resource/builder.rb +13 -25
- data/lib/remote_resource/collection.rb +25 -10
- data/lib/remote_resource/request.rb +1 -1
- data/lib/remote_resource/response.rb +33 -51
- data/lib/remote_resource/version.rb +1 -1
- data/spec/lib/remote_resource/base_spec.rb +62 -50
- data/spec/lib/remote_resource/builder_spec.rb +169 -173
- data/spec/lib/remote_resource/collection_spec.rb +94 -27
- data/spec/lib/remote_resource/request_spec.rb +14 -4
- data/spec/lib/remote_resource/response_spec.rb +102 -169
- data/spec/lib/remote_resource/version_spec.rb +1 -1
- metadata +2 -2
@@ -8,6 +8,8 @@ RSpec.describe RemoteResource::Base do
|
|
8
8
|
|
9
9
|
self.site = 'https://foobar.com'
|
10
10
|
|
11
|
+
attr_accessor :name
|
12
|
+
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
@@ -184,47 +186,42 @@ RSpec.describe RemoteResource::Base do
|
|
184
186
|
end
|
185
187
|
|
186
188
|
describe '#success?' do
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
context 'when response is successful' do
|
192
|
-
before { allow(response).to receive(:success?) { true } }
|
189
|
+
context 'when last response is successful' do
|
190
|
+
it 'returns true' do
|
191
|
+
dummy.last_response = instance_double(RemoteResource::Response, success?: true)
|
193
192
|
|
194
|
-
|
195
|
-
it 'returns true' do
|
196
|
-
expect(dummy.success?).to eql true
|
197
|
-
end
|
193
|
+
expect(dummy.success?).to eql true
|
198
194
|
end
|
195
|
+
end
|
199
196
|
|
200
|
-
|
201
|
-
|
202
|
-
|
197
|
+
context 'when last response is successful and (validation) errors are present' do
|
198
|
+
it 'returns false' do
|
199
|
+
dummy.last_response = instance_double(RemoteResource::Response, success?: true)
|
200
|
+
dummy.errors.add(:id, 'is invalid')
|
203
201
|
|
204
|
-
|
205
|
-
end
|
202
|
+
expect(dummy.success?).to eql false
|
206
203
|
end
|
207
204
|
end
|
208
205
|
|
209
|
-
context 'when response is NOT successful' do
|
210
|
-
before { allow(response).to receive(:success?) { false } }
|
211
|
-
|
206
|
+
context 'when last response is NOT successful' do
|
212
207
|
it 'returns false' do
|
208
|
+
dummy.last_response = instance_double(RemoteResource::Response, success?: false)
|
209
|
+
|
213
210
|
expect(dummy.success?).to eql false
|
214
211
|
end
|
215
212
|
end
|
216
213
|
end
|
217
214
|
|
218
215
|
describe '#errors?' do
|
219
|
-
context 'when
|
216
|
+
context 'when (validation) errors are present' do
|
220
217
|
it 'returns true' do
|
221
|
-
dummy.errors.add
|
218
|
+
dummy.errors.add(:id, 'is invalid')
|
222
219
|
|
223
220
|
expect(dummy.errors?).to eql true
|
224
221
|
end
|
225
222
|
end
|
226
223
|
|
227
|
-
context 'when
|
224
|
+
context 'when (validation) errors are NOT present' do
|
228
225
|
it 'returns false' do
|
229
226
|
expect(dummy.errors?).to eql false
|
230
227
|
end
|
@@ -232,55 +229,70 @@ RSpec.describe RemoteResource::Base do
|
|
232
229
|
end
|
233
230
|
|
234
231
|
describe '#handle_response' do
|
235
|
-
let(:response) { instance_double(RemoteResource::Response) }
|
236
|
-
|
237
|
-
before { allow(dummy).to receive(:rebuild_resource_from_response) { dummy } }
|
238
|
-
|
239
232
|
context 'when the response is a unprocessable_entity' do
|
240
|
-
|
241
|
-
allow(response).to receive(:unprocessable_entity?) { true }
|
233
|
+
let(:response) { instance_double(RemoteResource::Response, success?: false, unprocessable_entity?: true, attributes: {}, errors: { 'name' => ['is required'] }, meta: {}, request: instance_double(RemoteResource::Request)) }
|
242
234
|
|
243
|
-
|
235
|
+
it 'calls #rebuild_resource_from_response with the response' do
|
236
|
+
expect(dummy).to receive(:rebuild_resource_from_response).with(response).and_call_original
|
237
|
+
dummy.handle_response(response)
|
244
238
|
end
|
245
239
|
|
246
|
-
it '
|
247
|
-
expect(dummy).to receive(:
|
248
|
-
dummy.handle_response
|
240
|
+
it 'calls #assign_errors_from_response with the response' do
|
241
|
+
expect(dummy).to receive(:assign_errors_from_response).with(response).and_call_original
|
242
|
+
dummy.handle_response(response)
|
249
243
|
end
|
250
244
|
|
251
|
-
it '
|
252
|
-
|
253
|
-
|
245
|
+
it 'returns the same resource' do
|
246
|
+
expected_object_id = dummy.object_id
|
247
|
+
|
248
|
+
expect(dummy.handle_response(response).object_id).to eql expected_object_id
|
254
249
|
end
|
255
250
|
end
|
256
251
|
|
257
252
|
context 'when the response is NOT a unprocessable_entity' do
|
258
|
-
|
253
|
+
let(:response) { instance_double(RemoteResource::Response, success?: true, unprocessable_entity?: false, attributes: { 'id' => 12, 'name' => 'Mies' }, errors: {}, meta: {}, request: instance_double(RemoteResource::Request)) }
|
259
254
|
|
260
|
-
it '
|
261
|
-
expect(dummy).to receive(:rebuild_resource_from_response).with
|
262
|
-
dummy.handle_response
|
255
|
+
it 'calls #rebuild_resource_from_response with the response' do
|
256
|
+
expect(dummy).to receive(:rebuild_resource_from_response).with(response).and_call_original
|
257
|
+
dummy.handle_response(response)
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'does NOT calls #assign_errors_from_response' do
|
261
|
+
expect(dummy).not_to receive(:assign_errors_from_response)
|
262
|
+
dummy.handle_response(response)
|
263
263
|
end
|
264
|
-
end
|
265
|
-
end
|
266
264
|
|
267
|
-
|
268
|
-
|
265
|
+
it 'returns the same resource' do
|
266
|
+
expected_object_id = dummy.object_id
|
269
267
|
|
270
|
-
|
271
|
-
|
268
|
+
expect(dummy.handle_response(response).object_id).to eql expected_object_id
|
269
|
+
end
|
272
270
|
end
|
273
271
|
end
|
274
272
|
|
275
273
|
describe '#assign_errors_from_response' do
|
276
|
-
let(:response)
|
277
|
-
let(:error_messages_response_body) { double('error_messages_response_body') }
|
274
|
+
let(:response) { instance_double(RemoteResource::Response, success?: false, unprocessable_entity?: true, attributes: {}, errors: { 'name' => ['is required'], 'virtual_attribute' => ['is invalid'] }, meta: {}, request: instance_double(RemoteResource::Request)) }
|
278
275
|
|
279
|
-
it 'calls
|
280
|
-
|
276
|
+
it 'calls #assign_errors with the errors of the response' do
|
277
|
+
expect(dummy).to receive(:assign_errors).with(response.errors).and_call_original
|
278
|
+
dummy.assign_errors_from_response(response)
|
279
|
+
end
|
280
|
+
|
281
|
+
it 'assigns the errors of the response' do
|
282
|
+
expect(dummy.errors).to be_blank
|
283
|
+
|
284
|
+
dummy.assign_errors_from_response(response)
|
285
|
+
|
286
|
+
expect(dummy.errors).to be_present
|
287
|
+
expect(dummy.errors[:name]).to match_array ['is required']
|
288
|
+
expect(dummy.errors[:base]).to match_array ['is invalid']
|
289
|
+
end
|
290
|
+
end
|
281
291
|
|
282
|
-
|
283
|
-
|
292
|
+
describe '#_response' do
|
293
|
+
it 'warns that the method is deprecated' do
|
294
|
+
expect(dummy).to receive(:warn).with('[DEPRECATION] `._response` is deprecated. Please use `.last_response` instead.')
|
295
|
+
dummy._response
|
284
296
|
end
|
285
297
|
end
|
286
298
|
|
@@ -8,7 +8,7 @@ RSpec.describe RemoteResource::Builder do
|
|
8
8
|
|
9
9
|
self.site = 'https://foobar.com'
|
10
10
|
|
11
|
-
attr_accessor :
|
11
|
+
attr_accessor :name
|
12
12
|
|
13
13
|
end
|
14
14
|
end
|
@@ -16,245 +16,241 @@ RSpec.describe RemoteResource::Builder do
|
|
16
16
|
let(:dummy_class) { RemoteResource::BuilderDummy }
|
17
17
|
let(:dummy) { dummy_class.new }
|
18
18
|
|
19
|
+
let(:request) { instance_double(RemoteResource::Request) }
|
20
|
+
let(:response) { instance_double(RemoteResource::Response, request: request, attributes: {}, meta: { 'total' => 10 }) }
|
21
|
+
|
19
22
|
describe '.build_resource_from_response' do
|
20
|
-
let(:response)
|
21
|
-
let(:sanitized_response_body) do
|
22
|
-
{ "id" => "12", "username" => "foobar" }
|
23
|
-
end
|
24
|
-
let(:sanitized_response_meta) do
|
25
|
-
{ 'total' => '1'}
|
26
|
-
end
|
23
|
+
let(:response) { instance_double(RemoteResource::Response, request: request, attributes: { 'id' => 12, 'name' => 'Mies' }, meta: { 'total' => 10 }) }
|
27
24
|
|
28
|
-
|
29
|
-
|
30
|
-
allow(response).to receive(:sanitized_response_meta) { sanitized_response_meta }
|
31
|
-
end
|
25
|
+
it 'returns the instantiated resource from the response' do
|
26
|
+
result = dummy_class.build_resource_from_response(response)
|
32
27
|
|
33
|
-
|
34
|
-
|
35
|
-
|
28
|
+
aggregate_failures do
|
29
|
+
expect(result).to be_a dummy_class
|
30
|
+
expect(result.id).to eql 12
|
31
|
+
expect(result.name).to eql 'Mies'
|
32
|
+
end
|
36
33
|
end
|
37
|
-
end
|
38
34
|
|
39
|
-
|
40
|
-
|
41
|
-
let(:response_hash) { dummy_class.send :response_hash, response }
|
35
|
+
it 'includes the last request, last response and meta information in the instantiated resource' do
|
36
|
+
result = dummy_class.build_resource_from_response(response)
|
42
37
|
|
43
|
-
|
44
|
-
|
45
|
-
|
38
|
+
aggregate_failures do
|
39
|
+
expect(result.last_request).to eql request
|
40
|
+
expect(result.last_response).to eql response
|
41
|
+
expect(result.meta).to eql({ 'total' => 10 })
|
46
42
|
end
|
43
|
+
end
|
44
|
+
end
|
47
45
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
46
|
+
describe '.build_resource' do
|
47
|
+
let(:collection) do
|
48
|
+
{ 'id' => 12, 'name' => 'Mies' }
|
49
|
+
end
|
53
50
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
51
|
+
it 'instantiates the resource from the given collection and options' do
|
52
|
+
expect(dummy_class).to receive(:new).with(collection.merge({ meta: 'meta' })).and_call_original
|
53
|
+
dummy_class.build_resource(collection, { meta: 'meta' })
|
54
|
+
end
|
58
55
|
|
59
|
-
|
60
|
-
|
61
|
-
expect(dummy_class).to receive(:new).with(collection).and_call_original
|
62
|
-
dummy_class.build_resource collection
|
63
|
-
end
|
56
|
+
it 'returns the instantiated resource' do
|
57
|
+
result = dummy_class.build_resource(collection)
|
64
58
|
|
65
|
-
|
66
|
-
|
67
|
-
|
59
|
+
aggregate_failures do
|
60
|
+
expect(result).to be_a dummy_class
|
61
|
+
expect(result.id).to eql 12
|
62
|
+
expect(result.name).to eql 'Mies'
|
68
63
|
end
|
69
64
|
end
|
70
65
|
|
71
|
-
context 'when collection is NOT a Hash' do
|
72
|
-
let(:collection) { [] }
|
73
|
-
|
66
|
+
context 'when the given collection is NOT a Hash' do
|
74
67
|
it 'returns nil' do
|
75
|
-
|
68
|
+
aggregate_failures do
|
69
|
+
expect(dummy_class.build_resource(nil)).to be_nil
|
70
|
+
expect(dummy_class.build_resource([])).to be_nil
|
71
|
+
expect(dummy_class.build_resource('')).to be_nil
|
72
|
+
end
|
76
73
|
end
|
77
74
|
end
|
78
75
|
end
|
79
76
|
|
80
77
|
describe '.build_collection_from_response' do
|
81
|
-
let(:
|
82
|
-
let(:sanitized_response_body) do
|
78
|
+
let(:attributes) do
|
83
79
|
[
|
84
|
-
{
|
85
|
-
{
|
86
|
-
{
|
80
|
+
{ 'id' => 10, 'name' => 'Mies' },
|
81
|
+
{ 'id' => 11, 'name' => 'Aap' },
|
82
|
+
{ 'id' => 12, 'name' => 'Noot' }
|
87
83
|
]
|
88
84
|
end
|
89
|
-
let(:
|
90
|
-
|
85
|
+
let(:response) { instance_double(RemoteResource::Response, request: request, attributes: attributes, meta: { 'total' => 10 }) }
|
86
|
+
|
87
|
+
it 'returns the collection of instantiated resources from the response' do
|
88
|
+
result = dummy_class.build_collection_from_response(response)
|
89
|
+
|
90
|
+
aggregate_failures do
|
91
|
+
expect(result).to be_a RemoteResource::Collection
|
92
|
+
expect(result[0]).to be_a dummy_class
|
93
|
+
expect(result[0].id).to eql 10
|
94
|
+
expect(result[0].name).to eql 'Mies'
|
95
|
+
expect(result[1]).to be_a dummy_class
|
96
|
+
expect(result[1].id).to eql 11
|
97
|
+
expect(result[1].name).to eql 'Aap'
|
98
|
+
expect(result[2]).to be_a dummy_class
|
99
|
+
expect(result[2].id).to eql 12
|
100
|
+
expect(result[2].name).to eql 'Noot'
|
101
|
+
end
|
91
102
|
end
|
92
103
|
|
93
|
-
|
94
|
-
|
95
|
-
allow(response).to receive(:sanitized_response_meta) { sanitized_response_meta }
|
96
|
-
end
|
104
|
+
it 'includes the last request, last response and meta information in the collection of instantiated resources' do
|
105
|
+
result = dummy_class.build_collection_from_response(response)
|
97
106
|
|
98
|
-
|
99
|
-
|
100
|
-
|
107
|
+
aggregate_failures do
|
108
|
+
expect(result.last_request).to eql request
|
109
|
+
expect(result.last_response).to eql response
|
110
|
+
expect(result.meta).to eql({ 'total' => 10 })
|
111
|
+
end
|
101
112
|
end
|
102
113
|
end
|
103
114
|
|
104
115
|
describe '.build_collection' do
|
105
|
-
let(:
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
{ "id" => "11", "username" => "bazbar" },
|
113
|
-
{ "id" => "12", "username" => "aapmies" }
|
114
|
-
]
|
115
|
-
end
|
116
|
-
|
117
|
-
context 'and response_hash is given' do
|
118
|
-
it 'instantiates a RemoteResource::Collection with the class, collection AND response_hash' do
|
119
|
-
expect(RemoteResource::Collection).to receive(:new).with(dummy_class, collection, response_hash).and_call_original
|
120
|
-
dummy_class.build_collection collection, response_hash
|
121
|
-
end
|
122
|
-
|
123
|
-
it 'returns the resources' do
|
124
|
-
resources = dummy_class.build_collection collection, response_hash
|
125
|
-
resources.each { |resource| expect(resource).to be_a dummy_class }
|
126
|
-
end
|
127
|
-
end
|
116
|
+
let(:collection) do
|
117
|
+
[
|
118
|
+
{ 'id' => 10, 'name' => 'Mies' },
|
119
|
+
{ 'id' => 11, 'name' => 'Aap' },
|
120
|
+
{ 'id' => 12, 'name' => 'Noot' }
|
121
|
+
]
|
122
|
+
end
|
128
123
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
end
|
124
|
+
it 'instantiates collection of resources from the given collection and options' do
|
125
|
+
expect(RemoteResource::Collection).to receive(:new).with(dummy_class, collection, { meta: 'meta' }).and_call_original
|
126
|
+
dummy_class.build_collection(collection, { meta: 'meta' })
|
127
|
+
end
|
134
128
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
129
|
+
it 'returns the collection of instantiated resources' do
|
130
|
+
result = dummy_class.build_collection(collection)
|
131
|
+
|
132
|
+
aggregate_failures do
|
133
|
+
expect(result).to be_a RemoteResource::Collection
|
134
|
+
expect(result[0]).to be_a dummy_class
|
135
|
+
expect(result[0].id).to eql 10
|
136
|
+
expect(result[0].name).to eql 'Mies'
|
137
|
+
expect(result[1]).to be_a dummy_class
|
138
|
+
expect(result[1].id).to eql 11
|
139
|
+
expect(result[1].name).to eql 'Aap'
|
140
|
+
expect(result[2]).to be_a dummy_class
|
141
|
+
expect(result[2].id).to eql 12
|
142
|
+
expect(result[2].name).to eql 'Noot'
|
139
143
|
end
|
140
144
|
end
|
141
145
|
|
142
|
-
context 'when collection is NOT an Array' do
|
143
|
-
let(:collection) { {} }
|
144
|
-
|
146
|
+
context 'when the given collection is NOT an Array' do
|
145
147
|
it 'returns nil' do
|
146
|
-
|
148
|
+
aggregate_failures do
|
149
|
+
expect(dummy_class.build_collection(nil)).to be_nil
|
150
|
+
expect(dummy_class.build_collection({})).to be_nil
|
151
|
+
expect(dummy_class.build_collection('')).to be_nil
|
152
|
+
end
|
147
153
|
end
|
148
154
|
end
|
149
155
|
end
|
150
156
|
|
151
157
|
describe '#rebuild_resource_from_response' do
|
152
|
-
let(:response)
|
153
|
-
let(:sanitized_response_body) do
|
154
|
-
{ "id" => "12", "username" => "foobar" }
|
155
|
-
end
|
156
|
-
let(:sanitized_response_meta) do
|
157
|
-
{ 'total' => '1'}
|
158
|
-
end
|
158
|
+
let(:response) { instance_double(RemoteResource::Response, request: request, attributes: { 'name' => 'Mies' }, meta: { 'total' => 10 }) }
|
159
159
|
|
160
|
-
before
|
161
|
-
allow(response).to receive(:sanitized_response_body) { sanitized_response_body }
|
162
|
-
allow(response).to receive(:sanitized_response_meta) { sanitized_response_meta }
|
163
|
-
end
|
160
|
+
before { dummy.id = 12 }
|
164
161
|
|
165
|
-
it '
|
166
|
-
|
167
|
-
dummy.rebuild_resource_from_response response
|
168
|
-
end
|
169
|
-
end
|
162
|
+
it 'returns the same resource' do
|
163
|
+
expected_object_id = dummy.object_id
|
170
164
|
|
171
|
-
|
172
|
-
let(:response) { RemoteResource::Response.new double.as_null_object }
|
173
|
-
let(:response_hash) { dummy.send :response_hash, response }
|
165
|
+
result = dummy.rebuild_resource_from_response(response)
|
174
166
|
|
175
|
-
|
176
|
-
dummy.id = nil
|
177
|
-
dummy.username = "foo"
|
167
|
+
expect(result.object_id).to eql expected_object_id
|
178
168
|
end
|
179
169
|
|
180
|
-
|
181
|
-
|
182
|
-
|
170
|
+
it 'updates the resource from the response' do
|
171
|
+
aggregate_failures do
|
172
|
+
expect(dummy.id).to eql 12
|
173
|
+
expect(dummy.name).to be_blank
|
183
174
|
end
|
184
175
|
|
185
|
-
|
186
|
-
it 'mass-assigns the attributes of the resource with the collection AND with response_hash' do
|
187
|
-
expect(dummy.id).to be_nil
|
188
|
-
expect(dummy.username).to eql 'foo'
|
189
|
-
expect(dummy._response).to be_nil
|
190
|
-
|
191
|
-
dummy.rebuild_resource collection, response_hash
|
192
|
-
|
193
|
-
expect(dummy.id).to eql '12'
|
194
|
-
expect(dummy.username).to eql 'foobar'
|
195
|
-
expect(dummy._response).to be_a RemoteResource::Response
|
196
|
-
end
|
176
|
+
dummy.rebuild_resource_from_response(response)
|
197
177
|
|
198
|
-
|
199
|
-
|
200
|
-
|
178
|
+
aggregate_failures do
|
179
|
+
expect(dummy.id).to eql 12
|
180
|
+
expect(dummy.name).to eql 'Mies'
|
201
181
|
end
|
182
|
+
end
|
202
183
|
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
dummy.rebuild_resource collection
|
184
|
+
it 'includes the last request, last response and meta information in the resource' do
|
185
|
+
aggregate_failures do
|
186
|
+
expect(dummy.last_request).to be_blank
|
187
|
+
expect(dummy.last_response).to be_blank
|
188
|
+
expect(dummy.meta).to be_blank
|
189
|
+
end
|
210
190
|
|
211
|
-
|
212
|
-
expect(dummy.username).to eql 'foobar'
|
213
|
-
expect(dummy._response).to be_nil
|
214
|
-
end
|
191
|
+
dummy.rebuild_resource_from_response(response)
|
215
192
|
|
216
|
-
|
217
|
-
|
218
|
-
|
193
|
+
aggregate_failures do
|
194
|
+
expect(dummy.last_request).to eql request
|
195
|
+
expect(dummy.last_response).to eql response
|
196
|
+
expect(dummy.meta).to eql({ 'total' => 10 })
|
219
197
|
end
|
220
198
|
end
|
199
|
+
end
|
221
200
|
|
222
|
-
|
223
|
-
|
201
|
+
describe '#rebuild_resource' do
|
202
|
+
let(:collection) do
|
203
|
+
{ 'name' => 'Mies' }
|
204
|
+
end
|
224
205
|
|
225
|
-
|
226
|
-
it 'assigns the response_hash of the resource' do
|
227
|
-
expect(dummy.id).to be_nil
|
228
|
-
expect(dummy.username).to eql 'foo'
|
229
|
-
expect(dummy._response).to be_nil
|
206
|
+
before { dummy.id = 12 }
|
230
207
|
|
231
|
-
|
208
|
+
it 'returns the same resource' do
|
209
|
+
expected_object_id = dummy.object_id
|
232
210
|
|
233
|
-
|
234
|
-
expect(dummy.username).to eql 'foo'
|
235
|
-
expect(dummy._response).to be_a RemoteResource::Response
|
236
|
-
end
|
211
|
+
result = dummy.rebuild_resource(collection)
|
237
212
|
|
238
|
-
|
239
|
-
|
240
|
-
|
213
|
+
expect(result.object_id).to eql expected_object_id
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'updates the resource, using mass-assignment, from the given collection and options' do
|
217
|
+
expect(dummy).to receive(:attributes=).with(collection.merge({ meta: 'meta' })).and_call_original
|
218
|
+
dummy.rebuild_resource(collection, { meta: 'meta' })
|
219
|
+
end
|
220
|
+
|
221
|
+
it 'updates the resource' do
|
222
|
+
aggregate_failures do
|
223
|
+
expect(dummy.id).to eql 12
|
224
|
+
expect(dummy.name).to be_blank
|
241
225
|
end
|
242
226
|
|
243
|
-
|
244
|
-
it 'does NOT assign the response_hash of the resource' do
|
245
|
-
expect(dummy.id).to be_nil
|
246
|
-
expect(dummy.username).to eql 'foo'
|
247
|
-
expect(dummy._response).to be_nil
|
227
|
+
dummy.rebuild_resource(collection)
|
248
228
|
|
249
|
-
|
229
|
+
aggregate_failures do
|
230
|
+
expect(dummy.id).to eql 12
|
231
|
+
expect(dummy.name).to eql 'Mies'
|
232
|
+
end
|
233
|
+
end
|
250
234
|
|
251
|
-
|
252
|
-
|
253
|
-
|
235
|
+
context 'when the given collection is NOT a Hash' do
|
236
|
+
it 'does NOT update the resource' do
|
237
|
+
aggregate_failures do
|
238
|
+
expect { dummy.rebuild_resource(nil) }.not_to change { dummy.attributes }.from({ id: 12 })
|
239
|
+
expect { dummy.rebuild_resource([]) }.not_to change { dummy.attributes }.from({ id: 12 })
|
240
|
+
expect { dummy.rebuild_resource('') }.not_to change { dummy.attributes }.from({ id: 12 })
|
254
241
|
end
|
242
|
+
end
|
243
|
+
end
|
255
244
|
|
256
|
-
|
257
|
-
|
245
|
+
context 'when the given collection is NOT a Hash and options are given' do
|
246
|
+
it 'updates the resource with only the options' do
|
247
|
+
aggregate_failures do
|
248
|
+
dummy.meta = nil
|
249
|
+
expect { dummy.rebuild_resource(nil, meta: { 'total' => 10 }) }.to change { dummy.meta }.to({ 'total' => 10 })
|
250
|
+
dummy.meta = nil
|
251
|
+
expect { dummy.rebuild_resource([], meta: { 'total' => 10 }) }.to change { dummy.meta }.to({ 'total' => 10 })
|
252
|
+
dummy.meta = nil
|
253
|
+
expect { dummy.rebuild_resource('', meta: { 'total' => 10 }) }.to change { dummy.meta }.to({ 'total' => 10 })
|
258
254
|
end
|
259
255
|
end
|
260
256
|
end
|