her 0.8.2 → 0.10.4
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/.rspec +1 -1
- data/.rubocop.yml +1291 -0
- data/.travis.yml +6 -1
- data/README.md +29 -11
- data/her.gemspec +3 -5
- data/lib/her/api.rb +16 -9
- data/lib/her/middleware/json_api_parser.rb +1 -1
- data/lib/her/model/associations/association.rb +32 -5
- data/lib/her/model/associations/association_proxy.rb +1 -1
- data/lib/her/model/associations/belongs_to_association.rb +1 -1
- data/lib/her/model/associations/has_many_association.rb +3 -3
- data/lib/her/model/attributes.rb +105 -75
- data/lib/her/model/http.rb +3 -3
- data/lib/her/model/introspection.rb +1 -1
- data/lib/her/model/orm.rb +96 -19
- data/lib/her/model/parse.rb +27 -17
- data/lib/her/model/relation.rb +46 -2
- 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 +617 -295
- data/spec/model/attributes_spec.rb +114 -107
- data/spec/model/callbacks_spec.rb +59 -27
- data/spec/model/dirty_spec.rb +70 -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 +332 -157
- data/spec/model/parse_spec.rb +250 -77
- data/spec/model/paths_spec.rb +109 -109
- data/spec/model/relation_spec.rb +89 -69
- 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 +36 -63
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
|
|
@@ -24,14 +24,52 @@ describe Her::Model::Parse do
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
+
it "inherits attributes from parent class" do
|
28
|
+
spawn_model "Foo::ChildUser", super_class: Foo::User do
|
29
|
+
end
|
30
|
+
|
31
|
+
expect(Foo::ChildUser).to be_include_root_in_json
|
32
|
+
end
|
33
|
+
|
34
|
+
it "allows `include_root_in_json` to be set to `false` on a child model" do
|
35
|
+
spawn_model "Foo::ChildUser", super_class: Foo::User do
|
36
|
+
include_root_in_json false
|
37
|
+
end
|
38
|
+
|
39
|
+
expect(Foo::ChildUser).to_not be_include_root_in_json
|
40
|
+
end
|
41
|
+
|
27
42
|
it "wraps params in the element name in `to_params`" do
|
28
|
-
@new_user = Foo::User.new(:
|
29
|
-
@new_user.to_params.
|
43
|
+
@new_user = Foo::User.new(fullname: "Tobias Fünke")
|
44
|
+
expect(@new_user.to_params).to eq(user: { fullname: "Tobias Fünke" })
|
30
45
|
end
|
31
46
|
|
32
47
|
it "wraps params in the element name in `.create`" do
|
33
|
-
@new_user = Foo::User.admins(:
|
34
|
-
@new_user.fullname.
|
48
|
+
@new_user = Foo::User.admins(fullname: "Tobias Fünke")
|
49
|
+
expect(@new_user.fullname).to eq("Tobias Fünke")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "to false" do
|
54
|
+
before do
|
55
|
+
spawn_model "Foo::User" do
|
56
|
+
include_root_in_json false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it "inherits attributes from parent class" do
|
61
|
+
spawn_model "Foo::ChildUser", super_class: Foo::User do
|
62
|
+
end
|
63
|
+
|
64
|
+
expect(Foo::ChildUser).to_not be_include_root_in_json
|
65
|
+
end
|
66
|
+
|
67
|
+
it "allows `include_root_in_json` to be set to `true` on a child model" do
|
68
|
+
spawn_model "Foo::ChildUser", super_class: Foo::User do
|
69
|
+
include_root_in_json true
|
70
|
+
end
|
71
|
+
|
72
|
+
expect(Foo::ChildUser).to be_include_root_in_json
|
35
73
|
end
|
36
74
|
end
|
37
75
|
|
@@ -44,8 +82,8 @@ describe Her::Model::Parse do
|
|
44
82
|
end
|
45
83
|
|
46
84
|
it "wraps params in the specified value" do
|
47
|
-
@new_user = Foo::User.new(:
|
48
|
-
@new_user.to_params.
|
85
|
+
@new_user = Foo::User.new(fullname: "Tobias Fünke")
|
86
|
+
expect(@new_user.to_params).to eq(person: { fullname: "Tobias Fünke" })
|
49
87
|
end
|
50
88
|
end
|
51
89
|
|
@@ -58,15 +96,63 @@ describe Her::Model::Parse do
|
|
58
96
|
end
|
59
97
|
|
60
98
|
it "wraps params with the class name" do
|
61
|
-
@new_user = User.new(:
|
62
|
-
@new_user.to_params.
|
99
|
+
@new_user = User.new(fullname: "Tobias Fünke")
|
100
|
+
expect(@new_user.to_params).to eq(user: { fullname: "Tobias Fünke" })
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context "when `request_new_object_on_build` is set" do
|
106
|
+
context "to true" do
|
107
|
+
before do
|
108
|
+
spawn_model "Foo::User" do
|
109
|
+
request_new_object_on_build true
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
it "inherits attributes from parent class" do
|
114
|
+
spawn_model "Foo::ChildUser", super_class: Foo::User do
|
115
|
+
end
|
116
|
+
|
117
|
+
expect(Foo::ChildUser).to be_request_new_object_on_build
|
118
|
+
end
|
119
|
+
|
120
|
+
it "allows `request_new_object_on_build` to be set to `false` on a child model" do
|
121
|
+
spawn_model "Foo::ChildUser", super_class: Foo::User do
|
122
|
+
request_new_object_on_build false
|
123
|
+
end
|
124
|
+
|
125
|
+
expect(Foo::ChildUser).to_not be_request_new_object_on_build
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context "to false" do
|
130
|
+
before do
|
131
|
+
spawn_model "Foo::User" do
|
132
|
+
request_new_object_on_build false
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
it "inherits attributes from parent class" do
|
137
|
+
spawn_model "Foo::ChildUser", super_class: Foo::User do
|
138
|
+
end
|
139
|
+
|
140
|
+
expect(Foo::ChildUser).to_not be_request_new_object_on_build
|
141
|
+
end
|
142
|
+
|
143
|
+
it "allows `request_new_object_on_build` to be set to `true` on a child model" do
|
144
|
+
spawn_model "Foo::ChildUser", super_class: Foo::User do
|
145
|
+
request_new_object_on_build true
|
146
|
+
end
|
147
|
+
|
148
|
+
expect(Foo::ChildUser).to be_request_new_object_on_build
|
63
149
|
end
|
64
150
|
end
|
65
151
|
end
|
66
152
|
|
67
153
|
context "when parse_root_in_json is set" do
|
68
154
|
before do
|
69
|
-
Her::API.setup :
|
155
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
70
156
|
builder.use Her::Middleware::FirstLevelParseJSON
|
71
157
|
builder.use Faraday::Request::UrlEncoded
|
72
158
|
end
|
@@ -75,11 +161,11 @@ describe Her::Model::Parse do
|
|
75
161
|
context "to true" do
|
76
162
|
before do
|
77
163
|
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") {
|
164
|
+
stub.post("/users") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
165
|
+
stub.get("/users") { [200, {}, [{ user: { id: 1, fullname: "Lindsay Fünke" } }].to_json] }
|
166
|
+
stub.get("/users/admins") { [200, {}, [{ user: { id: 1, fullname: "Lindsay Fünke" } }].to_json] }
|
167
|
+
stub.get("/users/1") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
168
|
+
stub.put("/users/1") { [200, {}, { user: { id: 1, fullname: "Tobias Fünke Jr." } }.to_json] }
|
83
169
|
end
|
84
170
|
|
85
171
|
spawn_model("Foo::User") do
|
@@ -88,54 +174,93 @@ describe Her::Model::Parse do
|
|
88
174
|
end
|
89
175
|
end
|
90
176
|
|
177
|
+
it "inherits attributes from parent class" do
|
178
|
+
spawn_model "Foo::ChildUser", super_class: Foo::User do
|
179
|
+
end
|
180
|
+
|
181
|
+
expect(Foo::ChildUser).to be_parse_root_in_json
|
182
|
+
end
|
183
|
+
|
184
|
+
it "allows `parse_root_in_json` to be set to `false` on a child model" do
|
185
|
+
spawn_model "Foo::ChildUser", super_class: Foo::User do
|
186
|
+
parse_root_in_json false
|
187
|
+
end
|
188
|
+
|
189
|
+
expect(Foo::ChildUser).to_not be_parse_root_in_json
|
190
|
+
end
|
191
|
+
|
91
192
|
it "parse the data from the JSON root element after .create" do
|
92
|
-
@new_user = Foo::User.create(:
|
93
|
-
@new_user.fullname.
|
193
|
+
@new_user = Foo::User.create(fullname: "Lindsay Fünke")
|
194
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
94
195
|
end
|
95
196
|
|
96
197
|
it "parse the data from the JSON root element after an arbitrary HTTP request" do
|
97
198
|
@new_user = Foo::User.admins
|
98
|
-
@new_user.first.fullname.
|
199
|
+
expect(@new_user.first.fullname).to eq("Lindsay Fünke")
|
99
200
|
end
|
100
201
|
|
101
202
|
it "parse the data from the JSON root element after .all" do
|
102
203
|
@users = Foo::User.all
|
103
|
-
@users.first.fullname.
|
204
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
104
205
|
end
|
105
206
|
|
106
207
|
it "parse the data from the JSON root element after .find" do
|
107
208
|
@user = Foo::User.find(1)
|
108
|
-
@user.fullname.
|
209
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
109
210
|
end
|
110
211
|
|
111
212
|
it "parse the data from the JSON root element after .save" do
|
112
213
|
@user = Foo::User.find(1)
|
113
214
|
@user.fullname = "Tobias Fünke"
|
114
215
|
@user.save
|
115
|
-
@user.fullname.
|
216
|
+
expect(@user.fullname).to eq("Tobias Fünke Jr.")
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
context "to false" do
|
221
|
+
before do
|
222
|
+
spawn_model "Foo::User" do
|
223
|
+
parse_root_in_json false
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
it "inherits attributes from parent class" do
|
228
|
+
spawn_model "Foo::ChildUser", super_class: Foo::User do
|
229
|
+
end
|
230
|
+
|
231
|
+
expect(Foo::ChildUser).to_not be_parse_root_in_json
|
232
|
+
end
|
233
|
+
|
234
|
+
it "allows `parse_root_in_json` to be set to `true` on a child model" do
|
235
|
+
spawn_model "Foo::ChildUser", super_class: Foo::User do
|
236
|
+
parse_root_in_json true
|
237
|
+
end
|
238
|
+
|
239
|
+
expect(Foo::ChildUser).to be_parse_root_in_json
|
116
240
|
end
|
117
241
|
end
|
118
242
|
|
119
243
|
context "to a symbol" do
|
120
244
|
before do
|
121
245
|
Her::API.default_api.connection.adapter :test do |stub|
|
122
|
-
stub.post("/users") {
|
246
|
+
stub.post("/users") { [200, {}, { person: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
123
247
|
end
|
124
248
|
|
125
249
|
spawn_model("Foo::User") { parse_root_in_json :person }
|
126
250
|
end
|
127
251
|
|
128
252
|
it "parse the data with the symbol" do
|
129
|
-
@new_user = Foo::User.create(:
|
130
|
-
@new_user.
|
253
|
+
@new_user = Foo::User.create(fullname: "Lindsay Fünke")
|
254
|
+
expect(@new_user.id).to eq(1)
|
255
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
131
256
|
end
|
132
257
|
end
|
133
258
|
|
134
259
|
context "in the parent class" do
|
135
260
|
before do
|
136
261
|
Her::API.default_api.connection.adapter :test do |stub|
|
137
|
-
stub.post("/users") {
|
138
|
-
stub.get("/users") {
|
262
|
+
stub.post("/users") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
263
|
+
stub.get("/users") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
139
264
|
end
|
140
265
|
|
141
266
|
spawn_model("Foo::Model") { parse_root_in_json true, format: :active_model_serializers }
|
@@ -147,155 +272,155 @@ describe Her::Model::Parse do
|
|
147
272
|
end
|
148
273
|
|
149
274
|
it "parse the data with the symbol" do
|
150
|
-
@new_user = User.create(:
|
151
|
-
@new_user.fullname.
|
275
|
+
@new_user = User.create(fullname: "Lindsay Fünke")
|
276
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
152
277
|
end
|
153
278
|
|
154
279
|
it "parses the collection of data" do
|
155
280
|
@users = User.all
|
156
|
-
@users.first.fullname.
|
281
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
157
282
|
end
|
158
283
|
end
|
159
284
|
|
160
|
-
context "to true with :
|
285
|
+
context "to true with format: :active_model_serializers" do
|
161
286
|
before do
|
162
287
|
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") {
|
288
|
+
stub.post("/users") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
289
|
+
stub.get("/users") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
290
|
+
stub.get("/users/admins") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
291
|
+
stub.get("/users/1") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
292
|
+
stub.put("/users/1") { [200, {}, { user: { id: 1, fullname: "Tobias Fünke Jr." } }.to_json] }
|
168
293
|
end
|
169
294
|
|
170
295
|
spawn_model("Foo::User") do
|
171
|
-
parse_root_in_json true, :
|
296
|
+
parse_root_in_json true, format: :active_model_serializers
|
172
297
|
custom_get :admins
|
173
298
|
end
|
174
299
|
end
|
175
300
|
|
176
301
|
it "parse the data from the JSON root element after .create" do
|
177
|
-
@new_user = Foo::User.create(:
|
178
|
-
@new_user.fullname.
|
302
|
+
@new_user = Foo::User.create(fullname: "Lindsay Fünke")
|
303
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
179
304
|
end
|
180
305
|
|
181
306
|
it "parse the data from the JSON root element after an arbitrary HTTP request" do
|
182
307
|
@users = Foo::User.admins
|
183
|
-
@users.first.fullname.
|
308
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
184
309
|
end
|
185
310
|
|
186
311
|
it "parse the data from the JSON root element after .all" do
|
187
312
|
@users = Foo::User.all
|
188
|
-
@users.first.fullname.
|
313
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
189
314
|
end
|
190
315
|
|
191
316
|
it "parse the data from the JSON root element after .find" do
|
192
317
|
@user = Foo::User.find(1)
|
193
|
-
@user.fullname.
|
318
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
194
319
|
end
|
195
320
|
|
196
321
|
it "parse the data from the JSON root element after .save" do
|
197
322
|
@user = Foo::User.find(1)
|
198
323
|
@user.fullname = "Tobias Fünke"
|
199
324
|
@user.save
|
200
|
-
@user.fullname.
|
325
|
+
expect(@user.fullname).to eq("Tobias Fünke Jr.")
|
201
326
|
end
|
202
327
|
end
|
203
328
|
end
|
204
329
|
|
205
330
|
context "when to_params is set" do
|
206
331
|
before do
|
207
|
-
Her::API.setup :
|
332
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
208
333
|
builder.use Her::Middleware::FirstLevelParseJSON
|
209
334
|
builder.use Faraday::Request::UrlEncoded
|
210
335
|
builder.adapter :test do |stub|
|
211
|
-
stub.post("/users") { |env| ok! :
|
336
|
+
stub.post("/users") { |env| ok! id: 1, fullname: params(env)["fullname"] }
|
212
337
|
end
|
213
338
|
end
|
214
339
|
|
215
340
|
spawn_model "Foo::User" do
|
216
341
|
def to_params
|
217
|
-
{ :
|
342
|
+
{ fullname: "Lindsay Fünke" }
|
218
343
|
end
|
219
344
|
end
|
220
345
|
end
|
221
346
|
|
222
347
|
it "changes the request parameters for one-line resource creation" do
|
223
|
-
@user = Foo::User.create(:
|
224
|
-
@user.fullname.
|
348
|
+
@user = Foo::User.create(fullname: "Tobias Fünke")
|
349
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
225
350
|
end
|
226
351
|
|
227
352
|
it "changes the request parameters for Model.new + #save" do
|
228
|
-
@user = Foo::User.new(:
|
353
|
+
@user = Foo::User.new(fullname: "Tobias Fünke")
|
229
354
|
@user.save
|
230
|
-
@user.fullname.
|
355
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
231
356
|
end
|
232
357
|
end
|
233
358
|
|
234
359
|
context "when parse_root_in_json set json_api to true" do
|
235
360
|
before do
|
236
|
-
Her::API.setup :
|
361
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
237
362
|
builder.use Her::Middleware::FirstLevelParseJSON
|
238
363
|
builder.use Faraday::Request::UrlEncoded
|
239
364
|
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") {
|
365
|
+
stub.get("/users") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
366
|
+
stub.get("/users/admins") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
367
|
+
stub.get("/users/1") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
368
|
+
stub.post("/users") { [200, {}, { users: [{ fullname: "Lindsay Fünke" }] }.to_json] }
|
369
|
+
stub.put("/users/1") { [200, {}, { users: [{ id: 1, fullname: "Tobias Fünke Jr." }] }.to_json] }
|
245
370
|
end
|
246
371
|
end
|
247
372
|
|
248
373
|
spawn_model("Foo::User") do
|
249
|
-
parse_root_in_json true, :
|
374
|
+
parse_root_in_json true, format: :json_api
|
250
375
|
include_root_in_json true
|
251
376
|
custom_get :admins
|
252
377
|
end
|
253
378
|
end
|
254
379
|
|
255
380
|
it "parse the data from the JSON root element after .create" do
|
256
|
-
@new_user = Foo::User.create(:
|
257
|
-
@new_user.fullname.
|
381
|
+
@new_user = Foo::User.create(fullname: "Lindsay Fünke")
|
382
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
258
383
|
end
|
259
384
|
|
260
385
|
it "parse the data from the JSON root element after an arbitrary HTTP request" do
|
261
386
|
@new_user = Foo::User.admins
|
262
|
-
@new_user.first.fullname.
|
387
|
+
expect(@new_user.first.fullname).to eq("Lindsay Fünke")
|
263
388
|
end
|
264
389
|
|
265
390
|
it "parse the data from the JSON root element after .all" do
|
266
391
|
@users = Foo::User.all
|
267
|
-
@users.first.fullname.
|
392
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
268
393
|
end
|
269
394
|
|
270
395
|
it "parse the data from the JSON root element after .find" do
|
271
396
|
@user = Foo::User.find(1)
|
272
|
-
@user.fullname.
|
397
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
273
398
|
end
|
274
399
|
|
275
400
|
it "parse the data from the JSON root element after .save" do
|
276
401
|
@user = Foo::User.find(1)
|
277
402
|
@user.fullname = "Tobias Fünke"
|
278
403
|
@user.save
|
279
|
-
@user.fullname.
|
404
|
+
expect(@user.fullname).to eq("Tobias Fünke Jr.")
|
280
405
|
end
|
281
406
|
|
282
407
|
it "parse the data from the JSON root element after new/save" do
|
283
408
|
@user = Foo::User.new
|
284
409
|
@user.fullname = "Lindsay Fünke (before save)"
|
285
410
|
@user.save
|
286
|
-
@user.fullname.
|
411
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
287
412
|
end
|
288
413
|
end
|
289
414
|
|
290
415
|
context "when include_root_in_json set json_api" do
|
291
416
|
before do
|
292
|
-
Her::API.setup :
|
417
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
293
418
|
builder.use Her::Middleware::FirstLevelParseJSON
|
294
419
|
builder.use Faraday::Request::UrlEncoded
|
295
420
|
end
|
296
421
|
|
297
422
|
Her::API.default_api.connection.adapter :test do |stub|
|
298
|
-
stub.post("/users") { |env| [200, {}, { :
|
423
|
+
stub.post("/users") { |env| [200, {}, { users: [{ id: 1, fullname: params(env)[:users][:fullname] }] }.to_json] }
|
299
424
|
end
|
300
425
|
end
|
301
426
|
|
@@ -309,26 +434,26 @@ describe Her::Model::Parse do
|
|
309
434
|
end
|
310
435
|
|
311
436
|
it "wraps params in the element name in `to_params`" do
|
312
|
-
@new_user = Foo::User.new(:
|
313
|
-
@new_user.to_params.
|
437
|
+
@new_user = Foo::User.new(fullname: "Tobias Fünke")
|
438
|
+
expect(@new_user.to_params).to eq(users: [{ fullname: "Tobias Fünke" }])
|
314
439
|
end
|
315
440
|
|
316
441
|
it "wraps params in the element name in `.where`" do
|
317
|
-
@new_user = Foo::User.where(:
|
318
|
-
@new_user.fullname.
|
442
|
+
@new_user = Foo::User.where(fullname: "Tobias Fünke").build
|
443
|
+
expect(@new_user.fullname).to eq("Tobias Fünke")
|
319
444
|
end
|
320
445
|
end
|
321
446
|
end
|
322
447
|
|
323
|
-
context
|
448
|
+
context "when send_only_modified_attributes is set" do
|
324
449
|
before do
|
325
|
-
Her::API.setup :
|
450
|
+
Her::API.setup url: "https://api.example.com", send_only_modified_attributes: true do |builder|
|
326
451
|
builder.use Her::Middleware::FirstLevelParseJSON
|
327
452
|
builder.use Faraday::Request::UrlEncoded
|
328
453
|
end
|
329
454
|
|
330
455
|
Her::API.default_api.connection.adapter :test do |stub|
|
331
|
-
stub.get("/users/1") {
|
456
|
+
stub.get("/users/1") { [200, {}, { id: 1, first_name: "Gooby", last_name: "Pls" }.to_json] }
|
332
457
|
end
|
333
458
|
|
334
459
|
spawn_model "Foo::User" do
|
@@ -336,10 +461,58 @@ describe Her::Model::Parse do
|
|
336
461
|
end
|
337
462
|
end
|
338
463
|
|
339
|
-
it
|
464
|
+
it "only sends the attributes that were modified" do
|
340
465
|
user = Foo::User.find 1
|
341
|
-
user.first_name =
|
342
|
-
expect(user.to_params).to eql(:
|
466
|
+
user.first_name = "Someone"
|
467
|
+
expect(user.to_params).to eql(user: { first_name: "Someone" })
|
468
|
+
end
|
469
|
+
end
|
470
|
+
|
471
|
+
context 'when passed a non-Her ActiveModel instance' do
|
472
|
+
before do
|
473
|
+
klass = Class.new do
|
474
|
+
include ActiveModel::Serialization
|
475
|
+
|
476
|
+
def attributes
|
477
|
+
{ 'name' => nil }
|
478
|
+
end
|
479
|
+
|
480
|
+
def name
|
481
|
+
'foo'
|
482
|
+
end
|
483
|
+
end
|
484
|
+
|
485
|
+
@model = klass.new
|
486
|
+
|
487
|
+
Her::API.setup
|
488
|
+
spawn_model 'Foo::User'
|
489
|
+
end
|
490
|
+
|
491
|
+
it 'serializes the instance in `to_params`' do
|
492
|
+
attributes = { model: @model }
|
493
|
+
user = Foo::User.new(attributes)
|
494
|
+
expect(user.to_params).to eq(model: { name: 'foo' })
|
495
|
+
end
|
496
|
+
end
|
497
|
+
|
498
|
+
context "when attribute uses the same name as root element" do
|
499
|
+
before do
|
500
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
501
|
+
builder.use Her::Middleware::FirstLevelParseJSON
|
502
|
+
builder.use Faraday::Request::UrlEncoded
|
503
|
+
end
|
504
|
+
|
505
|
+
Her::API.default_api.connection.adapter :test do |stub|
|
506
|
+
stub.post("/users") { |env| [200, {}, { user: "foobar", id: 1, fullname: params(env)[:fullname] }.to_json] }
|
507
|
+
end
|
508
|
+
|
509
|
+
spawn_model "Foo::User"
|
510
|
+
end
|
511
|
+
|
512
|
+
it "parses as attribute instead of root element" do
|
513
|
+
user = Foo::User.create(fullname: "barfoo")
|
514
|
+
expect(user.fullname).to eq "barfoo"
|
515
|
+
expect(user.user).to eq "foobar"
|
343
516
|
end
|
344
517
|
end
|
345
518
|
end
|