her 0.8.2 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/.rubocop.yml +1291 -0
- data/.travis.yml +2 -0
- data/README.md +23 -3
- data/her.gemspec +1 -3
- data/lib/her/middleware/json_api_parser.rb +1 -1
- data/lib/her/model/associations/association.rb +31 -0
- data/lib/her/model/associations/association_proxy.rb +1 -1
- data/lib/her/model/attributes.rb +2 -0
- data/lib/her/model/orm.rb +79 -6
- data/lib/her/model/parse.rb +8 -12
- data/lib/her/model/relation.rb +45 -1
- data/lib/her/version.rb +1 -1
- data/spec/api_spec.rb +34 -31
- data/spec/collection_spec.rb +25 -10
- data/spec/json_api/model_spec.rb +75 -72
- data/spec/middleware/accept_json_spec.rb +1 -1
- data/spec/middleware/first_level_parse_json_spec.rb +20 -20
- data/spec/middleware/json_api_parser_spec.rb +26 -7
- data/spec/middleware/second_level_parse_json_spec.rb +8 -9
- data/spec/model/associations/association_proxy_spec.rb +2 -5
- data/spec/model/associations_spec.rb +248 -161
- data/spec/model/attributes_spec.rb +106 -99
- data/spec/model/callbacks_spec.rb +58 -26
- data/spec/model/dirty_spec.rb +30 -29
- data/spec/model/http_spec.rb +67 -35
- data/spec/model/introspection_spec.rb +26 -22
- data/spec/model/nested_attributes_spec.rb +31 -31
- data/spec/model/orm_spec.rb +312 -155
- data/spec/model/parse_spec.rb +77 -77
- data/spec/model/paths_spec.rb +109 -109
- data/spec/model/relation_spec.rb +76 -68
- data/spec/model/validations_spec.rb +6 -6
- data/spec/model_spec.rb +17 -17
- data/spec/spec_helper.rb +2 -3
- data/spec/support/macros/model_macros.rb +2 -2
- metadata +32 -59
@@ -6,89 +6,96 @@ describe Her::Model::Attributes do
|
|
6
6
|
before { spawn_model "Foo::User" }
|
7
7
|
|
8
8
|
it "handles new resource" do
|
9
|
-
@new_user = Foo::User.new(:
|
10
|
-
@new_user.new
|
11
|
-
@new_user.fullname.
|
9
|
+
@new_user = Foo::User.new(fullname: "Tobias Fünke")
|
10
|
+
expect(@new_user.new?).to be_truthy
|
11
|
+
expect(@new_user.fullname).to eq("Tobias Fünke")
|
12
|
+
end
|
13
|
+
|
14
|
+
it "handles new resource with block" do
|
15
|
+
@new_user = Foo::User.new do |user|
|
16
|
+
user.fullname = "Tobias Fünke"
|
17
|
+
end
|
18
|
+
expect(@new_user.new?).to be_truthy
|
19
|
+
expect(@new_user.fullname).to eq("Tobias Fünke")
|
12
20
|
end
|
13
21
|
|
14
22
|
it "accepts new resource with strings as hash keys" do
|
15
|
-
@new_user = Foo::User.new(
|
16
|
-
@new_user.fullname.
|
23
|
+
@new_user = Foo::User.new("fullname" => "Tobias Fünke")
|
24
|
+
expect(@new_user.fullname).to eq("Tobias Fünke")
|
17
25
|
end
|
18
26
|
|
19
27
|
it "handles method missing for getter" do
|
20
|
-
@new_user = Foo::User.new(:
|
28
|
+
@new_user = Foo::User.new(fullname: "Mayonegg")
|
21
29
|
expect { @new_user.unknown_method_for_a_user }.to raise_error(NoMethodError)
|
22
|
-
expect { @new_user.fullname }.not_to raise_error
|
30
|
+
expect { @new_user.fullname }.not_to raise_error
|
23
31
|
end
|
24
32
|
|
25
33
|
it "handles method missing for setter" do
|
26
34
|
@new_user = Foo::User.new
|
27
|
-
expect { @new_user.fullname = "Tobias Fünke" }.not_to raise_error
|
35
|
+
expect { @new_user.fullname = "Tobias Fünke" }.not_to raise_error
|
28
36
|
end
|
29
37
|
|
30
38
|
it "handles method missing for query" do
|
31
39
|
@new_user = Foo::User.new
|
32
|
-
expect { @new_user.fullname? }.not_to raise_error
|
40
|
+
expect { @new_user.fullname? }.not_to raise_error
|
33
41
|
end
|
34
42
|
|
35
43
|
it "handles respond_to for getter" do
|
36
|
-
@new_user = Foo::User.new(:
|
37
|
-
@new_user.
|
38
|
-
@new_user.
|
44
|
+
@new_user = Foo::User.new(fullname: "Mayonegg")
|
45
|
+
expect(@new_user).not_to respond_to(:unknown_method_for_a_user)
|
46
|
+
expect(@new_user).to respond_to(:fullname)
|
39
47
|
end
|
40
48
|
|
41
49
|
it "handles respond_to for setter" do
|
42
50
|
@new_user = Foo::User.new
|
43
|
-
@new_user.
|
51
|
+
expect(@new_user).to respond_to(:fullname=)
|
44
52
|
end
|
45
53
|
|
46
54
|
it "handles respond_to for query" do
|
47
55
|
@new_user = Foo::User.new
|
48
|
-
@new_user.
|
56
|
+
expect(@new_user).to respond_to(:fullname?)
|
49
57
|
end
|
50
58
|
|
51
59
|
it "handles has_attribute? for getter" do
|
52
|
-
@new_user = Foo::User.new(:
|
53
|
-
@new_user.
|
54
|
-
@new_user.
|
60
|
+
@new_user = Foo::User.new(fullname: "Mayonegg")
|
61
|
+
expect(@new_user).not_to have_attribute(:unknown_method_for_a_user)
|
62
|
+
expect(@new_user).to have_attribute(:fullname)
|
55
63
|
end
|
56
64
|
|
57
65
|
it "handles get_attribute for getter" do
|
58
|
-
@new_user = Foo::User.new(:
|
59
|
-
@new_user.get_attribute(:unknown_method_for_a_user).
|
60
|
-
@new_user.get_attribute(:fullname).
|
66
|
+
@new_user = Foo::User.new(fullname: "Mayonegg")
|
67
|
+
expect(@new_user.get_attribute(:unknown_method_for_a_user)).to be_nil
|
68
|
+
expect(@new_user.get_attribute(:fullname)).to eq("Mayonegg")
|
61
69
|
end
|
62
70
|
|
63
71
|
it "handles get_attribute for getter with dash" do
|
64
|
-
@new_user = Foo::User.new(:'life-span' =>
|
65
|
-
@new_user.get_attribute(:unknown_method_for_a_user).
|
66
|
-
@new_user.get_attribute(:'life-span').
|
72
|
+
@new_user = Foo::User.new(:'life-span' => "3 years")
|
73
|
+
expect(@new_user.get_attribute(:unknown_method_for_a_user)).to be_nil
|
74
|
+
expect(@new_user.get_attribute(:'life-span')).to eq("3 years")
|
67
75
|
end
|
68
76
|
end
|
69
77
|
|
70
|
-
|
71
78
|
context "assigning new resource data" do
|
72
79
|
before do
|
73
80
|
spawn_model "Foo::User"
|
74
|
-
@user = Foo::User.new(:
|
81
|
+
@user = Foo::User.new(active: false)
|
75
82
|
end
|
76
83
|
|
77
84
|
it "handles data update through #assign_attributes" do
|
78
|
-
@user.assign_attributes :
|
79
|
-
@user.
|
85
|
+
@user.assign_attributes active: true
|
86
|
+
expect(@user).to be_active
|
80
87
|
end
|
81
88
|
end
|
82
89
|
|
83
90
|
context "checking resource equality" do
|
84
91
|
before do
|
85
|
-
Her::API.setup :
|
92
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
86
93
|
builder.use Her::Middleware::FirstLevelParseJSON
|
87
94
|
builder.use Faraday::Request::UrlEncoded
|
88
95
|
builder.adapter :test do |stub|
|
89
|
-
stub.get("/users/1") {
|
90
|
-
stub.get("/users/2") {
|
91
|
-
stub.get("/admins/1") {
|
96
|
+
stub.get("/users/1") { [200, {}, { id: 1, fullname: "Lindsay Fünke" }.to_json] }
|
97
|
+
stub.get("/users/2") { [200, {}, { id: 1, fullname: "Tobias Fünke" }.to_json] }
|
98
|
+
stub.get("/admins/1") { [200, {}, { id: 1, fullname: "Lindsay Fünke" }.to_json] }
|
92
99
|
end
|
93
100
|
end
|
94
101
|
|
@@ -99,69 +106,69 @@ describe Her::Model::Attributes do
|
|
99
106
|
let(:user) { Foo::User.find(1) }
|
100
107
|
|
101
108
|
it "returns true for the exact same object" do
|
102
|
-
user.
|
109
|
+
expect(user).to eq(user)
|
103
110
|
end
|
104
111
|
|
105
112
|
it "returns true for the same resource via find" do
|
106
|
-
user.
|
113
|
+
expect(user).to eq(Foo::User.find(1))
|
107
114
|
end
|
108
115
|
|
109
116
|
it "returns true for the same class with identical data" do
|
110
|
-
user.
|
117
|
+
expect(user).to eq(Foo::User.new(id: 1, fullname: "Lindsay Fünke"))
|
111
118
|
end
|
112
119
|
|
113
120
|
it "returns true for a different resource with the same data" do
|
114
|
-
user.
|
121
|
+
expect(user).to eq(Foo::Admin.find(1))
|
115
122
|
end
|
116
123
|
|
117
124
|
it "returns false for the same class with different data" do
|
118
|
-
user.
|
125
|
+
expect(user).not_to eq(Foo::User.new(id: 2, fullname: "Tobias Fünke"))
|
119
126
|
end
|
120
127
|
|
121
128
|
it "returns false for a non-resource with the same data" do
|
122
|
-
fake_user = double(:
|
123
|
-
user.
|
129
|
+
fake_user = double(data: { id: 1, fullname: "Lindsay Fünke" })
|
130
|
+
expect(user).not_to eq(fake_user)
|
124
131
|
end
|
125
132
|
|
126
133
|
it "delegates eql? to ==" do
|
127
134
|
other = Object.new
|
128
|
-
user.
|
129
|
-
user.eql?(other).
|
135
|
+
expect(user).to receive(:==).with(other).and_return(true)
|
136
|
+
expect(user.eql?(other)).to be_truthy
|
130
137
|
end
|
131
138
|
|
132
139
|
it "treats equal resources as equal for Array#uniq" do
|
133
140
|
user2 = Foo::User.find(1)
|
134
|
-
[user, user2].uniq.
|
141
|
+
expect([user, user2].uniq).to eq([user])
|
135
142
|
end
|
136
143
|
|
137
144
|
it "treats equal resources as equal for hash keys" do
|
138
145
|
Foo::User.find(1)
|
139
146
|
hash = { user => true }
|
140
147
|
hash[Foo::User.find(1)] = false
|
141
|
-
hash.size.
|
142
|
-
hash.
|
148
|
+
expect(hash.size).to eq(1)
|
149
|
+
expect(hash).to eq(user => false)
|
143
150
|
end
|
144
151
|
end
|
145
152
|
|
146
153
|
context "handling metadata and errors" do
|
147
154
|
before do
|
148
|
-
Her::API.setup :
|
155
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
149
156
|
builder.use Her::Middleware::FirstLevelParseJSON
|
150
157
|
builder.adapter :test do |stub|
|
151
|
-
stub.post("/users") {
|
158
|
+
stub.post("/users") { [200, {}, { id: 1, fullname: "Tobias Fünke" }.to_json] }
|
152
159
|
end
|
153
160
|
end
|
154
161
|
|
155
|
-
spawn_model
|
162
|
+
spawn_model "Foo::User" do
|
156
163
|
store_response_errors :errors
|
157
164
|
store_metadata :my_data
|
158
165
|
end
|
159
166
|
|
160
|
-
@user = Foo::User.new(:
|
167
|
+
@user = Foo::User.new(_errors: %w(Foo Bar), _metadata: { secret: true })
|
161
168
|
end
|
162
169
|
|
163
170
|
it "should return response_errors stored in the method provided by `store_response_errors`" do
|
164
|
-
@user.errors.
|
171
|
+
expect(@user.errors).to eq(%w(Foo Bar))
|
165
172
|
end
|
166
173
|
|
167
174
|
it "should remove the default method for errors" do
|
@@ -169,7 +176,7 @@ describe Her::Model::Attributes do
|
|
169
176
|
end
|
170
177
|
|
171
178
|
it "should return metadata stored in the method provided by `store_metadata`" do
|
172
|
-
@user.my_data.
|
179
|
+
expect(@user.my_data).to eq(secret: true)
|
173
180
|
end
|
174
181
|
|
175
182
|
it "should remove the default method for metadata" do
|
@@ -177,25 +184,25 @@ describe Her::Model::Attributes do
|
|
177
184
|
end
|
178
185
|
|
179
186
|
it "should work with #save" do
|
180
|
-
@user.assign_attributes(:
|
187
|
+
@user.assign_attributes(fullname: "Tobias Fünke")
|
181
188
|
@user.save
|
182
189
|
expect { @user.metadata }.to raise_error(NoMethodError)
|
183
|
-
@user.my_data.
|
184
|
-
@user.errors.
|
190
|
+
expect(@user.my_data).to be_empty
|
191
|
+
expect(@user.errors).to be_empty
|
185
192
|
end
|
186
193
|
end
|
187
194
|
|
188
195
|
context "overwriting default attribute methods" do
|
189
196
|
context "for getter method" do
|
190
197
|
before do
|
191
|
-
Her::API.setup :
|
198
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
192
199
|
builder.use Her::Middleware::FirstLevelParseJSON
|
193
200
|
builder.adapter :test do |stub|
|
194
|
-
stub.get("/users/1") {
|
201
|
+
stub.get("/users/1") { [200, {}, { id: 1, fullname: "Tobias Fünke", document: { url: "http://example.com" } }.to_json] }
|
195
202
|
end
|
196
203
|
end
|
197
204
|
|
198
|
-
spawn_model
|
205
|
+
spawn_model "Foo::User" do
|
199
206
|
def document
|
200
207
|
@attributes[:document][:url]
|
201
208
|
end
|
@@ -204,23 +211,23 @@ describe Her::Model::Attributes do
|
|
204
211
|
|
205
212
|
it "bypasses Her's method" do
|
206
213
|
@user = Foo::User.find(1)
|
207
|
-
@user.document.
|
214
|
+
expect(@user.document).to eq("http://example.com")
|
208
215
|
|
209
216
|
@user = Foo::User.find(1)
|
210
|
-
@user.document.
|
217
|
+
expect(@user.document).to eq("http://example.com")
|
211
218
|
end
|
212
219
|
end
|
213
220
|
|
214
221
|
context "for setter method" do
|
215
222
|
before do
|
216
|
-
Her::API.setup :
|
223
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
217
224
|
builder.use Her::Middleware::FirstLevelParseJSON
|
218
225
|
builder.adapter :test do |stub|
|
219
|
-
stub.get("/users/1") {
|
226
|
+
stub.get("/users/1") { [200, {}, { id: 1, fullname: "Tobias Fünke", document: { url: "http://example.com" } }.to_json] }
|
220
227
|
end
|
221
228
|
end
|
222
229
|
|
223
|
-
spawn_model
|
230
|
+
spawn_model "Foo::User" do
|
224
231
|
def document=(document)
|
225
232
|
@attributes[:document] = document[:url]
|
226
233
|
end
|
@@ -229,24 +236,24 @@ describe Her::Model::Attributes do
|
|
229
236
|
|
230
237
|
it "bypasses Her's method" do
|
231
238
|
@user = Foo::User.find(1)
|
232
|
-
@user.document.
|
239
|
+
expect(@user.document).to eq("http://example.com")
|
233
240
|
|
234
241
|
@user = Foo::User.find(1)
|
235
|
-
@user.document.
|
242
|
+
expect(@user.document).to eq("http://example.com")
|
236
243
|
end
|
237
244
|
end
|
238
245
|
|
239
246
|
context "for predicate method" do
|
240
247
|
before do
|
241
|
-
Her::API.setup :
|
248
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
242
249
|
builder.use Her::Middleware::FirstLevelParseJSON
|
243
250
|
builder.adapter :test do |stub|
|
244
|
-
stub.get("/users/1") {
|
245
|
-
stub.get("/users/2") {
|
251
|
+
stub.get("/users/1") { [200, {}, { id: 1, fullname: "Lindsay Fünke", document: { url: nil } }.to_json] }
|
252
|
+
stub.get("/users/2") { [200, {}, { id: 1, fullname: "Tobias Fünke", document: { url: "http://example.com" } }.to_json] }
|
246
253
|
end
|
247
254
|
end
|
248
255
|
|
249
|
-
spawn_model
|
256
|
+
spawn_model "Foo::User" do
|
250
257
|
def document?
|
251
258
|
document[:url].present?
|
252
259
|
end
|
@@ -255,20 +262,20 @@ describe Her::Model::Attributes do
|
|
255
262
|
|
256
263
|
it "byoasses Her's method" do
|
257
264
|
@user = Foo::User.find(1)
|
258
|
-
@user.document
|
265
|
+
expect(@user.document?).to be_falsey
|
259
266
|
|
260
267
|
@user = Foo::User.find(1)
|
261
|
-
@user.document
|
268
|
+
expect(@user.document?).to be_falsey
|
262
269
|
|
263
270
|
@user = Foo::User.find(2)
|
264
|
-
@user.document
|
271
|
+
expect(@user.document?).to be_truthy
|
265
272
|
end
|
266
273
|
end
|
267
274
|
end
|
268
275
|
|
269
276
|
context "attributes class method" do
|
270
277
|
before do
|
271
|
-
spawn_model
|
278
|
+
spawn_model "Foo::User" do
|
272
279
|
attributes :fullname, :document
|
273
280
|
end
|
274
281
|
end
|
@@ -276,28 +283,28 @@ describe Her::Model::Attributes do
|
|
276
283
|
context "instance" do
|
277
284
|
subject { Foo::User.new }
|
278
285
|
|
279
|
-
it {
|
280
|
-
it {
|
281
|
-
it {
|
286
|
+
it { is_expected.to respond_to(:fullname) }
|
287
|
+
it { is_expected.to respond_to(:fullname=) }
|
288
|
+
it { is_expected.to respond_to(:fullname?) }
|
282
289
|
end
|
283
290
|
|
284
291
|
it "defines setter that affects @attributes" do
|
285
292
|
user = Foo::User.new
|
286
|
-
user.fullname =
|
287
|
-
user.attributes[:fullname].
|
293
|
+
user.fullname = "Tobias Fünke"
|
294
|
+
expect(user.attributes[:fullname]).to eq("Tobias Fünke")
|
288
295
|
end
|
289
296
|
|
290
297
|
it "defines getter that reads @attributes" do
|
291
298
|
user = Foo::User.new
|
292
|
-
user.assign_attributes(fullname:
|
293
|
-
user.fullname.
|
299
|
+
user.assign_attributes(fullname: "Tobias Fünke")
|
300
|
+
expect(user.fullname).to eq("Tobias Fünke")
|
294
301
|
end
|
295
302
|
|
296
303
|
it "defines predicate that reads @attributes" do
|
297
304
|
user = Foo::User.new
|
298
|
-
user.fullname
|
299
|
-
user.assign_attributes(fullname:
|
300
|
-
user.fullname
|
305
|
+
expect(user.fullname?).to be_falsey
|
306
|
+
user.assign_attributes(fullname: "Tobias Fünke")
|
307
|
+
expect(user.fullname?).to be_truthy
|
301
308
|
end
|
302
309
|
|
303
310
|
context "when attribute methods are already defined" do
|
@@ -311,75 +318,75 @@ describe Her::Model::Attributes do
|
|
311
318
|
end
|
312
319
|
@spawned_models << :AbstractUser
|
313
320
|
|
314
|
-
spawn_model
|
321
|
+
spawn_model "Foo::User", super_class: AbstractUser do
|
315
322
|
attributes :fullname
|
316
323
|
end
|
317
324
|
end
|
318
325
|
|
319
326
|
it "overrides getter method" do
|
320
|
-
Foo::User.generated_attribute_methods.instance_methods.
|
327
|
+
expect(Foo::User.generated_attribute_methods.instance_methods).to include(:fullname)
|
321
328
|
end
|
322
329
|
|
323
330
|
it "overrides setter method" do
|
324
|
-
Foo::User.generated_attribute_methods.instance_methods.
|
331
|
+
expect(Foo::User.generated_attribute_methods.instance_methods).to include(:fullname=)
|
325
332
|
end
|
326
333
|
|
327
334
|
it "overrides predicate method" do
|
328
|
-
Foo::User.generated_attribute_methods.instance_methods.
|
335
|
+
expect(Foo::User.generated_attribute_methods.instance_methods).to include(:fullname?)
|
329
336
|
end
|
330
337
|
|
331
338
|
it "defines setter that affects @attributes" do
|
332
339
|
user = Foo::User.new
|
333
|
-
user.fullname =
|
334
|
-
user.attributes[:fullname].
|
340
|
+
user.fullname = "Tobias Fünke"
|
341
|
+
expect(user.attributes[:fullname]).to eq("Tobias Fünke")
|
335
342
|
end
|
336
343
|
|
337
344
|
it "defines getter that reads @attributes" do
|
338
345
|
user = Foo::User.new
|
339
|
-
user.attributes[:fullname] =
|
340
|
-
user.fullname.
|
346
|
+
user.attributes[:fullname] = "Tobias Fünke"
|
347
|
+
expect(user.fullname).to eq("Tobias Fünke")
|
341
348
|
end
|
342
349
|
|
343
350
|
it "defines predicate that reads @attributes" do
|
344
351
|
user = Foo::User.new
|
345
|
-
user.fullname
|
346
|
-
user.attributes[:fullname] =
|
347
|
-
user.fullname
|
352
|
+
expect(user.fullname?).to be_falsey
|
353
|
+
user.attributes[:fullname] = "Tobias Fünke"
|
354
|
+
expect(user.fullname?).to be_truthy
|
348
355
|
end
|
349
356
|
end
|
350
357
|
|
351
358
|
if ActiveModel::VERSION::MAJOR < 4
|
352
359
|
it "creates a new mutex" do
|
353
360
|
expect(Mutex).to receive(:new).once.and_call_original
|
354
|
-
spawn_model
|
361
|
+
spawn_model "Foo::User" do
|
355
362
|
attributes :fullname
|
356
363
|
end
|
357
|
-
Foo::User.attribute_methods_mutex.
|
364
|
+
expect(Foo::User.attribute_methods_mutex).not_to eq(Foo::User.generated_attribute_methods)
|
358
365
|
end
|
359
366
|
|
360
367
|
it "works well with Module#synchronize monkey patched by ActiveSupport" do
|
361
368
|
Module.class_eval do
|
362
|
-
def synchronize(*
|
363
|
-
raise
|
369
|
+
def synchronize(*_args)
|
370
|
+
raise "gotcha!"
|
364
371
|
end
|
365
372
|
end
|
366
373
|
expect(Mutex).to receive(:new).once.and_call_original
|
367
|
-
spawn_model
|
374
|
+
spawn_model "Foo::User" do
|
368
375
|
attributes :fullname
|
369
376
|
end
|
370
|
-
Foo::User.attribute_methods_mutex.
|
377
|
+
expect(Foo::User.attribute_methods_mutex).not_to eq(Foo::User.generated_attribute_methods)
|
371
378
|
Module.class_eval do
|
372
379
|
undef :synchronize
|
373
380
|
end
|
374
381
|
end
|
375
382
|
else
|
376
383
|
it "uses ActiveModel's mutex" do
|
377
|
-
Foo::User.attribute_methods_mutex.
|
384
|
+
expect(Foo::User.attribute_methods_mutex).to eq(Foo::User.generated_attribute_methods)
|
378
385
|
end
|
379
386
|
end
|
380
387
|
|
381
388
|
it "uses a mutex" do
|
382
|
-
spawn_model
|
389
|
+
spawn_model "Foo::User"
|
383
390
|
expect(Foo::User.attribute_methods_mutex).to receive(:synchronize).once.and_call_original
|
384
391
|
Foo::User.class_eval do
|
385
392
|
attributes :fullname, :documents
|
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
3
3
|
|
4
4
|
describe "Her::Model and ActiveModel::Callbacks" do
|
5
5
|
before do
|
6
|
-
Her::API.setup :
|
6
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
7
7
|
builder.use Her::Middleware::FirstLevelParseJSON
|
8
8
|
end
|
9
9
|
|
@@ -11,11 +11,11 @@ describe "Her::Model and ActiveModel::Callbacks" do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
context :before_save do
|
14
|
-
subject { Foo::User.create(:
|
14
|
+
subject { Foo::User.create(name: "Tobias Funke") }
|
15
15
|
before do
|
16
16
|
Her::API.default_api.connection.adapter :test do |stub|
|
17
|
-
stub.post("/users") { |env| [200, {}, { :
|
18
|
-
stub.put("/users/1") { |env| [200, {}, { :
|
17
|
+
stub.post("/users") { |env| [200, {}, { id: 1, name: env[:body][:name] }.to_json] }
|
18
|
+
stub.put("/users/1") { |env| [200, {}, { id: 1, name: env[:body][:name] }.to_json] }
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -23,21 +23,29 @@ describe "Her::Model and ActiveModel::Callbacks" do
|
|
23
23
|
before do
|
24
24
|
class Foo::User
|
25
25
|
before_save :alter_name
|
26
|
-
def alter_name
|
26
|
+
def alter_name
|
27
|
+
name.upcase!
|
28
|
+
end
|
27
29
|
end
|
28
30
|
end
|
29
31
|
|
30
|
-
|
32
|
+
describe "#name" do
|
33
|
+
subject { super().name }
|
34
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
35
|
+
end
|
31
36
|
end
|
32
37
|
|
33
38
|
context "when using a block callback" do
|
34
39
|
before do
|
35
40
|
class Foo::User
|
36
|
-
before_save
|
41
|
+
before_save -> { name.upcase! }
|
37
42
|
end
|
38
43
|
end
|
39
44
|
|
40
|
-
|
45
|
+
describe "#name" do
|
46
|
+
subject { super().name }
|
47
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
48
|
+
end
|
41
49
|
end
|
42
50
|
|
43
51
|
context "when changing a value of an existing resource in a callback" do
|
@@ -51,18 +59,18 @@ describe "Her::Model and ActiveModel::Callbacks" do
|
|
51
59
|
end
|
52
60
|
|
53
61
|
it "should call the server with the canged value" do
|
54
|
-
subject.name.
|
62
|
+
expect(subject.name).to eq("Tobias Funke")
|
55
63
|
subject.save
|
56
|
-
subject.name.
|
64
|
+
expect(subject.name).to eq("Lumberjack")
|
57
65
|
end
|
58
66
|
end
|
59
67
|
end
|
60
68
|
|
61
69
|
context :before_create do
|
62
|
-
subject { Foo::User.create(:
|
70
|
+
subject { Foo::User.create(name: "Tobias Funke") }
|
63
71
|
before do
|
64
72
|
Her::API.default_api.connection.adapter :test do |stub|
|
65
|
-
stub.post("/users") { |env| [200, {}, { :
|
73
|
+
stub.post("/users") { |env| [200, {}, { id: 1, name: env[:body][:name] }.to_json] }
|
66
74
|
end
|
67
75
|
end
|
68
76
|
|
@@ -70,21 +78,29 @@ describe "Her::Model and ActiveModel::Callbacks" do
|
|
70
78
|
before do
|
71
79
|
class Foo::User
|
72
80
|
before_create :alter_name
|
73
|
-
def alter_name
|
81
|
+
def alter_name
|
82
|
+
name.upcase!
|
83
|
+
end
|
74
84
|
end
|
75
85
|
end
|
76
86
|
|
77
|
-
|
87
|
+
describe "#name" do
|
88
|
+
subject { super().name }
|
89
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
90
|
+
end
|
78
91
|
end
|
79
92
|
|
80
93
|
context "when using a block callback" do
|
81
94
|
before do
|
82
95
|
class Foo::User
|
83
|
-
before_create
|
96
|
+
before_create -> { name.upcase! }
|
84
97
|
end
|
85
98
|
end
|
86
99
|
|
87
|
-
|
100
|
+
describe "#name" do
|
101
|
+
subject { super().name }
|
102
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
103
|
+
end
|
88
104
|
end
|
89
105
|
end
|
90
106
|
|
@@ -92,7 +108,7 @@ describe "Her::Model and ActiveModel::Callbacks" do
|
|
92
108
|
subject { Foo::User.find(1) }
|
93
109
|
before do
|
94
110
|
Her::API.default_api.connection.adapter :test do |stub|
|
95
|
-
stub.get("/users/1") {
|
111
|
+
stub.get("/users/1") { [200, {}, { id: 1, name: "Tobias Funke" }.to_json] }
|
96
112
|
end
|
97
113
|
end
|
98
114
|
|
@@ -100,46 +116,62 @@ describe "Her::Model and ActiveModel::Callbacks" do
|
|
100
116
|
before do
|
101
117
|
class Foo::User
|
102
118
|
after_find :alter_name
|
103
|
-
def alter_name
|
119
|
+
def alter_name
|
120
|
+
name.upcase!
|
121
|
+
end
|
104
122
|
end
|
105
123
|
end
|
106
124
|
|
107
|
-
|
125
|
+
describe "#name" do
|
126
|
+
subject { super().name }
|
127
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
128
|
+
end
|
108
129
|
end
|
109
130
|
|
110
131
|
context "when using a block callback" do
|
111
132
|
before do
|
112
133
|
class Foo::User
|
113
|
-
after_find
|
134
|
+
after_find -> { name.upcase! }
|
114
135
|
end
|
115
136
|
end
|
116
137
|
|
117
|
-
|
138
|
+
describe "#name" do
|
139
|
+
subject { super().name }
|
140
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
141
|
+
end
|
118
142
|
end
|
119
143
|
end
|
120
144
|
|
121
145
|
context :after_initialize do
|
122
|
-
subject { Foo::User.new(:
|
146
|
+
subject { Foo::User.new(name: "Tobias Funke") }
|
123
147
|
|
124
148
|
context "when using a symbol callback" do
|
125
149
|
before do
|
126
150
|
class Foo::User
|
127
151
|
after_initialize :alter_name
|
128
|
-
def alter_name
|
152
|
+
def alter_name
|
153
|
+
name.upcase!
|
154
|
+
end
|
129
155
|
end
|
130
156
|
end
|
131
157
|
|
132
|
-
|
158
|
+
describe "#name" do
|
159
|
+
subject { super().name }
|
160
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
161
|
+
end
|
133
162
|
end
|
134
163
|
|
135
164
|
context "when using a block callback" do
|
136
165
|
before do
|
137
166
|
class Foo::User
|
138
|
-
after_initialize
|
167
|
+
after_initialize -> { name.upcase! }
|
139
168
|
end
|
140
169
|
end
|
141
170
|
|
142
|
-
|
171
|
+
describe "#name" do
|
172
|
+
subject { super().name }
|
173
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
174
|
+
end
|
143
175
|
end
|
144
176
|
end
|
145
177
|
end
|