can_be 0.2.1 → 0.3.0

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.
@@ -174,6 +174,10 @@ describe CanBe::ModelExtensions do
174
174
  it "uses the specified default_type option" do
175
175
  Person.new.should be_female
176
176
  end
177
+
178
+ it "uses the specified default_type block option" do
179
+ BlockOption.new.should be_option_2
180
+ end
177
181
  end
178
182
 
179
183
  context "database field" do
@@ -184,6 +188,10 @@ describe CanBe::ModelExtensions do
184
188
  it "uses the specified field" do
185
189
  Person.new_female.gender.should == 'female'
186
190
  end
191
+
192
+ it "uses the specified field (block options)" do
193
+ BlockOption.new_option_1.option_type.should == 'option_1'
194
+ end
187
195
  end
188
196
 
189
197
  context "validity of type value" do
@@ -197,195 +205,14 @@ describe CanBe::ModelExtensions do
197
205
  end
198
206
  end
199
207
 
200
- context "details" do
201
- context "persistence" do
202
- it "persists the details information to the database (create method)" do
203
- upload = Upload.create
204
- upload.details.format = "jpeg"
205
- upload.save
206
- ImageUploadDetail.first.format.should == "jpeg"
207
- end
208
-
209
- it "persists the details information to the database (create_image_upload method)" do
210
- upload = Upload.create_image_upload
211
- upload.details.format = "jpeg"
212
- upload.save
213
- ImageUploadDetail.first.format.should == "jpeg"
214
- end
215
-
216
- it "persists the details information to the database (new method)" do
217
- upload = Upload.new
218
- upload.details.format = "jpeg"
219
- upload.save
220
- ImageUploadDetail.first.format.should == "jpeg"
221
- end
222
-
223
- it "persists the details information to the database (new_image_upload method)" do
224
- upload = Upload.new_image_upload
225
- upload.details.format = "jpeg"
226
- upload.save
227
- ImageUploadDetail.first.format.should == "jpeg"
228
- end
229
-
230
- it "persists only one details record" do
231
- upload = Upload.new
232
- upload.details.format = "jpeg"
233
- upload.save
234
- found_upload = Upload.find(upload.id)
235
- found_upload.details.format.should == "jpeg"
236
- found_upload.save
237
- ImageUploadDetail.count.should == 1
238
- end
239
-
240
- it "deletes the details record" do
241
- u = Upload.create_image_upload
242
- ImageUploadDetail.count.should == 1
243
- u.destroy
244
- ImageUploadDetail.count.should == 0
245
- end
246
- end
247
-
248
- context "create method" do
249
- it "creates the correct details record" do
250
- Upload.create_image_upload.details.should be_instance_of(ImageUploadDetail)
251
- Upload.create_video_upload.details.should be_instance_of(VideoUploadDetail)
252
- end
253
-
254
- it "doesn't create details record if model doesn't call #can_be_detail" do
255
- Upload.create_thumbnail_upload.details.should be_nil
256
- end
257
-
258
- it "doesn't create details record if model doesn't exist" do
259
- Upload.create_pdf_upload.details.should be_nil
260
- end
261
-
262
- it "doesn't create details record unless an ActiveRecord model" do
263
- Upload.create_document_upload.details.should be_nil
264
- end
265
-
266
- it "persists the details record to the database" do
267
- Upload.create_image_upload
268
- ImageUploadDetail.count.should == 1
269
- end
270
- end
271
-
272
- context "new method" do
273
- it "instantiates the correct details record" do
274
- Upload.new_image_upload.details.should be_instance_of(ImageUploadDetail)
275
- Upload.new_video_upload.details.should be_instance_of(VideoUploadDetail)
276
- end
277
-
278
- it "doesn't instantiate details record if model doesn't call #can_be_detail" do
279
- Upload.new_thumbnail_upload.details.should be_nil
280
- end
281
-
282
- it "doesn't instantiate details record if model doesn't exist" do
283
- Upload.new_pdf_upload.details.should be_nil
284
- end
285
-
286
- it "doesn't instantiate details record unless an ActiveRecord model" do
287
- Upload.new_document_upload.details.should be_nil
288
- end
289
-
290
- it "doesn't persist the details record to the database" do
291
- Upload.new_image_upload
292
- ImageUploadDetail.count.should == 0
293
- end
294
- end
295
-
296
- context "change type via #change_to" do
297
- it "changes the details record type" do
298
- u = Upload.new_image_upload
299
- u.change_to_video_upload
300
- u.details.should be_instance_of(VideoUploadDetail)
301
- end
302
-
303
- it "changes the details to nil" do
304
- u = Upload.new_image_upload
305
- u.change_to_thumbnail_upload
306
- u.details.should be_nil
307
- end
308
-
309
- it "doesn't create a new record in the database" do
310
- u = Upload.new_image_upload
311
- u.change_to_video_upload
312
- ImageUploadDetail.count.should == 0
313
- VideoUploadDetail.count.should == 0
314
- end
315
-
316
- it "has access to the original details if not saved" do
317
- u = Upload.create_image_upload
318
- u.change_to_video_upload
319
- Upload.find(u.id).details.should be_instance_of(ImageUploadDetail)
320
- end
321
- end
322
-
323
- context "change type via #change_to!" do
324
- it "changes the details record type" do
325
- u = Upload.create_video_upload
326
- u.change_to_image_upload!
327
- Upload.find(u.id).details.should be_instance_of(ImageUploadDetail)
328
- end
329
-
330
- it "changes the details to nil" do
331
- u = Upload.create_image_upload
332
- u.change_to_thumbnail_upload!
333
- Upload.find(u.id).details.should be_nil
334
- end
335
-
336
- it "doesn't create a new record in the database" do
337
- u = Upload.create_video_upload
338
- u.change_to_image_upload!
339
- ImageUploadDetail.count.should == 1
340
- end
341
-
342
- it "removes the old database record from the database" do
343
- u = Upload.create_image_upload
344
- u.change_to_video_upload!
345
- ImageUploadDetail.count.should == 0
346
- end
347
- end
348
-
349
- context "change type setting the model attribute" do
350
- it "changes the details record type" do
351
- u = Upload.new_image_upload
352
- u.can_be_type = "video_upload"
353
- u.details.should be_instance_of(VideoUploadDetail)
354
- end
355
-
356
- it "changes the details to nil" do
357
- u = Upload.new_image_upload
358
- u.can_be_type = "thumbnail_upload"
359
- u.details.should be_nil
360
- end
361
-
362
- it "doesn't create a new record in the database" do
363
- u = Upload.new_image_upload
364
- u.can_be_type = "video_upload"
365
- ImageUploadDetail.count.should == 0
366
- VideoUploadDetail.count.should == 0
367
- end
368
-
369
- it "has access to the original details if not saved" do
370
- u = Upload.create_image_upload
371
- u.can_be_type = "video_upload"
372
- Upload.find(u.id).details.should be_instance_of(ImageUploadDetail)
373
- end
374
- end
375
- end
208
+ it_behaves_like "it has details", Upload, ImageUploadDetail, VideoUploadDetail
209
+ it_behaves_like "it has details", CustomUpload, CustomImageUploadDetail, CustomVideoUploadDetail, :custom_details
210
+ it_behaves_like "it has history"
376
211
  end
