active_remote 2.1.1 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/active_remote.gemspec +2 -1
- data/lib/active_remote/base.rb +5 -0
- data/lib/active_remote/errors.rb +11 -0
- data/lib/active_remote/persistence.rb +6 -6
- data/lib/active_remote/validations.rb +64 -0
- data/lib/active_remote/version.rb +1 -1
- data/spec/lib/active_remote/association_spec.rb +54 -54
- data/spec/lib/active_remote/base_spec.rb +1 -1
- data/spec/lib/active_remote/dirty_spec.rb +9 -9
- data/spec/lib/active_remote/dsl_spec.rb +6 -6
- data/spec/lib/active_remote/integration_spec.rb +10 -10
- data/spec/lib/active_remote/persistence_spec.rb +53 -53
- data/spec/lib/active_remote/primary_key_spec.rb +4 -4
- data/spec/lib/active_remote/publication_spec.rb +1 -1
- data/spec/lib/active_remote/rpc_spec.rb +9 -9
- data/spec/lib/active_remote/scope_keys_spec.rb +4 -4
- data/spec/lib/active_remote/search_spec.rb +10 -10
- data/spec/lib/active_remote/serialization_spec.rb +2 -2
- data/spec/lib/active_remote/serializers/json_spec.rb +7 -7
- data/spec/lib/active_remote/serializers/protobuf_spec.rb +5 -5
- data/spec/lib/active_remote/validations_spec.rb +56 -0
- data/spec/support/models/post.rb +2 -0
- metadata +21 -4
@@ -17,7 +17,7 @@ describe ActiveRemote::DSL do
|
|
17
17
|
Tag.attr_publishable :guid
|
18
18
|
Tag.attr_publishable :name
|
19
19
|
|
20
|
-
Tag.publishable_attributes.
|
20
|
+
expect(Tag.publishable_attributes).to match_array([ :guid, :name ])
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -25,7 +25,7 @@ describe ActiveRemote::DSL do
|
|
25
25
|
context "when given a value" do
|
26
26
|
it "sets @namespace to the value" do
|
27
27
|
Tag.namespace :foo
|
28
|
-
Tag.namespace.
|
28
|
+
expect(Tag.namespace).to eq :foo
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -35,14 +35,14 @@ describe ActiveRemote::DSL do
|
|
35
35
|
it "determines the service class" do
|
36
36
|
Tag.namespace :another
|
37
37
|
|
38
|
-
Tag.service_class.
|
38
|
+
expect(Tag.service_class).to eq Another::TagService
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
42
|
context "when given a value" do
|
43
43
|
it "sets @service_class to the value" do
|
44
44
|
Tag.service_class Generic::Remote::TagService
|
45
|
-
Tag.service_class.
|
45
|
+
expect(Tag.service_class).to eq Generic::Remote::TagService
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
@@ -50,14 +50,14 @@ describe ActiveRemote::DSL do
|
|
50
50
|
describe ".service_name" do
|
51
51
|
context "when nil" do
|
52
52
|
it "determines the service name" do
|
53
|
-
Tag.service_name.
|
53
|
+
expect(Tag.service_name).to eq :tag_service
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
57
|
context "when given a value" do
|
58
58
|
it "sets @service_name to the value" do
|
59
59
|
Tag.service_name :foo
|
60
|
-
Tag.service_name.
|
60
|
+
expect(Tag.service_name).to eq :foo
|
61
61
|
end
|
62
62
|
end
|
63
63
|
end
|
@@ -6,38 +6,38 @@ describe ::ActiveRemote::Integration do
|
|
6
6
|
|
7
7
|
context "#to_param" do
|
8
8
|
# API
|
9
|
-
specify { subject.
|
9
|
+
specify { expect(subject).to respond_to(:to_param) }
|
10
10
|
|
11
11
|
it "returns the guid if the guid is present (by default)" do
|
12
|
-
subject.to_param.
|
12
|
+
expect(subject.to_param).to eq(guid)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
context "#cache_key" do
|
17
17
|
# API
|
18
|
-
specify { subject.
|
18
|
+
specify { expect(subject).to respond_to(:cache_key) }
|
19
19
|
|
20
20
|
it "sets 'new' as the identifier when the record has not been persisted" do
|
21
|
-
subject.
|
22
|
-
subject.cache_key.
|
21
|
+
expect(subject).to receive(:new_record?).and_return(true)
|
22
|
+
expect(subject.cache_key).to match(/tag\/new/)
|
23
23
|
end
|
24
24
|
|
25
25
|
it "sets the cache_key to the class/guid as a default" do
|
26
|
-
subject.
|
27
|
-
subject.cache_key.
|
26
|
+
expect(subject).to receive(:new_record?).and_return(false)
|
27
|
+
expect(subject.cache_key).to eq("tag/#{guid}")
|
28
28
|
end
|
29
29
|
|
30
30
|
it "adds the 'updated_at' attribute to the cache_key if updated_at is present" do
|
31
31
|
::ActiveRemote.config.default_cache_key_updated_at = true
|
32
32
|
twenty_o_one_one = subject[:updated_at] = DateTime.new(2001, 01, 01)
|
33
|
-
subject.
|
34
|
-
subject.cache_key.
|
33
|
+
expect(subject).to receive(:new_record?).and_return(false)
|
34
|
+
expect(subject.cache_key).to eq("tag/#{guid}-#{twenty_o_one_one.to_s(:number)}")
|
35
35
|
subject[:updated_at] = nil
|
36
36
|
::ActiveRemote.config.default_cache_key_updated_at = false
|
37
37
|
end
|
38
38
|
|
39
39
|
it "defaults the cache updated_at to false" do
|
40
|
-
::ActiveRemote.config.default_cache_key_updated_at
|
40
|
+
expect(::ActiveRemote.config.default_cache_key_updated_at?).to be_falsey
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
@@ -7,36 +7,36 @@ describe ActiveRemote::Persistence do
|
|
7
7
|
|
8
8
|
before {
|
9
9
|
rpc.better_stub(:execute).and_return(HashWithIndifferentAccess.new)
|
10
|
-
HashWithIndifferentAccess.
|
10
|
+
allow_any_instance_of(HashWithIndifferentAccess).to receive(:errors).and_return([])
|
11
11
|
Tag.better_stub(:rpc).and_return(rpc)
|
12
12
|
}
|
13
|
-
after { Tag.
|
13
|
+
after { allow(Tag).to receive(:rpc).and_call_original }
|
14
14
|
|
15
15
|
describe ".create" do
|
16
16
|
it "runs create callbacks" do
|
17
|
-
Tag.
|
17
|
+
expect_any_instance_of(Tag).to receive(:after_create_callback)
|
18
18
|
Tag.create(:name => 'foo')
|
19
19
|
end
|
20
20
|
|
21
21
|
it "initializes and saves a new record" do
|
22
|
-
Tag.
|
22
|
+
expect_any_instance_of(Tag).to receive(:save)
|
23
23
|
Tag.create(:name => 'foo')
|
24
24
|
end
|
25
25
|
|
26
26
|
it "returns a new record" do
|
27
27
|
value = Tag.create(:name => 'foo')
|
28
|
-
value.
|
28
|
+
expect(value).to be_a(Tag)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
describe ".create!" do
|
33
33
|
it "initializes and saves a new record" do
|
34
|
-
Tag.
|
34
|
+
expect_any_instance_of(Tag).to receive(:save!)
|
35
35
|
Tag.create!(:name => 'foo')
|
36
36
|
end
|
37
37
|
|
38
38
|
context "when the record has errors" do
|
39
|
-
before { Tag.
|
39
|
+
before { allow_any_instance_of(Tag).to receive(:save!).and_raise(ActiveRemote::ActiveRemoteError) }
|
40
40
|
|
41
41
|
it "raises an exception" do
|
42
42
|
expect { Tag.create!(:name => 'foo') }.to raise_error(ActiveRemote::ActiveRemoteError)
|
@@ -46,14 +46,14 @@ describe ActiveRemote::Persistence do
|
|
46
46
|
|
47
47
|
describe "#delete" do
|
48
48
|
it "deletes a remote record" do
|
49
|
-
rpc.
|
49
|
+
expect(rpc).to receive(:execute).with(:delete, subject.scope_key_hash)
|
50
50
|
subject.delete
|
51
51
|
end
|
52
52
|
|
53
53
|
context "when the record doesn't have errors" do
|
54
54
|
it "freezes the record" do
|
55
55
|
subject.delete
|
56
|
-
subject.frozen
|
56
|
+
expect(subject.frozen?).to be_truthy
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
@@ -65,18 +65,18 @@ describe ActiveRemote::Persistence do
|
|
65
65
|
|
66
66
|
it "adds the errors to the record" do
|
67
67
|
subject.delete
|
68
|
-
subject.has_errors
|
68
|
+
expect(subject.has_errors?).to be_truthy
|
69
69
|
end
|
70
70
|
|
71
71
|
it "returns false" do
|
72
|
-
subject.delete.
|
72
|
+
expect(subject.delete).to be_falsey
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
77
|
describe "#delete!" do
|
78
78
|
it "deletes a remote record" do
|
79
|
-
rpc.
|
79
|
+
expect(rpc).to receive(:execute).with(:delete, subject.scope_key_hash)
|
80
80
|
subject.delete!
|
81
81
|
end
|
82
82
|
|
@@ -94,14 +94,14 @@ describe ActiveRemote::Persistence do
|
|
94
94
|
|
95
95
|
describe "#destroy" do
|
96
96
|
it "destroys a remote record" do
|
97
|
-
rpc.
|
97
|
+
expect(rpc).to receive(:execute).with(:destroy, subject.scope_key_hash)
|
98
98
|
subject.destroy
|
99
99
|
end
|
100
100
|
|
101
101
|
context "when the record doesn't have errors" do
|
102
102
|
it "freezes the record" do
|
103
103
|
subject.destroy
|
104
|
-
subject.frozen
|
104
|
+
expect(subject.frozen?).to be_truthy
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
@@ -113,18 +113,18 @@ describe ActiveRemote::Persistence do
|
|
113
113
|
|
114
114
|
it "adds the errors to the record" do
|
115
115
|
subject.destroy
|
116
|
-
subject.has_errors
|
116
|
+
expect(subject.has_errors?).to be_truthy
|
117
117
|
end
|
118
118
|
|
119
119
|
it "returns false" do
|
120
|
-
subject.destroy.
|
120
|
+
expect(subject.destroy).to be_falsey
|
121
121
|
end
|
122
122
|
end
|
123
123
|
end
|
124
124
|
|
125
125
|
describe "#destroy!" do
|
126
126
|
it "destroys a remote record" do
|
127
|
-
rpc.
|
127
|
+
expect(rpc).to receive(:execute).with(:destroy, subject.scope_key_hash)
|
128
128
|
subject.destroy!
|
129
129
|
end
|
130
130
|
|
@@ -144,8 +144,8 @@ describe ActiveRemote::Persistence do
|
|
144
144
|
context "when the record is created through instantiate with options[:readonly]" do
|
145
145
|
subject { Tag.instantiate({:guid => 'foo'}, :readonly => true) }
|
146
146
|
|
147
|
-
its(:new_record?) { should
|
148
|
-
its(:readonly?) { should
|
147
|
+
its(:new_record?) { should be_falsey }
|
148
|
+
its(:readonly?) { should be_truthy }
|
149
149
|
end
|
150
150
|
end
|
151
151
|
|
@@ -153,13 +153,13 @@ describe ActiveRemote::Persistence do
|
|
153
153
|
context "when errors are not present" do
|
154
154
|
before { subject.errors.clear }
|
155
155
|
|
156
|
-
its(:has_errors?) { should
|
156
|
+
its(:has_errors?) { should be_falsey }
|
157
157
|
end
|
158
158
|
|
159
159
|
context "when errors are present" do
|
160
160
|
before { subject.errors[:base] << "Boom!" }
|
161
161
|
|
162
|
-
its(:has_errors?) { should
|
162
|
+
its(:has_errors?) { should be_truthy }
|
163
163
|
end
|
164
164
|
end
|
165
165
|
|
@@ -167,19 +167,19 @@ describe ActiveRemote::Persistence do
|
|
167
167
|
context "when the record is created through instantiate" do
|
168
168
|
subject { Tag.instantiate(:guid => 'foo') }
|
169
169
|
|
170
|
-
its(:new_record?) { should
|
170
|
+
its(:new_record?) { should be_falsey }
|
171
171
|
end
|
172
172
|
|
173
173
|
context "when the record is persisted" do
|
174
174
|
subject { Tag.allocate.instantiate(:guid => 'foo') }
|
175
175
|
|
176
|
-
its(:new_record?) { should
|
176
|
+
its(:new_record?) { should be_falsey }
|
177
177
|
end
|
178
178
|
|
179
179
|
context "when the record is not persisted" do
|
180
180
|
subject { Tag.new }
|
181
181
|
|
182
|
-
its(:new_record?) { should
|
182
|
+
its(:new_record?) { should be_truthy }
|
183
183
|
end
|
184
184
|
end
|
185
185
|
|
@@ -187,19 +187,19 @@ describe ActiveRemote::Persistence do
|
|
187
187
|
context "when the record is persisted" do
|
188
188
|
subject { Tag.allocate.instantiate(:guid => 'foo') }
|
189
189
|
|
190
|
-
its(:persisted?) { should
|
190
|
+
its(:persisted?) { should be_truthy }
|
191
191
|
end
|
192
192
|
|
193
193
|
context "when the record is not persisted" do
|
194
194
|
subject { Tag.new }
|
195
195
|
|
196
|
-
its(:persisted?) { should
|
196
|
+
its(:persisted?) { should be_falsey }
|
197
197
|
end
|
198
198
|
end
|
199
199
|
|
200
200
|
describe "#save" do
|
201
201
|
it "runs save callbacks" do
|
202
|
-
subject.
|
202
|
+
expect(subject).to receive(:run_callbacks).with(:save)
|
203
203
|
subject.save
|
204
204
|
end
|
205
205
|
|
@@ -208,7 +208,7 @@ describe ActiveRemote::Persistence do
|
|
208
208
|
|
209
209
|
it "creates the record" do
|
210
210
|
expected_attributes = subject.attributes.reject { |key, value| key == "guid" }
|
211
|
-
rpc.
|
211
|
+
expect(rpc).to receive(:execute).with(:create, expected_attributes)
|
212
212
|
subject.save
|
213
213
|
end
|
214
214
|
end
|
@@ -219,7 +219,7 @@ describe ActiveRemote::Persistence do
|
|
219
219
|
subject { Tag.allocate.instantiate(attributes) }
|
220
220
|
|
221
221
|
it "updates the record" do
|
222
|
-
rpc.
|
222
|
+
expect(rpc).to receive(:execute).with(:update, attributes)
|
223
223
|
subject.save
|
224
224
|
end
|
225
225
|
end
|
@@ -227,14 +227,14 @@ describe ActiveRemote::Persistence do
|
|
227
227
|
context "when the record is saved" do
|
228
228
|
it "returns true" do
|
229
229
|
subject.better_stub(:has_errors?) { false }
|
230
|
-
subject.save.
|
230
|
+
expect(subject.save).to be_truthy
|
231
231
|
end
|
232
232
|
end
|
233
233
|
|
234
234
|
context "when the record is not saved" do
|
235
235
|
it "returns false" do
|
236
236
|
subject.better_stub(:has_errors?) { true }
|
237
|
-
subject.save.
|
237
|
+
expect(subject.save).to be_falsey
|
238
238
|
end
|
239
239
|
end
|
240
240
|
|
@@ -242,27 +242,27 @@ describe ActiveRemote::Persistence do
|
|
242
242
|
before { subject.errors[:base] << "Boom!" }
|
243
243
|
|
244
244
|
it "clears the errors before the save" do
|
245
|
-
subject.errors.
|
246
|
-
subject.save.
|
247
|
-
subject.errors.
|
245
|
+
expect(subject.errors).not_to be_empty
|
246
|
+
expect(subject.save).to be_truthy
|
247
|
+
expect(subject.errors).to be_empty
|
248
248
|
end
|
249
249
|
end
|
250
250
|
end
|
251
251
|
|
252
252
|
describe "#save!" do
|
253
|
-
before { subject.
|
254
|
-
after { subject.
|
253
|
+
before { allow(subject).to receive(:execute) }
|
254
|
+
after { allow(subject).to receive(:execute).and_call_original }
|
255
255
|
|
256
256
|
context "when the record is saved" do
|
257
257
|
it "returns true" do
|
258
|
-
subject.
|
259
|
-
subject.save
|
258
|
+
allow(subject).to receive(:save).and_return(true)
|
259
|
+
expect(subject.save!).to be_truthy
|
260
260
|
end
|
261
261
|
end
|
262
262
|
|
263
263
|
context "when the record is not saved" do
|
264
264
|
it "raises an exception" do
|
265
|
-
subject.
|
265
|
+
allow(subject).to receive(:save).and_return(false)
|
266
266
|
expect { subject.save! }.to raise_error(ActiveRemote::RemoteRecordNotSaved)
|
267
267
|
end
|
268
268
|
end
|
@@ -272,13 +272,13 @@ describe ActiveRemote::Persistence do
|
|
272
272
|
context "when errors are present" do
|
273
273
|
before { subject.errors[:base] << "Boom!" }
|
274
274
|
|
275
|
-
its(:success?) { should
|
275
|
+
its(:success?) { should be_falsey }
|
276
276
|
end
|
277
277
|
|
278
278
|
context "when errors are not present" do
|
279
279
|
before { subject.errors.clear }
|
280
280
|
|
281
|
-
its(:success?) { should
|
281
|
+
its(:success?) { should be_truthy }
|
282
282
|
end
|
283
283
|
end
|
284
284
|
|
@@ -286,29 +286,29 @@ describe ActiveRemote::Persistence do
|
|
286
286
|
let(:attributes) { HashWithIndifferentAccess.new(:name => 'bar') }
|
287
287
|
let(:tag) { Tag.allocate.instantiate({:guid => "123"}) }
|
288
288
|
|
289
|
-
before { Tag.rpc.
|
290
|
-
after { Tag.rpc.
|
289
|
+
before { allow(Tag.rpc).to receive(:execute).and_return(HashWithIndifferentAccess.new) }
|
290
|
+
after { allow(Tag.rpc).to receive(:execute).and_call_original }
|
291
291
|
|
292
292
|
it "runs update callbacks" do
|
293
|
-
tag.
|
293
|
+
expect(tag).to receive(:after_update_callback)
|
294
294
|
tag.update_attributes({})
|
295
295
|
end
|
296
296
|
|
297
297
|
it "updates a remote record" do
|
298
|
-
Tag.rpc.
|
298
|
+
expect(Tag.rpc).to receive(:execute).with(:update, tag.scope_key_hash)
|
299
299
|
tag.update_attributes({})
|
300
300
|
end
|
301
301
|
|
302
|
-
before { subject.
|
303
|
-
after { subject.
|
302
|
+
before { allow(subject).to receive(:save) }
|
303
|
+
after { allow(subject).to receive(:save).and_call_original }
|
304
304
|
|
305
305
|
it "assigns new attributes" do
|
306
|
-
subject.
|
306
|
+
expect(subject).to receive(:assign_attributes).with(attributes)
|
307
307
|
subject.update_attributes(attributes)
|
308
308
|
end
|
309
309
|
|
310
310
|
it "saves the record" do
|
311
|
-
subject.
|
311
|
+
expect(subject).to receive(:save)
|
312
312
|
subject.update_attributes(attributes)
|
313
313
|
end
|
314
314
|
end
|
@@ -316,16 +316,16 @@ describe ActiveRemote::Persistence do
|
|
316
316
|
describe "#update_attributes!" do
|
317
317
|
let(:attributes) { HashWithIndifferentAccess.new(:name => 'bar') }
|
318
318
|
|
319
|
-
before { subject.
|
320
|
-
after { subject.
|
319
|
+
before { allow(subject).to receive(:save!) }
|
320
|
+
after { allow(subject).to receive(:save!).and_call_original }
|
321
321
|
|
322
322
|
it "assigns new attributes" do
|
323
|
-
subject.
|
323
|
+
expect(subject).to receive(:assign_attributes).with(attributes)
|
324
324
|
subject.update_attributes!(attributes)
|
325
325
|
end
|
326
326
|
|
327
327
|
it "saves! the record" do
|
328
|
-
subject.
|
328
|
+
expect(subject).to receive(:save!)
|
329
329
|
subject.update_attributes!(attributes)
|
330
330
|
end
|
331
331
|
end
|
@@ -7,14 +7,14 @@ describe ActiveRemote::PrimaryKey do
|
|
7
7
|
|
8
8
|
describe ".default_primary_key" do
|
9
9
|
it 'returns array of :guid' do
|
10
|
-
Tag.default_primary_key.
|
10
|
+
expect(Tag.default_primary_key).to eq(:guid)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
describe "primary_key" do
|
15
15
|
context "when no arguments are passed" do
|
16
16
|
it "returns default primary key" do
|
17
|
-
Tag.primary_key.
|
17
|
+
expect(Tag.primary_key).to eq(:guid)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -22,14 +22,14 @@ describe ActiveRemote::PrimaryKey do
|
|
22
22
|
let(:specified_primary_key) { :name }
|
23
23
|
|
24
24
|
it "returns the given primary key" do
|
25
|
-
Tag.primary_key(specified_primary_key).
|
25
|
+
expect(Tag.primary_key(specified_primary_key)).to eq(specified_primary_key)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
describe "#primary_key" do
|
31
31
|
it "returns the primary key for the class" do
|
32
|
-
Tag.new.primary_key.
|
32
|
+
expect(Tag.new.primary_key).to eq Tag.primary_key
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|