client_side_validations-rails_2 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +4 -0
  3. data/HISTORY +5 -0
  4. data/README.markdown +45 -0
  5. data/Rakefile +21 -0
  6. data/client_side_validations-rails_2.gemspec +35 -0
  7. data/generators/client_side_validations/client_side_validations_generator.rb +14 -0
  8. data/lib/client_side_validations/rails_2.rb +5 -0
  9. data/lib/client_side_validations/rails_2/action_view.rb +7 -0
  10. data/lib/client_side_validations/rails_2/action_view/form_helper.rb +11 -0
  11. data/lib/client_side_validations/rails_2/action_view/form_tag_helper.rb +6 -0
  12. data/lib/client_side_validations/rails_2/active_record.rb +14 -0
  13. data/lib/client_side_validations/rails_2/active_record/active_model.rb +12 -0
  14. data/lib/client_side_validations/rails_2/active_record/active_model/validations.rb +51 -0
  15. data/lib/client_side_validations/rails_2/active_record/active_model/validations/base.rb +25 -0
  16. data/lib/client_side_validations/rails_2/active_record/active_model/validations/numericality.rb +5 -0
  17. data/lib/client_side_validations/rails_2/active_record/middleware.rb +42 -0
  18. data/lib/client_side_validations/rails_2/active_record/validations.rb +23 -0
  19. data/lib/client_side_validations/rails_2/middleware.rb +22 -0
  20. data/lib/client_side_validations/rails_2/version.rb +5 -0
  21. data/test/action_view/cases/helper.rb +155 -0
  22. data/test/action_view/cases/test_helpers.rb +516 -0
  23. data/test/action_view/cases/test_legacy_helpers.rb +140 -0
  24. data/test/action_view/models.rb +2 -0
  25. data/test/action_view/models/comment.rb +26 -0
  26. data/test/action_view/models/post.rb +31 -0
  27. data/test/active_record/cases/helper.rb +13 -0
  28. data/test/active_record/cases/test_base.rb +10 -0
  29. data/test/active_record/cases/test_middleware.rb +157 -0
  30. data/test/active_record/cases/test_validation_reflection.rb +144 -0
  31. data/test/active_record/cases/test_validations.rb +27 -0
  32. data/test/active_record/models/guid.rb +7 -0
  33. data/test/active_record/models/user.rb +11 -0
  34. data/test/base_helper.rb +10 -0
  35. data/test/middleware/cases/helper.rb +6 -0
  36. metadata +237 -0