377
212
 
378
213
  context "#can_be_details" do
379
- it "implements has_one to the can_be model" do
380
- u = Upload.create_image_upload
381
- ImageUploadDetail.first.upload.should == u
382
- end
383
-
384
- it "deletes the can_be model" do
385
- Upload.create_image_upload
386
- Upload.count.should == 1
387
- ImageUploadDetail.first.destroy
388
- Upload.count.should == 0
389
- end
214
+ it_behaves_like "it is a details model", Upload, ImageUploadDetail
215
+ it_behaves_like "it is a details model", CustomUpload, CustomImageUploadDetail
216
+ it_behaves_like "it is a details model with history"
390
217
  end
391
218
  end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe "CanBe::RSpec::Matchers::CanBeDetailMatcher" do
4
+ it "matches when the correct can_be model is passed" do
5
+ VideoUploadDetail.should implement_can_be_detail(:upload)
6
+ end
7
+
8
+ it "matches when the correct can_be model and details_name are passed" do
9
+ CustomVideoUploadDetail.should implement_can_be_detail(:custom_upload, :custom_details)
10
+ end
11
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe "CanBe::RSpec::Matchers::CanBeMatcher" do
4
+ it "doesn't match that can_be is implemented if a list of types isn't provided" do
5
+ Address.should_not implement_can_be
6
+ end
7
+
8
+ it "matches with a list of can_be types" do
9
+ Address.should implement_can_be(:home_address, :work_address, :vacation_address)
10
+ end
11
+
12
+ it "doesn't match with an incorrect list of can_be types" do
13
+ Address.should_not implement_can_be(:home, :work, :vacation)
14
+ end
15
+
16
+ it "matches with a list of can_be types out of order" do
17
+ Address.should implement_can_be(:vacation_address, :work_address, :home_address)
18
+ end
19
+
20
+ it "matches when the default type is specified" do
21
+ Person.should implement_can_be(:male, :female).with_default_type(:female)
22
+ end
23
+
24
+ it "doesn't match with an incorrect default type" do
25
+ Person.should_not implement_can_be(:male, :female).with_default_type(:male)
26
+ end
27
+
28
+ it "matches when the field name is specified" do
29
+ Person.should implement_can_be(:male, :female).with_field_name(:gender)
30
+ end
31
+
32
+ it "doesn't match when the field name is incorrect" do
33
+ Person.should_not implement_can_be(:male, :female).with_field_name(:not_gender)
34
+ end
35
+
36
+ it "matches when the details name is specified" do
37
+ CustomUpload.should implement_can_be(:image_upload, :video_upload, :thumbnail_upload, :document_upload, :pdf_upload)
38
+ .with_details_name(:custom_details)
39
+ end
40
+
41
+ it "doesn't match when the details name is incorrect" do
42
+ CustomUpload.should_not implement_can_be(:image_upload, :video_upload, :thumbnail_upload, :document_upload, :pdf_upload)
43
+ .with_details_name(:custom_details_that_is_in_correct)
44
+ end
45
+
46
+ it "matches when the details models are specified" do
47
+ CustomUpload.should implement_can_be(:image_upload, :video_upload, :thumbnail_upload, :document_upload, :pdf_upload)
48
+ .and_has_details(:image_upload, :custom_image_upload_detail)
49
+ .and_has_details(:video_upload, :custom_video_upload_detail)
50
+ end
51
+
52
+ it "doesn't match when the details models are incorrect" do
53
+ CustomUpload.should_not implement_can_be(:image_upload, :video_upload, :thumbnail_upload, :document_upload, :pdf_upload)
54
+ .and_has_details(:image_uploads, :custom_image_upload_detail)
55
+ .and_has_details(:video_uploads, :custom_video_upload_detail)
56
+ end
57
+ end
@@ -12,6 +12,12 @@ ActiveRecord::Base.send(:include, CanBe::ModelExtensions)
12
12
  # now that we have the database configured, we can create the models and
