freeform 1.0.11 → 2.0.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.
@@ -1,645 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FreeForm::Form do
4
- let!(:task_class) do
5
- klass = Class.new(FreeForm::Form) do
6
- form_input_key :task
7
- form_model :task
8
- validate_models
9
- allow_destroy_on_save
10
-
11
- property :name, :on => :task
12
- property :start_date, :on => :task
13
- property :end_date, :on => :task
14
- end
15
- # This wrapper just avoids CONST warnings
16
- v, $VERBOSE = $VERBOSE, nil
17
- Module.const_set("TaskForm", klass)
18
- $VERBOSE = v
19
- klass
20
- end
21
-
22
- let(:form_class) do
23
- klass = Class.new(FreeForm::Form) do
24
- form_input_key :company
25
- form_models :company
26
- child_model :project do
27
- company.project.present? ? company.project : company.build_project
28
- end
29
- validate_models
30
- allow_destroy_on_save
31
-
32
- property :name, :on => :company, :as => :company_name
33
- property :name, :on => :project, :as => :project_name
34
- property :due_date, :on => :project
35
-
36
- has_many :tasks, :class => Module::TaskForm, :default_initializer => :default_task_initializer
37
-
38
- def default_task_initializer
39
- { :task => project.tasks.build }
40
- end
41
- end
42
- # This wrapper just avoids CONST warnings
43
- v, $VERBOSE = $VERBOSE, nil
44
- Module.const_set("AcceptanceForm", klass)
45
- $VERBOSE = v
46
- klass
47
- end
48
-
49
- let(:company) { Company.create!(:name => "Demo Corporation") }
50
- let(:project) { Project.create!(:company => company, :name => "Widget", :due_date => Date.new(2014, 1, 1)) }
51
- let(:task_1) { Task.create!(:project => project, :name => "Task 1", :start_date => Date.new(2014, 2, 2), :end_date => Date.new(2014, 3, 3)) }
52
- let(:task_2) { Task.create!(:project => project, :name => "Task 2", :start_date => Date.new(2014, 9, 1), :end_date => Date.new(2014, 10, 1)) }
53
-
54
- let(:form) do
55
- f = form_class.new( :company => company )
56
- company.project.tasks.each do |task|
57
- f.build_task(:task => task)
58
- end
59
- f
60
- end
61
-
62
- before(:each) do
63
- company.reload
64
- project.reload
65
- task_1.reload
66
- task_2.reload
67
- end
68
-
69
- describe "form initialization", :initialization => true do
70
- it "initializes with Company model" do
71
- form.company.should eq(company)
72
- end
73
-
74
- it "initializes with Project model" do
75
- form.project.should eq(project)
76
- end
77
-
78
- it "initializes with Task model" do
79
- form.tasks.first.task.should eq(task_1)
80
- end
81
-
82
- it "initializes with Task with project parent" do
83
- task = form.tasks.first.task
84
- task.project.should eq(form.project)
85
- end
86
- end
87
-
88
- describe "building nested models", :nested_models => true do
89
- it "can build multiple tasks" do
90
- form.tasks.count.should eq(2)
91
- form.build_task
92
- form.tasks.count.should eq(3)
93
- end
94
-
95
- it "build new models for each task" do
96
- form.build_task
97
- task_1 = form.tasks.first.task
98
- task_2 = form.tasks.last.task
99
- task_1.should_not eq(task_2)
100
- end
101
-
102
- it "builds new tasks with project parent" do
103
- form.build_task
104
- task_1 = form.tasks.first.task
105
- task_2 = form.tasks.last.task
106
- task_1.project.should eq(task_2.project)
107
- end
108
- end
109
-
110
- describe "assigning parameters", :assign_params => true do
111
- let(:attributes) do {
112
- :company_name => "dummycorp",
113
- :project_name => "railsapp",
114
- "due_date(1i)" => "2014",
115
- "due_date(2i)" => "10",
116
- "due_date(3i)" => "30",
117
- :tasks_attributes => {
118
- "0" => {
119
- :name => "task_1",
120
- "start_date(1i)" => "2012",
121
- "start_date(2i)" => "1",
122
- "start_date(3i)" => "2",
123
- },
124
- "1" => {
125
- :name => "task_2",
126
- "end_date(1i)" => "2011",
127
- "end_date(2i)" => "12",
128
- "end_date(3i)" => "15",
129
- }
130
- } }
131
- end
132
-
133
- before(:each) do
134
- form.fill(attributes)
135
- end
136
-
137
- it "assigns company name" do
138
- form.company_name.should eq("dummycorp")
139
- form.company.name.should eq("dummycorp")
140
- end
141
-
142
- it "assigns project name" do
143
- form.project_name.should eq("railsapp")
144
- form.project.name.should eq("railsapp")
145
- end
146
-
147
- it "assigns project due date" do
148
- form.due_date.should eq(Date.new(2014, 10, 30))
149
- form.project.due_date.should eq(Date.new(2014, 10, 30))
150
- end
151
-
152
- it "builds new task automatically" do
153
- form.tasks.count.should eq(2)
154
- end
155
-
156
- it "assigns first task name" do
157
- form.tasks.first.task.name.should eq("task_1")
158
- end
159
-
160
- it "assigns first task start_date" do
161
- form.tasks.first.task.start_date.should eq(Date.new(2012, 1, 2))
162
- end
163
-
164
- it "assigns second task name" do
165
- form.tasks.last.task.name.should eq("task_2")
166
- end
167
-
168
- it "assigns second task end_date" do
169
- form.tasks.last.task.end_date.should eq(Date.new(2011, 12, 15))
170
- end
171
- end
172
-
173
- describe "validations", :validations => true do
174
- context "with invalid attributes" do
175
- let(:attributes) do {
176
- :company_name => "dummycorp",
177
- :project_name => nil,
178
- "due_date(1i)" => "2014",
179
- "due_date(2i)" => "10",
180
- "due_date(3i)" => "30",
181
- :tasks_attributes => {
182
- "0" => {
183
- :name => "task_1",
184
- "start_date(1i)" => "2012",
185
- "start_date(2i)" => "1",
186
- "start_date(3i)" => "2",
187
- },
188
- "1" => {
189
- :name => "task_2",
190
- "end_date(1i)" => "2011",
191
- "end_date(2i)" => "12",
192
- "end_date(3i)" => "15",
193
- }
194
- } }
195
- end
196
-
197
- before(:each) do
198
- form.fill(attributes)
199
- form.valid?
200
- end
201
-
202
- it "should be invalid" do
203
- form.should_not be_valid
204
- end
205
-
206
- it "should have errors on project name" do
207
- form.errors[:project_name].should eq(["can't be blank"])
208
- end
209
-
210
- it "should not have errors on last tasks's start_date" do
211
- form.tasks.last.errors[:start_date].should be_empty
212
- end
213
- end
214
-
215
- context "with invalid nested model only" do
216
- let(:attributes) do {
217
- :company_name => "dummycorp",
218
- :project_name => "rails app",
219
- "due_date(1i)" => "2014",
220
- "due_date(2i)" => "10",
221
- "due_date(3i)" => "30",
222
- :tasks_attributes => {
223
- "0" => {
224
- :name => "task_1",
225
- "start_date(1i)" => nil,
226
- "start_date(2i)" => nil,
227
- "start_date(3i)" => nil,
228
- },
229
- "1" => {
230
- :name => "task_2",
231
- "end_date(1i)" => "2011",
232
- "end_date(2i)" => "12",
233
- "end_date(3i)" => "15",
234
- }
235
- } }
236
- end
237
-
238
- before(:each) do
239
- form.fill(attributes)
240
- form.valid?
241
- end
242
-
243
- it "should be invalid" do
244
- form.should_not be_valid
245
- end
246
-
247
- it "should have errors on first tasks's start_date" do
248
- form.tasks.first.errors[:start_date].should eq(["can't be blank"])
249
- end
250
- end
251
-
252
- context "with valid attributes" do
253
- let(:attributes) do {
254
- :company_name => "dummycorp",
255
- :project_name => "railsapp",
256
- "due_date(1i)" => "2014",
257
- "due_date(2i)" => "10",
258
- "due_date(3i)" => "30",
259
- :tasks_attributes => {
260
- "0" => {
261
- :name => "task_1",
262
- "start_date(1i)" => "2012",
263
- "start_date(2i)" => "1",
264
- "start_date(3i)" => "2",
265
- },
266
- "1" => {
267
- :name => "task_2",
268
- "start_date(1i)" => "2011",
269
- "start_date(2i)" => "8",
270
- "start_date(3i)" => "15",
271
- "end_date(1i)" => "2011",
272
- "end_date(2i)" => "12",
273
- "end_date(3i)" => "15",
274
- }
275
- } }
276
- end
277
-
278
- before(:each) do
279
- form.fill(attributes)
280
- end
281
-
282
- it "should be valid" do
283
- form.should be_valid
284
- end
285
- end
286
- end
287
-
288
- describe "saving and destroying", :saving => true do
289
- context "with invalid attributes" do
290
- let(:attributes) do {
291
- :company_name => "dummycorp",
292
- :project_name => nil,
293
- "due_date(1i)" => "2014",
294
- "due_date(2i)" => "10",
295
- "due_date(3i)" => "30",
296
- :tasks_attributes => {
297
- "0" => {
298
- :name => "task_1",
299
- "start_date(1i)" => "2012",
300
- "start_date(2i)" => "1",
301
- "start_date(3i)" => "2",
302
- },
303
- "1" => {
304
- :name => "task_2",
305
- "end_date(1i)" => "2011",
306
- "end_date(2i)" => "12",
307
- "end_date(3i)" => "15",
308
- }
309
- } }
310
- end
311
-
312
- before(:each) do
313
- form.fill(attributes)
314
- end
315
-
316
- it "should return false on 'save'" do
317
- form.save.should be_false
318
- end
319
-
320
- it "should raise error on 'save!'" do
321
- expect{ form.save! }.to raise_error(FreeForm::FormInvalid)
322
- end
323
- end
324
-
325
- context "with invalid, marked for destruction attributes", :failing => true do
326
- let(:attributes) do {
327
- :company_name => "dummycorp",
328
- :project_name => "my_project",
329
- "due_date(1i)" => "2014",
330
- "due_date(2i)" => "10",
331
- "due_date(3i)" => "30",
332
- :tasks_attributes => {
333
- "0" => {
334
- :name => "task_1",
335
- "start_date(1i)" => "2012",
336
- "start_date(2i)" => "1",
337
- "start_date(3i)" => "2",
338
- },
339
- "1" => {
340
- :name => "task_2",
341
- "end_date(1i)" => "2011",
342
- "end_date(2i)" => "12",
343
- "end_date(3i)" => "15",
344
- :_destroy => "1"
345
- }
346
- } }
347
- end
348
-
349
- before(:each) do
350
- form.fill(attributes)
351
- end
352
-
353
- it "should return true on 'save'" do
354
- form.save.should be_true
355
- end
356
-
357
- it "should not raise error on 'save!'" do
358
- expect{ form.save! }.to_not raise_error
359
- end
360
- end
361
-
362
- context "with valid attributes" do
363
- let(:attributes) do {
364
- :company_name => "dummycorp",
365
- :project_name => "railsapp",
366
- "due_date(1i)" => "2014",
367
- "due_date(2i)" => "10",
368
- "due_date(3i)" => "30",
369
- :tasks_attributes => {
370
- "0" => {
371
- :name => "new_task_1",
372
- "start_date(1i)" => "2012",
373
- "start_date(2i)" => "1",
374
- "start_date(3i)" => "2",
375
- },
376
- "1" => {
377
- :name => "new_task_2",
378
- "start_date(1i)" => "2011",
379
- "start_date(2i)" => "8",
380
- "start_date(3i)" => "15",
381
- "end_date(1i)" => "2011",
382
- "end_date(2i)" => "12",
383
- "end_date(3i)" => "15",
384
- }
385
- } }
386
- end
387
-
388
- before(:each) do
389
- form.fill(attributes)
390
- end
391
-
392
- describe "save", :save => true do
393
- describe "without destroy" do
394
- it "should return true on 'save', and call save on other models" do
395
- form.company.should_receive(:save).and_return(true)
396
- form.project.should_receive(:save).and_return(true)
397
- form.tasks.first.task.should_receive(:save).and_return(true)
398
- form.tasks.last.task.should_receive(:save).and_return(true)
399
- form.save
400
- end
401
-
402
- describe "value assignment and persistence" do
403
- before(:each) { form.save }
404
-
405
- it "assign company values" do
406
- company.reload
407
- company.name.should eq("dummycorp")
408
- end
409
-
410
- it "assigns project values" do
411
- project.reload
412
- project.name.should eq("railsapp")
413
- project.due_date.should eq(Date.new(2014, 10, 30))
414
- end
415
-
416
- it "assigns task_1 values" do
417
- task_1.reload
418
- task_1.name.should eq("new_task_1")
419
- task_1.start_date.should eq(Date.new(2012, 1, 2))
420
- task_1.end_date.should eq(Date.new(2014, 3, 3))
421
- end
422
-
423
- it "assigns task_1 values" do
424
- task_2.reload
425
- task_2.name.should eq("new_task_2")
426
- task_2.start_date.should eq(Date.new(2011, 8, 15))
427
- task_2.end_date.should eq(Date.new(2011, 12, 15))
428
- end
429
- end
430
- end
431
-
432
- describe "with destroy" do
433
- it "destroys models on save if set" do
434
- form._destroy = true
435
- form.company.should_receive(:destroy).and_return(true)
436
- form.project.should_receive(:destroy).and_return(true)
437
- form.tasks.first.task.should_receive(:save).and_return(true)
438
- form.tasks.last.task.should_receive(:save).and_return(true)
439
- form.save
440
- end
441
-
442
- it "destroys models on save if set through attribute" do
443
- form.fill({:_destroy => "1"})
444
- form.company.should_receive(:destroy).and_return(true)
445
- form.project.should_receive(:destroy).and_return(true)
446
- form.tasks.first.task.should_receive(:save).and_return(true)
447
- form.tasks.last.task.should_receive(:save).and_return(true)
448
- form.save
449
- end
450
-
451
- describe "value assignment and persistence" do
452
- before(:each) { form._destroy = true }
453
- before(:each) { form.save }
454
-
455
- it "destroys company" do
456
- expect { company.reload }.to raise_error
457
- end
458
-
459
- it "destroys project" do
460
- expect { project.reload }.to raise_error
461
- end
462
-
463
- it "assigns task_1 values" do
464
- task_1.reload
465
- task_1.name.should eq("new_task_1")
466
- task_1.start_date.should eq(Date.new(2012, 1, 2))
467
- task_1.end_date.should eq(Date.new(2014, 3, 3))
468
- end
469
-
470
- it "assigns task_1 values" do
471
- task_2.reload
472
- task_2.name.should eq("new_task_2")
473
- task_2.start_date.should eq(Date.new(2011, 8, 15))
474
- task_2.end_date.should eq(Date.new(2011, 12, 15))
475
- end
476
- end
477
- end
478
-
479
- describe "with nested destroy" do
480
- before(:each) { form.tasks.first._destroy = true }
481
-
482
- it "destroys nested models on save if set" do
483
- form.company.should_receive(:save).and_return(true)
484
- form.project.should_receive(:save).and_return(true)
485
- form.tasks.first.task.should_receive(:destroy).and_return(true)
486
- form.tasks.last.task.should_receive(:save).and_return(true)
487
- form.save
488
- end
489
-
490
- describe "value assignment and persistence" do
491
- before(:each) { form.save }
492
-
493
- it "assign company values" do
494
- company.reload
495
- company.name.should eq("dummycorp")
496
- end
497
-
498
- it "assigns project values" do
499
- project.reload
500
- project.name.should eq("railsapp")
501
- project.due_date.should eq(Date.new(2014, 10, 30))
502
- end
503
-
504
- it "destroys task_1" do
505
- expect { task_1.reload }.to raise_error
506
- end
507
-
508
- it "assigns task_1 values" do
509
- task_2.reload
510
- task_2.name.should eq("new_task_2")
511
- task_2.start_date.should eq(Date.new(2011, 8, 15))
512
- task_2.end_date.should eq(Date.new(2011, 12, 15))
513
- end
514
- end
515
- end
516
- end
517
-
518
- describe "save!", :save! => true do
519
- describe "without destroy" do
520
- it "should return true on 'save!', and call save! on other models" do
521
- form.company.should_receive(:save!).and_return(true)
522
- form.project.should_receive(:save!).and_return(true)
523
- form.tasks.first.task.should_receive(:save!).and_return(true)
524
- form.tasks.last.task.should_receive(:save!).and_return(true)
525
- form.save!
526
- end
527
-
528
- describe "value assignment and persistence" do
529
- before(:each) { form.save! }
530
-
531
- it "assign company values" do
532
- company.reload
533
- company.name.should eq("dummycorp")
534
- end
535
-
536
- it "assigns project values" do
537
- project.reload
538
- project.name.should eq("railsapp")
539
- project.due_date.should eq(Date.new(2014, 10, 30))
540
- end
541
-
542
- it "assigns task_1 values" do
543
- task_1.reload
544
- task_1.name.should eq("new_task_1")
545
- task_1.start_date.should eq(Date.new(2012, 1, 2))
546
- task_1.end_date.should eq(Date.new(2014, 3, 3))
547
- end
548
-
549
- it "assigns task_1 values" do
550
- task_2.reload
551
- task_2.name.should eq("new_task_2")
552
- task_2.start_date.should eq(Date.new(2011, 8, 15))
553
- task_2.end_date.should eq(Date.new(2011, 12, 15))
554
- end
555
- end
556
- end
557
-
558
- describe "with destroy" do
559
- it "destroys models on save! if set" do
560
- form._destroy = true
561
- form.company.should_receive(:destroy).and_return(true)
562
- form.project.should_receive(:destroy).and_return(true)
563
- form.tasks.first.task.should_receive(:save!).and_return(true)
564
- form.tasks.last.task.should_receive(:save!).and_return(true)
565
- form.save!
566
- end
567
-
568
- it "destroys models on save! if set" do
569
- form.fill({:_destroy => "1"})
570
- form.company.should_receive(:destroy).and_return(true)
571
- form.project.should_receive(:destroy).and_return(true)
572
- form.tasks.first.task.should_receive(:save!).and_return(true)
573
- form.tasks.last.task.should_receive(:save!).and_return(true)
574
- form.save!
575
- end
576
-
577
- describe "value assignment and persistence" do
578
- before(:each) { form._destroy = true }
579
- before(:each) { form.save! }
580
-
581
- it "destroys company" do
582
- expect { company.reload }.to raise_error
583
- end
584
-
585
- it "destroys project" do
586
- expect { project.reload }.to raise_error
587
- end
588
-
589
- it "assigns task_1 values" do
590
- task_1.reload
591
- task_1.name.should eq("new_task_1")
592
- task_1.start_date.should eq(Date.new(2012, 1, 2))
593
- task_1.end_date.should eq(Date.new(2014, 3, 3))
594
- end
595
-
596
- it "assigns task_1 values" do
597
- task_2.reload
598
- task_2.name.should eq("new_task_2")
599
- task_2.start_date.should eq(Date.new(2011, 8, 15))
600
- task_2.end_date.should eq(Date.new(2011, 12, 15))
601
- end
602
- end
603
- end
604
-
605
- describe "with nested destroy" do
606
- it "destroys nested models on save! if set" do
607
- form.tasks.last._destroy = true
608
- form.company.should_receive(:save!).and_return(true)
609
- form.project.should_receive(:save!).and_return(true)
610
- form.tasks.first.task.should_receive(:save!).and_return(true)
611
- form.tasks.last.task.should_receive(:destroy).and_return(true)
612
- form.save!
613
- end
614
-
615
- describe "value assignment and persistence" do
616
- before(:each) { form.tasks.first._destroy = true }
617
- before(:each) { form.save! }
618
-
619
- it "assign company values" do
620
- company.reload
621
- company.name.should eq("dummycorp")
622
- end
623
-
624
- it "assigns project values" do
625
- project.reload
626
- project.name.should eq("railsapp")
627
- project.due_date.should eq(Date.new(2014, 10, 30))
628
- end
629
-
630
- it "destroys task_1" do
631
- expect { task_1.reload }.to raise_error
632
- end
633
-
634
- it "assigns task_1 values" do
635
- task_2.reload
636
- task_2.name.should eq("new_task_2")
637
- task_2.start_date.should eq(Date.new(2011, 8, 15))
638
- task_2.end_date.should eq(Date.new(2011, 12, 15))
639
- end
640
- end
641
- end
642
- end
643
- end
644
- end
645
- end