@@ -0,0 +1,516 @@
1
+ require 'action_view/cases/helper'
2
+
3
+ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
4
+ include ActionViewTestSetup
5
+
6
+ cattr_accessor :field_error_proc
7
+ @@field_error_proc = Proc.new { |html_tag, instance| html_tag }
8
+
9
+ def client_side_form_settings_helper
10
+ {
11
+ :type => "ActionView::Helpers::FormBuilder",
12
+ :input_tag => %{<span id="input_tag" />},
13
+ :label_tag => %{<label id="label_tag" />}
14
+ }
15
+ end
16
+
17
+ def test_text_field
18
+ form_for(@post, :validate => true) do |f|
19
+ concat f.text_field(:cost)
20
+ end
21
+
22
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
23
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
24
+ %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />}
25
+ end
26
+ assert_equal expected, output_buffer
27
+ end
28
+
29
+ def test_password_field
30
+ form_for(@post, :validate => true) do |f|
31
+ concat f.password_field(:cost)
32
+ end
33
+
34
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
35
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
36
+ %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="password" />}
37
+ end
38
+ assert_equal expected, output_buffer
39
+ end
40
+
41
+ def test_file_field
42
+ form_for(@post, :validate => true) do |f|
43
+ concat f.file_field(:cost)
44
+ end
45
+
46
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
47
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
48
+ %{<input data-validate="true" id="post_cost" name="post[cost]" size=\"30\" type="file" />}
49
+ end
50
+ assert_equal expected, output_buffer
51
+ end
52
+
53
+ def test_text_area
54
+ form_for(@post, :validate => true) do |f|
55
+ concat f.text_area(:cost)
56
+ end
57
+
58
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
59
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
60
+ %{<textarea cols="40" data-validate="true" id="post_cost" name="post[cost]" rows="20"></textarea>}
61
+ end
62
+ assert_equal expected, output_buffer
63
+ end
64
+
65
+ def test_check_box
66
+ form_for(@post, :validate => true) do |f|
67
+ concat f.check_box(:cost)
68
+ end
69
+
70
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
71
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
72
+ %{<input name="post[cost]" type="hidden" value="0" />} +
73
+ %{<input data-validate="true" id="post_cost" name="post[cost]" type="checkbox" value="1" />}
74
+ end
75
+ assert_equal expected, output_buffer
76
+ end
77
+
78
+ def test_radio_button
79
+ form_for(@post, :validate => true) do |f|
80
+ concat f.radio_button(:cost, "10")
81
+ end
82
+
83
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
84
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
85
+ %{<input data-validate="true" id="post_cost_10" name="post[cost]" type="radio" value="10" />}
86
+ end
87
+ assert_equal expected, output_buffer
88
+ end
89
+
90
+ def test_field_without_validations
91
+ form_for(@post, :validate => true) do |f|
92
+ concat f.text_field(:title)
93
+ end
94
+
95
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => {}) do
96
+ %{<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />}
97
+ end
98
+ assert_equal expected, output_buffer
99
+ end
100
+
101
+ def test_text_field_with_validations_turned_off
102
+ form_for(@post, :validate => true) do |f|
103
+ concat f.text_field(:cost, :validate => false)
104
+ end
105
+
106
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => {}) do
107
+ %{<input id="post_cost" name="post[cost]" size="30" type="text" />}
108
+ end
109
+ assert_equal expected, output_buffer
110
+ end
111
+
112
+ def test_nested_fields_for_inherit_validation_settings
113
+ form_for(@post, :validate => true) do |f|
114
+ f.fields_for(:comment, @comment) { |c|
115
+ concat c.text_field(:title)
116
+ }
117
+ end
118
+
119
+ validators = {'post[comment][title]' => {:presence => {:message => "can't be blank"}}}
120
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
121
+ %{<input data-validate="true" id="post_comment_title" name="post[comment][title]" size="30" type="text" />}
122
+ end
123
+
124
+ assert_equal expected, output_buffer
125
+ end
126
+
127
+ def test_nested_fields_for_dont_overwrite_validation_with_inheritance
128
+ form_for(@post, :validate => true) do |f|
129
+ f.fields_for(:comment, @comment, :validate => false) { |c|
130
+ concat c.text_field(:title)
131
+ }
132
+ end
133
+
134
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => {}) do
135
+ %{<input id="post_comment_title" name="post[comment][title]" size="30" type="text" />}
136
+ end
137
+
138
+ assert_equal expected, output_buffer
139
+ end
140
+
141
+ def test_with_custom_id_for_form
142
+ form_for(@post, :validate => true, :html => { :id => 'some_form' }) do |f|
143
+ concat f.text_field(:cost)
144
+ end
145
+
146
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
147
+ expected = whole_form("/posts/123", "some_form", "edit_post", :method => "put", :validators => validators) do
148
+ %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />}
149
+ end
150
+ assert_equal expected, output_buffer
151
+ end
152
+
153
+ def test_ignore_an_individual_validator
154
+ hash = {
155
+ :cost => {
156
+ :presence => {
157
+ :message => "can't be blank"
158
+ },
159
+ :format => {
160
+ :with => /.+/,
161
+ :message => "is invalid"
162
+ }
163
+ }
164
+ }
165
+ @post.stubs(:client_side_validation_hash).returns(hash)
166
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
167
+ form_for(@post, :validate => true) do |f|
168
+ concat f.text_field(:cost, :validate => { :format => false })
169
+ end
170
+
171
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
172
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
173
+ %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />}
174
+ end
175
+ assert_equal expected, output_buffer
176
+ end
177
+
178
+ def test_ignore_many_validators
179
+ hash = {
180
+ :cost => {
181
+ :presence => {
182
+ :message => "can't be blank"
183
+ },
184
+ :format => {
185
+ :with => /.+/,
186
+ :message => "is invalid"
187
+ }
188
+ }
189
+ }
190
+ @post.stubs(:client_side_validation_hash).returns(hash)
191
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
192
+ form_for(@post, :validate => true) do |f|
193
+ concat f.text_field(:cost, :validate => { :presence => false, :format => false })
194
+ end
195
+
196
+ validators = {}
197
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
198
+ %{<input id="post_cost" name="post[cost]" size="30" type="text" />}
199
+ end
200
+ assert_equal expected, output_buffer
201
+ end
202
+
203
+ def test_select
204
+ form_for(@post, :validate => true) do |f|
205
+ concat f.select(:cost, [])
206
+ end
207
+
208
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
209
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
210
+ %{<select data-validate="true" id="post_cost" name="post[cost]"></select>}
211
+ end
212
+ assert_equal expected, output_buffer
213
+ end
214
+
215
+ def test_select_multiple
216
+ form_for(@post, :validate => true) do |f|
217
+ concat f.select(:cost, [], {}, :multiple => true)
218
+ end
219
+
220
+ validators = {'post[cost][]' => {:presence => {:message => "can't be blank"}}}
221
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
222
+ %{<select data-validate="true" id="post_cost" multiple="multiple" name="post[cost][]"></select>}
223
+ end
224
+ assert_equal expected, output_buffer
225
+ end
226
+
227
+ def test_collection_select
228
+ form_for(@post, :validate => true) do |f|
229
+ concat f.collection_select(:cost, [], :id, :name)
230
+ end
231
+
232
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
233
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
234
+ %{<select data-validate="true" id="post_cost" name="post[cost]"></select>}
235
+ end
236
+ assert_equal expected, output_buffer
237
+ end
238
+
239
+ def test_grouped_collection_select
240
+ form_for(@post, :validate => true) do |f|
241
+ concat f.grouped_collection_select(:cost, [], :group_method, :group_label_method, :id, :name)
242
+ end
243
+
244
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
245
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
246
+ %{<select data-validate="true" id="post_cost" name="post[cost]"></select>}
247
+ end
248
+ assert_equal expected, output_buffer
249
+ end
250
+
251
+ def test_time_zone_select
252
+ zones = mock('TimeZones')
253
+ zones.stubs(:all).returns([])
254
+ form_for(@post, :validate => true) do |f|
255
+ concat f.time_zone_select(:cost, nil, :model => zones)
256
+ end
257
+
258
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
259
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
260
+ %{<select data-validate="true" id="post_cost" name="post[cost]"></select>}
261
+ end
262
+ assert_equal expected, output_buffer
263
+ end
264
+
265
+ def test_conditional_validator_filters
266
+ hash = {
267
+ :cost => {
268
+ :presence => {
269
+ :message => "can't be blank",
270
+ :unless => :cannot_validate?
271
+ }
272
+ },
273
+ :title => {
274
+ :presence => {
275
+ :message => "can't be blank",
276
+ :if => :can_validate?
277
+ }
278
+ }
279
+ }
280
+
281
+ @post.title = nil
282
+ @post.stubs(:cannot_validate?).returns(false)
283
+ @post.stubs(:can_validate?).returns(true)
284
+ @post.stubs(:client_side_validation_hash).returns(hash)
285
+ form_for(@post, :validate => true) do |f|
286
+ concat f.text_field(:cost)
287
+ concat f.text_field(:title)
288
+ end
289
+
290
+ validators = {}
291
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
292
+ %{<input id="post_cost" name="post[cost]" size="30" type="text" />} +
293
+ %{<input id="post_title" name="post[title]" size="30" type="text" />}
294
+ end
295
+ assert_equal expected, output_buffer
296
+ end
297
+
298
+ def test_conditional_validators_should_be_filtered
299
+ hash = {
300
+ :cost => {
301
+ :presence => {
302
+ :message => "can't be blank",
303
+ :unless => :cannot_validate?
304
+ }
305
+ },
306
+ :title => {
307
+ :presence => {
308
+ :message => "can't be blank",
309
+ :if => :can_validate?
310
+ }
311
+ }
312
+ }
313
+
314
+ @post.title = nil
315
+ @post.stubs(:cannot_validate?).returns(false)
316
+ @post.stubs(:can_validate?).returns(true)
317
+ @post.stubs(:client_side_validation_hash).returns(hash)
318
+ form_for(@post, :validate => true) do |f|
319
+ concat f.text_field(:cost)
320
+ concat f.text_field(:title)
321
+ end
322
+
323
+ validators = {}
324
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
325
+ %{<input id="post_cost" name="post[cost]" size="30" type="text" />} +
326
+ %{<input id="post_title" name="post[title]" size="30" type="text" />}
327
+ end
328
+ assert_equal expected, output_buffer
329
+ end
330
+
331
+ def test_conditional_validator_filters_being_forced
332
+ hash = {
333
+ :cost => {
334
+ :presence => {
335
+ :message => "can't be blank",
336
+ :unless => :cannot_validate?
337
+ }
338
+ },
339
+ :title => {
340
+ :presence => {
341
+ :message => "can't be blank",
342
+ :if => :can_validate?
343
+ }
344
+ }
345
+ }
346
+
347
+ @post.title = nil
348
+ @post.stubs(:cannot_validate?).returns(false)
349
+ @post.stubs(:can_validate?).returns(true)
350
+ @post.stubs(:client_side_validation_hash).returns(hash)
351
+ form_for(@post, :validate => true) do |f|
352
+ concat f.text_field(:cost, :validate => true)
353
+ concat f.text_field(:title, :validate => true)
354
+ end
355
+
356
+ validators = {
357
+ 'post[cost]' => {:presence => {:message => "can't be blank"}},
358
+ 'post[title]' => {:presence => {:message => "can't be blank"}}
359
+ }
360
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
361
+ %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />} +
362
+ %{<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" />}
363
+ end
364
+ assert_equal expected, output_buffer
365
+ end
366
+
367
+ def test_conditional_validator_filters_being_forced_and_not_meeting_condition
368
+ hash = {
369
+ :cost => {
370
+ :presence => {
371
+ :message => "can't be blank",
372
+ :unless => :cannot_validate?
373
+ }
374
+ },
375
+ :title => {
376
+ :presence => {
377
+ :message => "can't be blank",
378
+ :if => :can_validate?
379
+ }
380
+ }
381
+ }
382
+
383
+ @post.title = nil
384
+ @post.stubs(:cannot_validate?).returns(true)
385
+ @post.stubs(:can_validate?).returns(false)
386
+ @post.stubs(:client_side_validation_hash).returns(hash)
387
+ form_for(@post, :validate => true) do |f|
388
+ concat f.text_field(:cost, :validate => true)
389
+ concat f.text_field(:title, :validate => true)
390
+ end
391
+
392
+ validators = {}
393
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
394
+ %{<input id="post_cost" name="post[cost]" size="30" type="text" />} +
395
+ %{<input id="post_title" name="post[title]" size="30" type="text" />}
396
+ end
397
+ assert_equal expected, output_buffer
398
+ end
399
+
400
+ def test_conditional_validator_filters_being_forced_individually
401
+ hash = {
402
+ :cost => {
403
+ :presence => {
404
+ :message => "can't be blank",
405
+ :unless => :cannot_validate?
406
+ }
407
+ },
408
+ :title => {
409
+ :presence => {
410
+ :message => "can't be blank",
411
+ :if => :can_validate?
412
+ }
413
+ }
414
+ }
415
+
416
+ @post.title = nil
417
+ @post.stubs(:cannot_validate?).returns(false)
418
+ @post.stubs(:can_validate?).returns(true)
419
+ @post.stubs(:client_side_validation_hash).returns(hash)
420
+ form_for(@post, :validate => true) do |f|
421
+ concat f.text_field(:cost, :validate => { :presence => true })
422
+ concat f.text_field(:title, :validate => { :presence => true })
423
+ end
424
+
425
+ validators = {
426
+ 'post[cost]' => {:presence => {:message => "can't be blank"}},
427
+ 'post[title]' => {:presence => {:message => "can't be blank"}}
428
+ }
429
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
430
+ %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />} +
431
+ %{<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" />}
432
+ end
433
+ assert_equal expected, output_buffer
434
+ end
435
+
436
+ def test_conditional_validator_filters_being_forced_and_not_meeting_condition_individually
437
+ hash = {
438
+ :cost => {
439
+ :presence => {
440
+ :message => "can't be blank",
441
+ :unless => :cannot_validate?
442
+ }
443
+ },
444
+ :title => {
445
+ :presence => {
446
+ :message => "can't be blank",
447
+ :if => :can_validate?
448
+ }
449
+ }
450
+ }
451
+
452
+ @post.title = nil
453
+ @post.stubs(:cannot_validate?).returns(true)
454
+ @post.stubs(:can_validate?).returns(false)
455
+ @post.stubs(:client_side_validation_hash).returns(hash)
456
+ form_for(@post, :validate => true) do |f|
457
+ concat f.text_field(:cost, :validate => { :presence => true })
458
+ concat f.text_field(:title, :validate => { :presence => true })
459
+ end
460
+
461
+ validators = {}
462
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
463
+ %{<input id="post_cost" name="post[cost]" size="30" type="text" />} +
464
+ %{<input id="post_title" name="post[title]" size="30" type="text" />}
465
+ end
466
+ assert_equal expected, output_buffer
467
+ end
468
+
469
+ def test_conditional_validator_filters_being_forced_with_procs
470
+ hash = {
471
+ :cost => {
472
+ :presence => {
473
+ :message => "can't be blank",
474
+ :unless => Proc.new { |post| post.cannot_validate? }
475
+ }
476
+ },
477
+ :title => {
478
+ :presence => {
479
+ :message => "can't be blank",
480
+ :if => Proc.new { |post| post.can_validate? }
481
+ }
482
+ }
483
+ }
484
+
485
+ @post.title = nil
486
+ @post.stubs(:cannot_validate?).returns(false)
487
+ @post.stubs(:can_validate?).returns(true)
488
+ @post.stubs(:client_side_validation_hash).returns(hash)
489
+ form_for(@post, :validate => true) do |f|
490
+ concat f.text_field(:cost, :validate => true)
491
+ concat f.text_field(:title, :validate => true)
492
+ end
493
+
494
+ validators = {
495
+ 'post[cost]' => {:presence => {:message => "can't be blank"}},
496
+ 'post[title]' => {:presence => {:message => "can't be blank"}}
497
+ }
498
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
499
+ %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />} +
500
+ %{<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" />}
501
+ end
502
+ assert_equal expected, output_buffer
503
+ end
504
+
505
+ def test_pushing_script_to_content_for
506
+ form_for(@post, :validate => :post) do |f|
507
+ concat f.text_field(:cost)
508
+ end
509
+
510
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
511
+ expected = %{<form action="/posts/123" class="edit_post" data-validate="true" id="edit_post_123" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="_method" type="hidden" value="put" /></div><input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" /></form>}
512
+ assert_equal expected, output_buffer
513
+ assert_equal build_script_tag(nil, "edit_post_123", validators), @content_for_post
514
+ end
515
+ end
516
+