her 0.8.1 → 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/README.md +6 -0
- data/UPGRADE.md +1 -1
- data/her.gemspec +3 -5
- data/lib/her/model/associations/association_proxy.rb +1 -2
- data/lib/her/model/orm.rb +7 -3
- 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 +183 -146
- 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 +24 -11
- data/spec/spec_helper.rb +2 -3
- data/spec/support/macros/model_macros.rb +2 -2
- metadata +10 -37
data/spec/model/parse_spec.rb
CHANGED
@@ -4,14 +4,14 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
4
4
|
describe Her::Model::Parse do
|
5
5
|
context "when include_root_in_json is set" do
|
6
6
|
before do
|
7
|
-
Her::API.setup :
|
7
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
8
8
|
builder.use Her::Middleware::FirstLevelParseJSON
|
9
9
|
builder.use Faraday::Request::UrlEncoded
|
10
10
|
end
|
11
11
|
|
12
12
|
Her::API.default_api.connection.adapter :test do |stub|
|
13
|
-
stub.post("/users") { |env| [200, {}, { :
|
14
|
-
stub.post("/users/admins") { |env| [200, {}, { :
|
13
|
+
stub.post("/users") { |env| [200, {}, { user: { id: 1, fullname: params(env)[:user][:fullname] } }.to_json] }
|
14
|
+
stub.post("/users/admins") { |env| [200, {}, { user: { id: 1, fullname: params(env)[:user][:fullname] } }.to_json] }
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -25,13 +25,13 @@ describe Her::Model::Parse do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it "wraps params in the element name in `to_params`" do
|
28
|
-
@new_user = Foo::User.new(:
|
29
|
-
@new_user.to_params.
|
28
|
+
@new_user = Foo::User.new(fullname: "Tobias Fünke")
|
29
|
+
expect(@new_user.to_params).to eq(user: { fullname: "Tobias Fünke" })
|
30
30
|
end
|
31
31
|
|
32
32
|
it "wraps params in the element name in `.create`" do
|
33
|
-
@new_user = Foo::User.admins(:
|
34
|
-
@new_user.fullname.
|
33
|
+
@new_user = Foo::User.admins(fullname: "Tobias Fünke")
|
34
|
+
expect(@new_user.fullname).to eq("Tobias Fünke")
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -44,8 +44,8 @@ describe Her::Model::Parse do
|
|
44
44
|
end
|
45
45
|
|
46
46
|
it "wraps params in the specified value" do
|
47
|
-
@new_user = Foo::User.new(:
|
48
|
-
@new_user.to_params.
|
47
|
+
@new_user = Foo::User.new(fullname: "Tobias Fünke")
|
48
|
+
expect(@new_user.to_params).to eq(person: { fullname: "Tobias Fünke" })
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
@@ -58,15 +58,15 @@ describe Her::Model::Parse do
|
|
58
58
|
end
|
59
59
|
|
60
60
|
it "wraps params with the class name" do
|
61
|
-
@new_user = User.new(:
|
62
|
-
@new_user.to_params.
|
61
|
+
@new_user = User.new(fullname: "Tobias Fünke")
|
62
|
+
expect(@new_user.to_params).to eq(user: { fullname: "Tobias Fünke" })
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
67
|
context "when parse_root_in_json is set" do
|
68
68
|
before do
|
69
|
-
Her::API.setup :
|
69
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
70
70
|
builder.use Her::Middleware::FirstLevelParseJSON
|
71
71
|
builder.use Faraday::Request::UrlEncoded
|
72
72
|
end
|
@@ -75,11 +75,11 @@ describe Her::Model::Parse do
|
|
75
75
|
context "to true" do
|
76
76
|
before do
|
77
77
|
Her::API.default_api.connection.adapter :test do |stub|
|
78
|
-
stub.post("/users") {
|
79
|
-
stub.get("/users") {
|
80
|
-
stub.get("/users/admins") {
|
81
|
-
stub.get("/users/1") {
|
82
|
-
stub.put("/users/1") {
|
78
|
+
stub.post("/users") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
79
|
+
stub.get("/users") { [200, {}, [{ user: { id: 1, fullname: "Lindsay Fünke" } }].to_json] }
|
80
|
+
stub.get("/users/admins") { [200, {}, [{ user: { id: 1, fullname: "Lindsay Fünke" } }].to_json] }
|
81
|
+
stub.get("/users/1") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
82
|
+
stub.put("/users/1") { [200, {}, { user: { id: 1, fullname: "Tobias Fünke Jr." } }.to_json] }
|
83
83
|
end
|
84
84
|
|
85
85
|
spawn_model("Foo::User") do
|
@@ -89,53 +89,53 @@ describe Her::Model::Parse do
|
|
89
89
|
end
|
90
90
|
|
91
91
|
it "parse the data from the JSON root element after .create" do
|
92
|
-
@new_user = Foo::User.create(:
|
93
|
-
@new_user.fullname.
|
92
|
+
@new_user = Foo::User.create(fullname: "Lindsay Fünke")
|
93
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
94
94
|
end
|
95
95
|
|
96
96
|
it "parse the data from the JSON root element after an arbitrary HTTP request" do
|
97
97
|
@new_user = Foo::User.admins
|
98
|
-
@new_user.first.fullname.
|
98
|
+
expect(@new_user.first.fullname).to eq("Lindsay Fünke")
|
99
99
|
end
|
100
100
|
|
101
101
|
it "parse the data from the JSON root element after .all" do
|
102
102
|
@users = Foo::User.all
|
103
|
-
@users.first.fullname.
|
103
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
104
104
|
end
|
105
105
|
|
106
106
|
it "parse the data from the JSON root element after .find" do
|
107
107
|
@user = Foo::User.find(1)
|
108
|
-
@user.fullname.
|
108
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
109
109
|
end
|
110
110
|
|
111
111
|
it "parse the data from the JSON root element after .save" do
|
112
112
|
@user = Foo::User.find(1)
|
113
113
|
@user.fullname = "Tobias Fünke"
|
114
114
|
@user.save
|
115
|
-
@user.fullname.
|
115
|
+
expect(@user.fullname).to eq("Tobias Fünke Jr.")
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
119
119
|
context "to a symbol" do
|
120
120
|
before do
|
121
121
|
Her::API.default_api.connection.adapter :test do |stub|
|
122
|
-
stub.post("/users") {
|
122
|
+
stub.post("/users") { [200, {}, { person: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
123
123
|
end
|
124
124
|
|
125
125
|
spawn_model("Foo::User") { parse_root_in_json :person }
|
126
126
|
end
|
127
127
|
|
128
128
|
it "parse the data with the symbol" do
|
129
|
-
@new_user = Foo::User.create(:
|
130
|
-
@new_user.fullname.
|
129
|
+
@new_user = Foo::User.create(fullname: "Lindsay Fünke")
|
130
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
131
131
|
end
|
132
132
|
end
|
133
133
|
|
134
134
|
context "in the parent class" do
|
135
135
|
before do
|
136
136
|
Her::API.default_api.connection.adapter :test do |stub|
|
137
|
-
stub.post("/users") {
|
138
|
-
stub.get("/users") {
|
137
|
+
stub.post("/users") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
138
|
+
stub.get("/users") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
139
139
|
end
|
140
140
|
|
141
141
|
spawn_model("Foo::Model") { parse_root_in_json true, format: :active_model_serializers }
|
@@ -147,155 +147,155 @@ describe Her::Model::Parse do
|
|
147
147
|
end
|
148
148
|
|
149
149
|
it "parse the data with the symbol" do
|
150
|
-
@new_user = User.create(:
|
151
|
-
@new_user.fullname.
|
150
|
+
@new_user = User.create(fullname: "Lindsay Fünke")
|
151
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
152
152
|
end
|
153
153
|
|
154
154
|
it "parses the collection of data" do
|
155
155
|
@users = User.all
|
156
|
-
@users.first.fullname.
|
156
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
157
157
|
end
|
158
158
|
end
|
159
159
|
|
160
|
-
context "to true with :
|
160
|
+
context "to true with format: :active_model_serializers" do
|
161
161
|
before do
|
162
162
|
Her::API.default_api.connection.adapter :test do |stub|
|
163
|
-
stub.post("/users") {
|
164
|
-
stub.get("/users") {
|
165
|
-
stub.get("/users/admins") {
|
166
|
-
stub.get("/users/1") {
|
167
|
-
stub.put("/users/1") {
|
163
|
+
stub.post("/users") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
164
|
+
stub.get("/users") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
165
|
+
stub.get("/users/admins") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
166
|
+
stub.get("/users/1") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
167
|
+
stub.put("/users/1") { [200, {}, { user: { id: 1, fullname: "Tobias Fünke Jr." } }.to_json] }
|
168
168
|
end
|
169
169
|
|
170
170
|
spawn_model("Foo::User") do
|
171
|
-
parse_root_in_json true, :
|
171
|
+
parse_root_in_json true, format: :active_model_serializers
|
172
172
|
custom_get :admins
|
173
173
|
end
|
174
174
|
end
|
175
175
|
|
176
176
|
it "parse the data from the JSON root element after .create" do
|
177
|
-
@new_user = Foo::User.create(:
|
178
|
-
@new_user.fullname.
|
177
|
+
@new_user = Foo::User.create(fullname: "Lindsay Fünke")
|
178
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
179
179
|
end
|
180
180
|
|
181
181
|
it "parse the data from the JSON root element after an arbitrary HTTP request" do
|
182
182
|
@users = Foo::User.admins
|
183
|
-
@users.first.fullname.
|
183
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
184
184
|
end
|
185
185
|
|
186
186
|
it "parse the data from the JSON root element after .all" do
|
187
187
|
@users = Foo::User.all
|
188
|
-
@users.first.fullname.
|
188
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
189
189
|
end
|
190
190
|
|
191
191
|
it "parse the data from the JSON root element after .find" do
|
192
192
|
@user = Foo::User.find(1)
|
193
|
-
@user.fullname.
|
193
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
194
194
|
end
|
195
195
|
|
196
196
|
it "parse the data from the JSON root element after .save" do
|
197
197
|
@user = Foo::User.find(1)
|
198
198
|
@user.fullname = "Tobias Fünke"
|
199
199
|
@user.save
|
200
|
-
@user.fullname.
|
200
|
+
expect(@user.fullname).to eq("Tobias Fünke Jr.")
|
201
201
|
end
|
202
202
|
end
|
203
203
|
end
|
204
204
|
|
205
205
|
context "when to_params is set" do
|
206
206
|
before do
|
207
|
-
Her::API.setup :
|
207
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
208
208
|
builder.use Her::Middleware::FirstLevelParseJSON
|
209
209
|
builder.use Faraday::Request::UrlEncoded
|
210
210
|
builder.adapter :test do |stub|
|
211
|
-
stub.post("/users") { |env| ok! :
|
211
|
+
stub.post("/users") { |env| ok! id: 1, fullname: params(env)["fullname"] }
|
212
212
|
end
|
213
213
|
end
|
214
214
|
|
215
215
|
spawn_model "Foo::User" do
|
216
216
|
def to_params
|
217
|
-
{ :
|
217
|
+
{ fullname: "Lindsay Fünke" }
|
218
218
|
end
|
219
219
|
end
|
220
220
|
end
|
221
221
|
|
222
222
|
it "changes the request parameters for one-line resource creation" do
|
223
|
-
@user = Foo::User.create(:
|
224
|
-
@user.fullname.
|
223
|
+
@user = Foo::User.create(fullname: "Tobias Fünke")
|
224
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
225
225
|
end
|
226
226
|
|
227
227
|
it "changes the request parameters for Model.new + #save" do
|
228
|
-
@user = Foo::User.new(:
|
228
|
+
@user = Foo::User.new(fullname: "Tobias Fünke")
|
229
229
|
@user.save
|
230
|
-
@user.fullname.
|
230
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
231
231
|
end
|
232
232
|
end
|
233
233
|
|
234
234
|
context "when parse_root_in_json set json_api to true" do
|
235
235
|
before do
|
236
|
-
Her::API.setup :
|
236
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
237
237
|
builder.use Her::Middleware::FirstLevelParseJSON
|
238
238
|
builder.use Faraday::Request::UrlEncoded
|
239
239
|
builder.adapter :test do |stub|
|
240
|
-
stub.get("/users") {
|
241
|
-
stub.get("/users/admins") {
|
242
|
-
stub.get("/users/1") {
|
243
|
-
stub.post("/users") {
|
244
|
-
stub.put("/users/1") {
|
240
|
+
stub.get("/users") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
241
|
+
stub.get("/users/admins") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
242
|
+
stub.get("/users/1") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
243
|
+
stub.post("/users") { [200, {}, { users: [{ fullname: "Lindsay Fünke" }] }.to_json] }
|
244
|
+
stub.put("/users/1") { [200, {}, { users: [{ id: 1, fullname: "Tobias Fünke Jr." }] }.to_json] }
|
245
245
|
end
|
246
246
|
end
|
247
247
|
|
248
248
|
spawn_model("Foo::User") do
|
249
|
-
parse_root_in_json true, :
|
249
|
+
parse_root_in_json true, format: :json_api
|
250
250
|
include_root_in_json true
|
251
251
|
custom_get :admins
|
252
252
|
end
|
253
253
|
end
|
254
254
|
|
255
255
|
it "parse the data from the JSON root element after .create" do
|
256
|
-
@new_user = Foo::User.create(:
|
257
|
-
@new_user.fullname.
|
256
|
+
@new_user = Foo::User.create(fullname: "Lindsay Fünke")
|
257
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
258
258
|
end
|
259
259
|
|
260
260
|
it "parse the data from the JSON root element after an arbitrary HTTP request" do
|
261
261
|
@new_user = Foo::User.admins
|
262
|
-
@new_user.first.fullname.
|
262
|
+
expect(@new_user.first.fullname).to eq("Lindsay Fünke")
|
263
263
|
end
|
264
264
|
|
265
265
|
it "parse the data from the JSON root element after .all" do
|
266
266
|
@users = Foo::User.all
|
267
|
-
@users.first.fullname.
|
267
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
268
268
|
end
|
269
269
|
|
270
270
|
it "parse the data from the JSON root element after .find" do
|
271
271
|
@user = Foo::User.find(1)
|
272
|
-
@user.fullname.
|
272
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
273
273
|
end
|
274
274
|
|
275
275
|
it "parse the data from the JSON root element after .save" do
|
276
276
|
@user = Foo::User.find(1)
|
277
277
|
@user.fullname = "Tobias Fünke"
|
278
278
|
@user.save
|
279
|
-
@user.fullname.
|
279
|
+
expect(@user.fullname).to eq("Tobias Fünke Jr.")
|
280
280
|
end
|
281
281
|
|
282
282
|
it "parse the data from the JSON root element after new/save" do
|
283
283
|
@user = Foo::User.new
|
284
284
|
@user.fullname = "Lindsay Fünke (before save)"
|
285
285
|
@user.save
|
286
|
-
@user.fullname.
|
286
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
287
287
|
end
|
288
288
|
end
|
289
289
|
|
290
290
|
context "when include_root_in_json set json_api" do
|
291
291
|
before do
|
292
|
-
Her::API.setup :
|
292
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
293
293
|
builder.use Her::Middleware::FirstLevelParseJSON
|
294
294
|
builder.use Faraday::Request::UrlEncoded
|
295
295
|
end
|
296
296
|
|
297
297
|
Her::API.default_api.connection.adapter :test do |stub|
|
298
|
-
stub.post("/users") { |env| [200, {}, { :
|
298
|
+
stub.post("/users") { |env| [200, {}, { users: [{ id: 1, fullname: params(env)[:users][:fullname] }] }.to_json] }
|
299
299
|
end
|
300
300
|
end
|
301
301
|
|
@@ -309,26 +309,26 @@ describe Her::Model::Parse do
|
|
309
309
|
end
|
310
310
|
|
311
311
|
it "wraps params in the element name in `to_params`" do
|
312
|
-
@new_user = Foo::User.new(:
|
313
|
-
@new_user.to_params.
|
312
|
+
@new_user = Foo::User.new(fullname: "Tobias Fünke")
|
313
|
+
expect(@new_user.to_params).to eq(users: [{ fullname: "Tobias Fünke" }])
|
314
314
|
end
|
315
315
|
|
316
316
|
it "wraps params in the element name in `.where`" do
|
317
|
-
@new_user = Foo::User.where(:
|
318
|
-
@new_user.fullname.
|
317
|
+
@new_user = Foo::User.where(fullname: "Tobias Fünke").build
|
318
|
+
expect(@new_user.fullname).to eq("Tobias Fünke")
|
319
319
|
end
|
320
320
|
end
|
321
321
|
end
|
322
322
|
|
323
|
-
context
|
323
|
+
context "when send_only_modified_attributes is set" do
|
324
324
|
before do
|
325
|
-
Her::API.setup :
|
325
|
+
Her::API.setup url: "https://api.example.com", send_only_modified_attributes: true do |builder|
|
326
326
|
builder.use Her::Middleware::FirstLevelParseJSON
|
327
327
|
builder.use Faraday::Request::UrlEncoded
|
328
328
|
end
|
329
329
|
|
330
330
|
Her::API.default_api.connection.adapter :test do |stub|
|
331
|
-
stub.get("/users/1") {
|
331
|
+
stub.get("/users/1") { [200, {}, { id: 1, first_name: "Gooby", last_name: "Pls" }.to_json] }
|
332
332
|
end
|
333
333
|
|
334
334
|
spawn_model "Foo::User" do
|
@@ -336,10 +336,10 @@ describe Her::Model::Parse do
|
|
336
336
|
end
|
337
337
|
end
|
338
338
|
|
339
|
-
it
|
339
|
+
it "only sends the attributes that were modified" do
|
340
340
|
user = Foo::User.find 1
|
341
|
-
user.first_name =
|
342
|
-
expect(user.to_params).to eql(:
|
341
|
+
user.first_name = "Someone"
|
342
|
+
expect(user.to_params).to eql(user: { first_name: "Someone" })
|
343
343
|
end
|
344
344
|
end
|
345
345
|
end
|