her 0.3.3 → 0.3.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.
- data/Rakefile +4 -4
- data/lib/her/api.rb +8 -8
- data/lib/her/collection.rb +2 -2
- data/lib/her/middleware/accept_json.rb +4 -4
- data/lib/her/middleware/first_level_parse_json.rb +4 -4
- data/lib/her/middleware/second_level_parse_json.rb +4 -4
- data/lib/her/model/hooks.rb +12 -12
- data/lib/her/model/http.rb +56 -56
- data/lib/her/model/introspection.rb +13 -13
- data/lib/her/model/orm.rb +85 -59
- data/lib/her/model/paths.rb +8 -8
- data/lib/her/model/relationships.rb +15 -13
- data/lib/her/version.rb +1 -1
- data/spec/api_spec.rb +16 -16
- data/spec/middleware/accept_json_spec.rb +2 -2
- data/spec/middleware/first_level_parse_json_spec.rb +4 -4
- data/spec/middleware/second_level_parse_json_spec.rb +4 -4
- data/spec/model/hooks_spec.rb +66 -66
- data/spec/model/http_spec.rb +56 -56
- data/spec/model/introspection_spec.rb +12 -12
- data/spec/model/orm_spec.rb +113 -94
- data/spec/model/paths_spec.rb +74 -74
- data/spec/model/relationships_spec.rb +52 -52
- metadata +5 -5
data/spec/model/paths_spec.rb
CHANGED
@@ -4,29 +4,29 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
4
4
|
describe Her::Model::Paths do
|
5
5
|
context "building request paths" do
|
6
6
|
context "simple model" do
|
7
|
-
before do
|
7
|
+
before do
|
8
8
|
spawn_model "Foo::User"
|
9
|
-
end
|
9
|
+
end
|
10
10
|
|
11
11
|
describe "#build_request_path" do
|
12
|
-
it "builds paths with defaults" do
|
12
|
+
it "builds paths with defaults" do
|
13
13
|
Foo::User.build_request_path(:id => "foo").should == "users/foo"
|
14
14
|
Foo::User.build_request_path.should == "users"
|
15
|
-
end
|
15
|
+
end
|
16
16
|
|
17
|
-
it "builds paths with custom collection path" do
|
17
|
+
it "builds paths with custom collection path" do
|
18
18
|
Foo::User.collection_path "/utilisateurs"
|
19
19
|
Foo::User.build_request_path(:id => "foo").should == "/utilisateurs/foo"
|
20
20
|
Foo::User.build_request_path.should == "/utilisateurs"
|
21
|
-
end
|
21
|
+
end
|
22
22
|
|
23
|
-
it "builds paths with custom relative collection path" do
|
23
|
+
it "builds paths with custom relative collection path" do
|
24
24
|
Foo::User.collection_path "utilisateurs"
|
25
25
|
Foo::User.build_request_path(:id => "foo").should == "utilisateurs/foo"
|
26
26
|
Foo::User.build_request_path.should == "utilisateurs"
|
27
|
-
end
|
27
|
+
end
|
28
28
|
|
29
|
-
it "builds paths with custom collection path with multiple variables" do
|
29
|
+
it "builds paths with custom collection path with multiple variables" do
|
30
30
|
Foo::User.collection_path "/organizations/:organization_id/utilisateurs"
|
31
31
|
|
32
32
|
Foo::User.build_request_path(:id => "foo", :_organization_id => "acme").should == "/organizations/acme/utilisateurs/foo"
|
@@ -34,9 +34,9 @@ describe Her::Model::Paths do
|
|
34
34
|
|
35
35
|
Foo::User.build_request_path(:id => "foo", :organization_id => "acme").should == "/organizations/acme/utilisateurs/foo"
|
36
36
|
Foo::User.build_request_path(:organization_id => "acme").should == "/organizations/acme/utilisateurs"
|
37
|
-
end
|
37
|
+
end
|
38
38
|
|
39
|
-
it "builds paths with custom relative collection path with multiple variables" do
|
39
|
+
it "builds paths with custom relative collection path with multiple variables" do
|
40
40
|
Foo::User.collection_path "organizations/:organization_id/utilisateurs"
|
41
41
|
|
42
42
|
Foo::User.build_request_path(:id => "foo", :_organization_id => "acme").should == "organizations/acme/utilisateurs/foo"
|
@@ -44,102 +44,102 @@ describe Her::Model::Paths do
|
|
44
44
|
|
45
45
|
Foo::User.build_request_path(:id => "foo", :organization_id => "acme").should == "organizations/acme/utilisateurs/foo"
|
46
46
|
Foo::User.build_request_path(:organization_id => "acme").should == "organizations/acme/utilisateurs"
|
47
|
-
end
|
47
|
+
end
|
48
48
|
|
49
|
-
it "builds paths with custom item path" do
|
49
|
+
it "builds paths with custom item path" do
|
50
50
|
Foo::User.resource_path "/utilisateurs/:id"
|
51
51
|
Foo::User.build_request_path(:id => "foo").should == "/utilisateurs/foo"
|
52
52
|
Foo::User.build_request_path.should == "users"
|
53
|
-
end
|
53
|
+
end
|
54
54
|
|
55
|
-
it "builds paths with custom relative item path" do
|
55
|
+
it "builds paths with custom relative item path" do
|
56
56
|
Foo::User.resource_path "utilisateurs/:id"
|
57
57
|
Foo::User.build_request_path(:id => "foo").should == "utilisateurs/foo"
|
58
58
|
Foo::User.build_request_path.should == "users"
|
59
|
-
end
|
59
|
+
end
|
60
60
|
|
61
|
-
it "raises exceptions when building a path without required custom variables" do
|
61
|
+
it "raises exceptions when building a path without required custom variables" do
|
62
62
|
Foo::User.collection_path "/organizations/:organization_id/utilisateurs"
|
63
63
|
expect { Foo::User.build_request_path(:id => "foo") }.to raise_error(Her::Errors::PathError)
|
64
|
-
end
|
64
|
+
end
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
68
|
context "simple model with multiple words" do
|
69
|
-
before do
|
69
|
+
before do
|
70
70
|
spawn_model "Foo::AdminUser"
|
71
|
-
end
|
71
|
+
end
|
72
72
|
|
73
73
|
describe "#build_request_path" do
|
74
|
-
it "builds paths with defaults" do
|
74
|
+
it "builds paths with defaults" do
|
75
75
|
Foo::AdminUser.build_request_path(:id => "foo").should == "admin_users/foo"
|
76
76
|
Foo::AdminUser.build_request_path.should == "admin_users"
|
77
|
-
end
|
77
|
+
end
|
78
78
|
|
79
|
-
it "builds paths with custom collection path" do
|
79
|
+
it "builds paths with custom collection path" do
|
80
80
|
Foo::AdminUser.collection_path "/users"
|
81
81
|
Foo::AdminUser.build_request_path(:id => "foo").should == "/users/foo"
|
82
82
|
Foo::AdminUser.build_request_path.should == "/users"
|
83
|
-
end
|
83
|
+
end
|
84
84
|
|
85
|
-
it "builds paths with custom relative collection path" do
|
85
|
+
it "builds paths with custom relative collection path" do
|
86
86
|
Foo::AdminUser.collection_path "users"
|
87
87
|
Foo::AdminUser.build_request_path(:id => "foo").should == "users/foo"
|
88
88
|
Foo::AdminUser.build_request_path.should == "users"
|
89
|
-
end
|
89
|
+
end
|
90
90
|
|
91
|
-
it "builds paths with custom collection path with multiple variables" do
|
91
|
+
it "builds paths with custom collection path with multiple variables" do
|
92
92
|
Foo::AdminUser.collection_path "/organizations/:organization_id/users"
|
93
93
|
Foo::AdminUser.build_request_path(:id => "foo", :_organization_id => "acme").should == "/organizations/acme/users/foo"
|
94
94
|
Foo::AdminUser.build_request_path(:_organization_id => "acme").should == "/organizations/acme/users"
|
95
|
-
end
|
95
|
+
end
|
96
96
|
|
97
|
-
it "builds paths with custom relative collection path with multiple variables" do
|
97
|
+
it "builds paths with custom relative collection path with multiple variables" do
|
98
98
|
Foo::AdminUser.collection_path "organizations/:organization_id/users"
|
99
99
|
Foo::AdminUser.build_request_path(:id => "foo", :_organization_id => "acme").should == "organizations/acme/users/foo"
|
100
100
|
Foo::AdminUser.build_request_path(:_organization_id => "acme").should == "organizations/acme/users"
|
101
|
-
end
|
101
|
+
end
|
102
102
|
|
103
|
-
it "builds paths with custom item path" do
|
103
|
+
it "builds paths with custom item path" do
|
104
104
|
Foo::AdminUser.resource_path "/users/:id"
|
105
105
|
Foo::AdminUser.build_request_path(:id => "foo").should == "/users/foo"
|
106
106
|
Foo::AdminUser.build_request_path.should == "admin_users"
|
107
|
-
end
|
107
|
+
end
|
108
108
|
|
109
|
-
it "builds paths with custom relative item path" do
|
109
|
+
it "builds paths with custom relative item path" do
|
110
110
|
Foo::AdminUser.resource_path "users/:id"
|
111
111
|
Foo::AdminUser.build_request_path(:id => "foo").should == "users/foo"
|
112
112
|
Foo::AdminUser.build_request_path.should == "admin_users"
|
113
|
-
end
|
113
|
+
end
|
114
114
|
|
115
|
-
it "raises exceptions when building a path without required custom variables" do
|
115
|
+
it "raises exceptions when building a path without required custom variables" do
|
116
116
|
Foo::AdminUser.collection_path "/organizations/:organization_id/users"
|
117
117
|
expect { Foo::AdminUser.build_request_path(:id => "foo") }.to raise_error(Her::Errors::PathError)
|
118
|
-
end
|
118
|
+
end
|
119
119
|
|
120
|
-
it "raises exceptions when building a relative path without required custom variables" do
|
120
|
+
it "raises exceptions when building a relative path without required custom variables" do
|
121
121
|
Foo::AdminUser.collection_path "organizations/:organization_id/users"
|
122
122
|
expect { Foo::AdminUser.build_request_path(:id => "foo") }.to raise_error(Her::Errors::PathError)
|
123
|
-
end
|
123
|
+
end
|
124
124
|
end
|
125
125
|
end
|
126
126
|
|
127
127
|
context "nested model" do
|
128
|
-
before do
|
128
|
+
before do
|
129
129
|
spawn_model "Foo::User"
|
130
|
-
end
|
130
|
+
end
|
131
131
|
|
132
132
|
describe "#build_request_path" do
|
133
|
-
it "builds paths with defaults" do
|
133
|
+
it "builds paths with defaults" do
|
134
134
|
Foo::User.build_request_path(:id => "foo").should == "users/foo"
|
135
135
|
Foo::User.build_request_path.should == "users"
|
136
|
-
end
|
136
|
+
end
|
137
137
|
end
|
138
138
|
end
|
139
139
|
end
|
140
140
|
|
141
141
|
context "making subdomain HTTP requests" do
|
142
|
-
before do
|
142
|
+
before do
|
143
143
|
Her::API.setup :url => "https://api.example.com/" do |builder|
|
144
144
|
builder.use Her::Middleware::FirstLevelParseJSON
|
145
145
|
builder.use Faraday::Request::UrlEncoded
|
@@ -155,85 +155,85 @@ describe Her::Model::Paths do
|
|
155
155
|
spawn_model "Foo::User" do
|
156
156
|
collection_path "organizations/:organization_id/users"
|
157
157
|
end
|
158
|
-
end
|
158
|
+
end
|
159
159
|
|
160
160
|
describe "fetching a resource" do
|
161
|
-
it "maps a single resource to a Ruby object" do
|
161
|
+
it "maps a single resource to a Ruby object" do
|
162
162
|
@user = Foo::User.find(1, :_organization_id => 2)
|
163
163
|
@user.id.should == 1
|
164
164
|
@user.fullname.should == "Tobias Fünke"
|
165
|
-
end
|
165
|
+
end
|
166
166
|
end
|
167
167
|
|
168
168
|
describe "fetching a collection" do
|
169
|
-
it "maps a collection of resources to an array of Ruby objects" do
|
169
|
+
it "maps a collection of resources to an array of Ruby objects" do
|
170
170
|
@users = Foo::User.all(:_organization_id => 2)
|
171
171
|
@users.length.should == 2
|
172
172
|
@users.first.fullname.should == "Tobias Fünke"
|
173
|
-
end
|
173
|
+
end
|
174
174
|
end
|
175
175
|
|
176
176
|
describe "handling new resource" do
|
177
|
-
it "handles new resource" do
|
177
|
+
it "handles new resource" do
|
178
178
|
@new_user = Foo::User.new(:fullname => "Tobias Fünke", :organization_id => 2)
|
179
179
|
@new_user.new?.should be_true
|
180
180
|
|
181
181
|
@existing_user = Foo::User.find(1, :_organization_id => 2)
|
182
182
|
@existing_user.new?.should be_false
|
183
|
-
end
|
183
|
+
end
|
184
184
|
end
|
185
185
|
|
186
186
|
describe "creating resources" do
|
187
|
-
it "handle one-line resource creation" do
|
187
|
+
it "handle one-line resource creation" do
|
188
188
|
@user = Foo::User.create(:fullname => "Tobias Fünke", :organization_id => 2)
|
189
189
|
@user.id.should == 1
|
190
190
|
@user.fullname.should == "Tobias Fünke"
|
191
|
-
end
|
191
|
+
end
|
192
192
|
|
193
|
-
it "handle resource creation through Model.new + #save" do
|
193
|
+
it "handle resource creation through Model.new + #save" do
|
194
194
|
@user = Foo::User.new(:fullname => "Tobias Fünke", :organization_id => 2)
|
195
195
|
@user.save
|
196
196
|
@user.fullname.should == "Tobias Fünke"
|
197
|
-
end
|
197
|
+
end
|
198
198
|
end
|
199
199
|
|
200
200
|
context "updating resources" do
|
201
|
-
it "handle resource data update without saving it" do
|
201
|
+
it "handle resource data update without saving it" do
|
202
202
|
@user = Foo::User.find(1, :_organization_id => 2)
|
203
203
|
@user.fullname.should == "Tobias Fünke"
|
204
204
|
@user.fullname = "Kittie Sanchez"
|
205
205
|
@user.fullname.should == "Kittie Sanchez"
|
206
|
-
end
|
206
|
+
end
|
207
207
|
|
208
|
-
it "handle resource update through the .update class method" do
|
208
|
+
it "handle resource update through the .update class method" do
|
209
209
|
@user = Foo::User.save_existing(1, { :fullname => "Lindsay Fünke", :organization_id => 2 })
|
210
210
|
@user.fullname.should == "Lindsay Fünke"
|
211
|
-
end
|
211
|
+
end
|
212
212
|
|
213
|
-
it "handle resource update through #save on an existing resource" do
|
213
|
+
it "handle resource update through #save on an existing resource" do
|
214
214
|
@user = Foo::User.find(1, :_organization_id => 2)
|
215
215
|
@user.fullname = "Lindsay Fünke"
|
216
216
|
@user.save
|
217
217
|
@user.fullname.should == "Lindsay Fünke"
|
218
|
-
end
|
218
|
+
end
|
219
219
|
end
|
220
220
|
|
221
221
|
context "deleting resources" do
|
222
|
-
it "handle resource deletion through the .destroy class method" do
|
222
|
+
it "handle resource deletion through the .destroy class method" do
|
223
223
|
@user = Foo::User.destroy_existing(1, :_organization_id => 2)
|
224
224
|
@user.active.should be_false
|
225
|
-
end
|
225
|
+
end
|
226
226
|
|
227
|
-
it "handle resource deletion through #destroy on an existing resource" do
|
227
|
+
it "handle resource deletion through #destroy on an existing resource" do
|
228
228
|
@user = Foo::User.find(1, :_organization_id => 2)
|
229
229
|
@user.destroy
|
230
230
|
@user.active.should be_false
|
231
|
-
end
|
231
|
+
end
|
232
232
|
end
|
233
233
|
end
|
234
234
|
|
235
235
|
context "making path HTTP requests" do
|
236
|
-
before do
|
236
|
+
before do
|
237
237
|
Her::API.setup :url => "https://example.com/api/" do |builder|
|
238
238
|
builder.use Her::Middleware::FirstLevelParseJSON
|
239
239
|
builder.use Faraday::Request::UrlEncoded
|
@@ -246,40 +246,40 @@ describe Her::Model::Paths do
|
|
246
246
|
spawn_model "Foo::User" do
|
247
247
|
collection_path "organizations/:organization_id/users"
|
248
248
|
end
|
249
|
-
end
|
249
|
+
end
|
250
250
|
|
251
251
|
describe "fetching a resource" do
|
252
|
-
it "maps a single resource to a Ruby object" do
|
252
|
+
it "maps a single resource to a Ruby object" do
|
253
253
|
@user = Foo::User.find(1, :_organization_id => 2)
|
254
254
|
@user.id.should == 1
|
255
255
|
@user.fullname.should == "Tobias Fünke"
|
256
|
-
end
|
256
|
+
end
|
257
257
|
end
|
258
258
|
|
259
259
|
describe "fetching a collection" do
|
260
|
-
it "maps a collection of resources to an array of Ruby objects" do
|
260
|
+
it "maps a collection of resources to an array of Ruby objects" do
|
261
261
|
@users = Foo::User.all(:_organization_id => 2)
|
262
262
|
@users.length.should == 2
|
263
263
|
@users.first.fullname.should == "Tobias Fünke"
|
264
|
-
end
|
264
|
+
end
|
265
265
|
end
|
266
266
|
|
267
267
|
describe "fetching a resource with absolute path" do
|
268
|
-
it "maps a single resource to a Ruby object" do
|
268
|
+
it "maps a single resource to a Ruby object" do
|
269
269
|
Foo::User.resource_path '/api/' + Foo::User.resource_path
|
270
270
|
@user = Foo::User.find(1, :_organization_id => 2)
|
271
271
|
@user.id.should == 1
|
272
272
|
@user.fullname.should == "Tobias Fünke"
|
273
|
-
end
|
273
|
+
end
|
274
274
|
end
|
275
275
|
|
276
276
|
describe "fetching a collection with absolute path" do
|
277
|
-
it "maps a collection of resources to an array of Ruby objects" do
|
277
|
+
it "maps a collection of resources to an array of Ruby objects" do
|
278
278
|
Foo::User.collection_path '/api/' + Foo::User.collection_path
|
279
279
|
@users = Foo::User.all(:_organization_id => 2)
|
280
280
|
@users.length.should == 2
|
281
281
|
@users.first.fullname.should == "Tobias Fünke"
|
282
|
-
end
|
282
|
+
end
|
283
283
|
end
|
284
284
|
end
|
285
285
|
end
|
@@ -3,77 +3,77 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
3
3
|
|
4
4
|
describe Her::Model::Relationships do
|
5
5
|
context "setting relationships without details" do
|
6
|
-
before do
|
6
|
+
before do
|
7
7
|
spawn_model "Foo::User"
|
8
|
-
end
|
8
|
+
end
|
9
9
|
|
10
|
-
it "handles a single 'has_many' relationship" do
|
10
|
+
it "handles a single 'has_many' relationship" do
|
11
11
|
Foo::User.has_many :comments
|
12
12
|
Foo::User.relationships[:has_many].should == [{ :name => :comments, :class_name => "Comment", :path => "/comments" }]
|
13
|
-
end
|
13
|
+
end
|
14
14
|
|
15
|
-
it "handles multiples 'has_many' relationship" do
|
15
|
+
it "handles multiples 'has_many' relationship" do
|
16
16
|
Foo::User.has_many :comments
|
17
17
|
Foo::User.has_many :posts
|
18
18
|
Foo::User.relationships[:has_many].should == [{ :name => :comments, :class_name => "Comment", :path => "/comments" }, { :name => :posts, :class_name => "Post", :path => "/posts" }]
|
19
|
-
end
|
19
|
+
end
|
20
20
|
|
21
|
-
it "handles a single 'has_one' relationship" do
|
21
|
+
it "handles a single 'has_one' relationship" do
|
22
22
|
Foo::User.has_one :category
|
23
23
|
Foo::User.relationships[:has_one].should == [{ :name => :category, :class_name => "Category", :path => "/category" }]
|
24
|
-
end
|
24
|
+
end
|
25
25
|
|
26
|
-
it "handles multiples 'has_one' relationship" do
|
26
|
+
it "handles multiples 'has_one' relationship" do
|
27
27
|
Foo::User.has_one :category
|
28
28
|
Foo::User.has_one :role
|
29
29
|
Foo::User.relationships[:has_one].should == [{ :name => :category, :class_name => "Category", :path => "/category" }, { :name => :role, :class_name => "Role", :path => "/role" }]
|
30
|
-
end
|
30
|
+
end
|
31
31
|
|
32
|
-
it "handles a single belongs_to relationship" do
|
32
|
+
it "handles a single belongs_to relationship" do
|
33
33
|
Foo::User.belongs_to :organization
|
34
34
|
Foo::User.relationships[:belongs_to].should == [{ :name => :organization, :class_name => "Organization", :foreign_key => "organization_id", :path => "/organizations/:id" }]
|
35
|
-
end
|
35
|
+
end
|
36
36
|
|
37
|
-
it "handles multiples 'belongs_to' relationship" do
|
37
|
+
it "handles multiples 'belongs_to' relationship" do
|
38
38
|
Foo::User.belongs_to :organization
|
39
39
|
Foo::User.belongs_to :family
|
40
40
|
Foo::User.relationships[:belongs_to].should == [{ :name => :organization, :class_name => "Organization", :foreign_key => "organization_id", :path => "/organizations/:id" }, { :name => :family, :class_name => "Family", :foreign_key => "family_id", :path => "/families/:id" }]
|
41
|
-
end
|
41
|
+
end
|
42
42
|
end
|
43
43
|
|
44
44
|
context "setting relationships with details" do
|
45
|
-
before do
|
45
|
+
before do
|
46
46
|
spawn_model "Foo::User"
|
47
|
-
end
|
47
|
+
end
|
48
48
|
|
49
|
-
it "handles a single 'has_many' relationship" do
|
49
|
+
it "handles a single 'has_many' relationship" do
|
50
50
|
Foo::User.has_many :comments, :class_name => "Post"
|
51
51
|
Foo::User.relationships[:has_many].should == [{ :name => :comments, :class_name => "Post", :path => "/comments" }]
|
52
|
-
end
|
52
|
+
end
|
53
53
|
|
54
|
-
it "handles a single 'has_one' relationship" do
|
54
|
+
it "handles a single 'has_one' relationship" do
|
55
55
|
Foo::User.has_one :category, :class_name => "Topic", :foreign_key => "topic_id"
|
56
56
|
Foo::User.relationships[:has_one].should == [{ :name => :category, :class_name => "Topic", :foreign_key => "topic_id", :path => "/category" }]
|
57
|
-
end
|
57
|
+
end
|
58
58
|
|
59
|
-
it "handles a single belongs_to relationship" do
|
59
|
+
it "handles a single belongs_to relationship" do
|
60
60
|
Foo::User.belongs_to :organization, :class_name => "Business", :foreign_key => "org_id"
|
61
61
|
Foo::User.relationships[:belongs_to].should == [{ :name => :organization, :class_name => "Business", :foreign_key => "org_id", :path => "/organizations/:id" }]
|
62
|
-
end
|
62
|
+
end
|
63
63
|
|
64
64
|
context "inheriting relationships from a superclass" do
|
65
|
-
it "copies relationships to the subclass" do
|
65
|
+
it "copies relationships to the subclass" do
|
66
66
|
Foo::User.has_many :comments, :class_name => "Post"
|
67
67
|
subclass = Class.new(Foo::User)
|
68
68
|
subclass.relationships.object_id.should_not == Foo::User.relationships.object_id
|
69
69
|
subclass.relationships[:has_many].length.should == 1
|
70
70
|
subclass.relationships[:has_many].first[:class_name].should == "Post"
|
71
|
-
end
|
71
|
+
end
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
75
|
context "handling relationships without details" do
|
76
|
-
before do
|
76
|
+
before do
|
77
77
|
Her::API.setup :url => "https://api.example.com" do |builder|
|
78
78
|
builder.use Her::Middleware::FirstLevelParseJSON
|
79
79
|
builder.use Faraday::Request::UrlEncoded
|
@@ -101,61 +101,61 @@ describe Her::Model::Relationships do
|
|
101
101
|
|
102
102
|
@user_with_included_data = Foo::User.find(1)
|
103
103
|
@user_without_included_data = Foo::User.find(2)
|
104
|
-
end
|
104
|
+
end
|
105
105
|
|
106
|
-
it "maps an array of included data through has_many" do
|
106
|
+
it "maps an array of included data through has_many" do
|
107
107
|
@user_with_included_data.comments.first.should be_a(Foo::Comment)
|
108
108
|
@user_with_included_data.comments.length.should == 2
|
109
109
|
@user_with_included_data.comments.first.id.should == 2
|
110
110
|
@user_with_included_data.comments.first.body.should == "Tobias, you blow hard!"
|
111
|
-
end
|
111
|
+
end
|
112
112
|
|
113
|
-
it "fetches data that was not included through has_many" do
|
113
|
+
it "fetches data that was not included through has_many" do
|
114
114
|
@user_without_included_data.comments.first.should be_a(Foo::Comment)
|
115
115
|
@user_without_included_data.comments.length.should == 2
|
116
116
|
@user_without_included_data.comments.first.id.should == 4
|
117
117
|
@user_without_included_data.comments.first.body.should == "They're having a FIRESALE?"
|
118
|
-
end
|
118
|
+
end
|
119
119
|
|
120
|
-
it "fetches has_many data even if it was included, only if called with parameters" do
|
120
|
+
it "fetches has_many data even if it was included, only if called with parameters" do
|
121
121
|
@user_with_included_data.comments(:foo_id => 1).length.should == 1
|
122
|
-
end
|
122
|
+
end
|
123
123
|
|
124
|
-
it "maps an array of included data through has_one" do
|
124
|
+
it "maps an array of included data through has_one" do
|
125
125
|
@user_with_included_data.role.should be_a(Foo::Role)
|
126
126
|
@user_with_included_data.role.id.should == 1
|
127
127
|
@user_with_included_data.role.body.should == "Admin"
|
128
|
-
end
|
128
|
+
end
|
129
129
|
|
130
|
-
it "fetches data that was not included through has_one" do
|
130
|
+
it "fetches data that was not included through has_one" do
|
131
131
|
@user_without_included_data.role.should be_a(Foo::Role)
|
132
132
|
@user_without_included_data.role.id.should == 2
|
133
133
|
@user_without_included_data.role.body.should == "User"
|
134
|
-
end
|
134
|
+
end
|
135
135
|
|
136
|
-
it "fetches has_one data even if it was included, only if called with parameters" do
|
136
|
+
it "fetches has_one data even if it was included, only if called with parameters" do
|
137
137
|
@user_with_included_data.role(:foo_id => 2).id.should == 3
|
138
|
-
end
|
138
|
+
end
|
139
139
|
|
140
|
-
it "maps an array of included data through belongs_to" do
|
140
|
+
it "maps an array of included data through belongs_to" do
|
141
141
|
@user_with_included_data.organization.should be_a(Foo::Organization)
|
142
142
|
@user_with_included_data.organization.id.should == 1
|
143
143
|
@user_with_included_data.organization.name.should == "Bluth Company"
|
144
|
-
end
|
144
|
+
end
|
145
145
|
|
146
|
-
it "fetches data that was not included through belongs_to" do
|
146
|
+
it "fetches data that was not included through belongs_to" do
|
147
147
|
@user_without_included_data.organization.should be_a(Foo::Organization)
|
148
148
|
@user_without_included_data.organization.id.should == 2
|
149
149
|
@user_without_included_data.organization.name.should == "Bluth Company"
|
150
|
-
end
|
150
|
+
end
|
151
151
|
|
152
|
-
it "fetches belongs_to data even if it was included, only if called with parameters" do
|
152
|
+
it "fetches belongs_to data even if it was included, only if called with parameters" do
|
153
153
|
@user_with_included_data.organization(:foo_id => 1).name.should == "Bluth Company Foo"
|
154
|
-
end
|
154
|
+
end
|
155
155
|
end
|
156
156
|
|
157
157
|
context "handling relationships with details" do
|
158
|
-
before do
|
158
|
+
before do
|
159
159
|
Her::API.setup :url => "https://api.example.com" do |builder|
|
160
160
|
builder.use Her::Middleware::FirstLevelParseJSON
|
161
161
|
builder.use Faraday::Request::UrlEncoded
|
@@ -176,22 +176,22 @@ describe Her::Model::Relationships do
|
|
176
176
|
@user_with_included_data = Foo::User.find(1)
|
177
177
|
@user_without_included_data = Foo::User.find(2)
|
178
178
|
@user_with_included_nil_data = Foo::User.find(3)
|
179
|
-
end
|
179
|
+
end
|
180
180
|
|
181
|
-
it "maps an array of included data through belongs_to" do
|
181
|
+
it "maps an array of included data through belongs_to" do
|
182
182
|
@user_with_included_data.company.should be_a(Foo::Company)
|
183
183
|
@user_with_included_data.company.id.should == 1
|
184
184
|
@user_with_included_data.company.name.should == "Bluth Company"
|
185
|
-
end
|
185
|
+
end
|
186
186
|
|
187
|
-
it "does not map included data if it’s nil" do
|
187
|
+
it "does not map included data if it’s nil" do
|
188
188
|
@user_with_included_nil_data.organization.should be_nil
|
189
|
-
end
|
189
|
+
end
|
190
190
|
|
191
|
-
it "fetches data that was not included through belongs_to" do
|
191
|
+
it "fetches data that was not included through belongs_to" do
|
192
192
|
@user_without_included_data.company.should be_a(Foo::Company)
|
193
193
|
@user_without_included_data.company.id.should == 1
|
194
194
|
@user_without_included_data.company.name.should == "Bluth Company"
|
195
|
-
end
|
195
|
+
end
|
196
196
|
end
|
197
197
|
end
|