13
13
  # migrate the database
14
14
  require 'support/models'
15
+ require 'support/can_be_shared_examples'
16
+ require 'support/can_be_history_shared_examples'
17
+ require 'support/can_be_detail_shared_examples'
18
+ require 'support/can_be_detail_history_shared_examples'
19
+
20
+ require 'can_be/rspec/matchers'
15
21
 
16
22
  RSpec.configure do |config|
17
23
  config.include ModelMacros
@@ -0,0 +1,17 @@
1
+ shared_examples "it is a details model with history" do
2
+ context "unattached details records" do
3
+ it "finds the correct can_be record" do
4
+ u = HistoryUpload.create_video_upload
5
+ u.change_to_image_upload!
6
+ HistoryVideoUploadDetail.first.history_upload.should == u
7
+ end
8
+
9
+ it "finds the correct can_be record given duplicate id's" do
10
+ u1 = HistoryUpload.create_video_upload
11
+ u2 = HistoryUpload.create_image_upload
12
+ u1.change_to_image_upload!
13
+ u2.change_to_video_upload!
14
+ HistoryImageUploadDetail.first.history_upload.should == u2
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ shared_examples "it is a details model" do |can_be_class, image_class|
2
+ it "implements has_one to the can_be model" do
3
+ u = can_be_class.create_image_upload
4
+ image_class.first.send(can_be_class.name.underscore).should == u
5
+ end
6
+
7
+ it "deletes the can_be model" do
8
+ can_be_class.create_image_upload
9
+ can_be_class.count.should == 1
10
+ image_class.first.destroy
11
+ can_be_class.count.should == 0
12
+ end
13
+ end
@@ -0,0 +1,135 @@
1
+ shared_examples "it has history" do
2
+ context "details models" do
3
+ it "doesn't delete the details record after changing the can_be type" do
4
+ u = HistoryUpload.create_image_upload
5
+ u.change_to_video_upload!
6
+ HistoryImageUploadDetail.count.should == 1
7
+ HistoryVideoUploadDetail.count.should == 1
8
+ end
9
+
10
+ it "doesn't create additional details records when switching the types back and forth" do
11
+ u = HistoryUpload.create_image_upload
12
+ u.change_to_video_upload!
13
+ u.change_to_image_upload!
14
+ HistoryImageUploadDetail.count.should == 1
15
+ HistoryVideoUploadDetail.count.should == 1
16
+ end
17
+
18
+ it "sets the details to the original value when swtiching the type back" do
19
+ u = HistoryUpload.create_image_upload
20
+ u.change_to_video_upload!
21
+ u.change_to_image_upload!
22
+ u.details.should == HistoryImageUploadDetail.first
23
+ end
24
+ end
25
+
26
+ context "history model" do
27
+ it "stores a history record after creation in the history model" do
28
+ u = HistoryUpload.create_image_upload
29
+ HistoryUploadHistoryRecord.count.should == 1
30
+ end
31
+
32
+ it "stores a history record after new and save in the history model" do
33
+ u = HistoryUpload.new_image_upload
34
+ u.save
35
+ HistoryUploadHistoryRecord.count.should == 1
36
+ end
37
+
38
+ it "stores the correct information in the history table" do
39
+ u = HistoryUpload.create_image_upload
40
+ h = HistoryUploadHistoryRecord.first
41
+ h.can_be_model_id.should == u.id
42
+ h.can_be_type.should == u.can_be_type
43
+ h.can_be_details_id.should == u.details.id
44
+ end
45
+
46
+ context "history fields" do
47
+ before :each do
48
+ @u = HistoryUpload.create_image_upload
49
+ @u.change_to_video_upload!
50
+ HistoryUploadHistoryRecord.count.should == 2
51
+ @h = HistoryUploadHistoryRecord.last
52
+ end
53
+
54
+ it "stores the correct can_be_model_id value" do
55
+ @h.can_be_model_id.should == @u.id
56
+ end
57
+
58
+ it "stores the correct can_be_type value" do
59
+ @h.can_be_type.should == @u.can_be_type
60
+ end
61
+
62
+ it "stores the correct can_be_details_id value" do
63
+ @h.can_be_details_id.should == @u.details.id
64
+ end
65
+
66
+ it "stores the correct can_be_details_type" do
67
+ @h.can_be_details_type.should == HistoryVideoUploadDetail.name.underscore.to_s
68
+ end
69
+ end
70
+
71
+ it "doesn't store an additional history record when changing back to the original type" do
72
+ u = HistoryUpload.create_image_upload
73
+ u.change_to_video_upload
74
+ u.save
75
+ u.change_to_image_upload!
76
+ HistoryUploadHistoryRecord.count.should == 2
77
+ end
78
+
79
+ context "destroy can_be record" do
80
+ before :each do
81
+ u = HistoryUpload.create_video_upload
82
+ u.change_to_image_upload!
83
+ u.destroy
84
+ end
85
+
86
+ it "destroys all details records" do
87
+ HistoryImageUploadDetail.count.should == 0
88
+ HistoryVideoUploadDetail.count.should == 0
89
+ end
90
+
91
+ it "destroys all of the history records" do
92
+ HistoryUploadHistoryRecord.count.should == 0
93
+ end
94
+ end
95
+
96
+ context "change_to! force destory" do
97
+ before :each do
98
+ @u = HistoryUpload.create_image_upload
99
+ @u.change_to_video_upload!(true)
100
+ end
101
+
102
+ it "destroys the details record" do
103
+ HistoryImageUploadDetail.count.should == 0
104
+ end
105
+
106
+ it "keeps the correct details record" do
107
+ HistoryVideoUploadDetail.count.should == 1
108
+ end
109
+
110
+ it "destroys the history record" do
111
+ HistoryUploadHistoryRecord.count.should == 1
112
+ end
113
+ end
114
+
115
+ context "change_to force destory" do
116
+ before :each do
117
+ @u = HistoryUpload.create_image_upload
118
+ @u.change_to_video_upload(true)
119
+ @u.save
120
+ end
121
+
122
+ it "destroys the details record" do
123
+ HistoryImageUploadDetail.count.should == 0
124
+ end
125
+
126
+ it "keeps the correct details record" do
127
+ HistoryVideoUploadDetail.count.should == 1
128
+ end
129
+
130
+ it "destroys the history record" do
131
+ HistoryUploadHistoryRecord.count.should == 1
132
+ end
133
+ end
134
+ end
135
+ end