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