client_side_validations 3.1.0 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -78,33 +78,33 @@ module ClientSideValidations::ActionView::Helpers
78
78
  private
79
79
 
80
80
  def apply_client_side_validators(method, options = {})
81
- if @options[:validate] && options[:validate] != false && validators = filter_validators(@object.client_side_validation_hash[method], options[:validate])
81
+ if @options[:validate] && options[:validate] != false && validators = filter_validators(method, options[:validate])
82
82
  options.merge!("data-validate" => true)
83
83
  @options[:validators].merge!("#{@object_name}[#{method}]#{options[:multiple] ? "[]" : nil}" => validators)
84
84
  end
85
85
  end
86
86
 
87
- def filter_validators(validators, filters)
88
- if validators
89
- filtered_validators = validators.inject({}) do |filtered_validators, validator|
90
- filtered_validators[validator.first] = validator.last
87
+ def filter_validators(method, filters)
88
+ if validators = @object.client_side_validation_hash[method]
89
+ unfiltered_validators = validators.inject({}) do |unfiltered_validators, validator|
90
+ unfiltered_validators[validator.first] = validator.last
91
91
  if has_filter_for_validator?(validator, filters)
92
92
  if filter_validator?(validator, filters)
93
- filtered_validators.delete(validator.first)
94
- elsif force_validator_despite_conditional?(validator, filters) && !can_run_validator?(validator)
95
- filtered_validators.delete(validator.first)
93
+ unfiltered_validators.delete(validator.first)
94
+ elsif force_validator_despite_conditional?(validator, filters) && !can_run_validator?(validator, method)
95
+ unfiltered_validators.delete(validator.first)
96
96
  end
97
97
  else
98
- if validator.last.key?(:if) || validator.last.key?(:unless)
99
- filtered_validators.delete(validator.first)
98
+ if (conditional = (validator.last[:if] || validator.last[:unless])) && conditional.is_a?(Symbol) && !conditional_method_is_change_method?(conditional, method)
99
+ unfiltered_validators.delete(validator.first)
100
100
  end
101
101
  end
102
- filtered_validators[validator.first].delete(:if) if filtered_validators[validator.first]
103
- filtered_validators[validator.first].delete(:unless) if filtered_validators[validator.first]
104
- filtered_validators
102
+ unfiltered_validators[validator.first].delete(:if) if unfiltered_validators[validator.first]
103
+ unfiltered_validators[validator.first].delete(:unless) if unfiltered_validators[validator.first]
104
+ unfiltered_validators
105
105
  end
106
106
 
107
- filtered_validators.empty? ? nil : filtered_validators
107
+ unfiltered_validators.empty? ? nil : unfiltered_validators
108
108
  end
109
109
  end
110
110
 
@@ -120,33 +120,38 @@ module ClientSideValidations::ActionView::Helpers
120
120
  filters == true || filters[validator.first] == true
121
121
  end
122
122
 
123
- def can_run_validator?(validator)
123
+ def can_run_validator?(validator, method)
124
124
  result = true
125
- if_result = can_run_if_validator?(validator.last[:if])
126
- unless_result = can_run_unless_validator?(validator.last[:unless])
125
+ if_result = run_if_validator(validator.last[:if], method)
126
+ unless_result = run_unless_validator(validator.last[:unless], method)
127
127
  result = result && if_result unless if_result.nil?
128
128
  result = result && unless_result unless unless_result.nil?
129
129
  result
130
130
  end
131
131
 
132
- def can_run_if_validator?(conditional)
132
+ def run_if_validator(conditional, method)
133
133
  if conditional
134
134
  if conditional.is_a?(Symbol)
135
- !!@object.send(conditional)
135
+ conditional_method_is_change_method?(conditional, method) ? true : !!@object.send(conditional)
136
136
  else
137
137
  !!conditional.call(@object)
138
138
  end
139
139
  end
140
140
  end
141
141
 
142
- def can_run_unless_validator?(conditional)
142
+ def run_unless_validator(conditional, method)
143
143
  if conditional
144
144
  if conditional.is_a?(Symbol)
145
- !@object.send(conditional)
145
+ conditional_method_is_change_method?(conditional, method) ? true : !@object.send(conditional)
146
146
  else
147
147
  !conditional.call(@object)
148
148
  end
149
149
  end
150
150
  end
151
+
152
+ def conditional_method_is_change_method?(conditional, method)
153
+ conditional.to_sym == "#{method}_changed?".to_sym
154
+ end
151
155
  end
152
156
  end
157
+
@@ -1,3 +1,3 @@
1
1
  module ClientSideValidations
2
- VERSION = '3.1.0'
2
+ VERSION = '3.1.1'
3
3
  end
@@ -346,7 +346,7 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
346
346
  assert_equal expected, output_buffer
347
347
  end
348
348
 
349
- def test_conditional_validator_filters
349
+ def test_conditional_validators_should_be_filtered
350
350
  hash = {
351
351
  :cost => {
352
352
  :presence => {
@@ -379,7 +379,7 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
379
379
  assert_equal expected, output_buffer
380
380
  end
381
381
 
382
- def test_conditional_validators_should_be_filtered
382
+ def test_conditional_validator_filters_being_forced
383
383
  hash = {
384
384
  :cost => {
385
385
  :presence => {
@@ -400,8 +400,44 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
400
400
  @post.stubs(:can_validate?).returns(true)
401
401
  @post.stubs(:client_side_validation_hash).returns(hash)
402
402
  form_for(@post, :validate => true) do |f|
403
- concat f.text_field(:cost)
404
- concat f.text_field(:title)
403
+ concat f.text_field(:cost, :validate => true)
404
+ concat f.text_field(:title, :validate => true)
405
+ end
406
+
407
+ validators = {
408
+ 'post[cost]' => {:presence => {:message => "can't be blank"}},
409
+ 'post[title]' => {:presence => {:message => "can't be blank"}}
410
+ }
411
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
412
+ %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />} +
413
+ %{<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" />}
414
+ end
415
+ assert_equal expected, output_buffer
416
+ end
417
+
418
+ def test_conditional_validator_filters_being_forced_and_not_meeting_condition
419
+ hash = {
420
+ :cost => {
421
+ :presence => {
422
+ :message => "can't be blank",
423
+ :unless => :cannot_validate?
424
+ }
425
+ },
426
+ :title => {
427
+ :presence => {
428
+ :message => "can't be blank",
429
+ :if => :can_validate?
430
+ }
431
+ }
432
+ }
433
+
434
+ @post.title = nil
435
+ @post.stubs(:cannot_validate?).returns(true)
436
+ @post.stubs(:can_validate?).returns(false)
437
+ @post.stubs(:client_side_validation_hash).returns(hash)
438
+ form_for(@post, :validate => true) do |f|
439
+ concat f.text_field(:cost, :validate => true)
440
+ concat f.text_field(:title, :validate => true)
405
441
  end
406
442
 
407
443
  validators = {}
@@ -412,7 +448,7 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
412
448
  assert_equal expected, output_buffer
413
449
  end
414
450
 
415
- def test_conditional_validator_filters_being_forced
451
+ def test_conditional_validator_filters_being_forced_individually
416
452
  hash = {
417
453
  :cost => {
418
454
  :presence => {
@@ -433,8 +469,8 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
433
469
  @post.stubs(:can_validate?).returns(true)
434
470
  @post.stubs(:client_side_validation_hash).returns(hash)
435
471
  form_for(@post, :validate => true) do |f|
436
- concat f.text_field(:cost, :validate => true)
437
- concat f.text_field(:title, :validate => true)
472
+ concat f.text_field(:cost, :validate => { :presence => true })
473
+ concat f.text_field(:title, :validate => { :presence => true })
438
474
  end
439
475
 
440
476
  validators = {
@@ -448,7 +484,7 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
448
484
  assert_equal expected, output_buffer
449
485
  end
450
486
 
451
- def test_conditional_validator_filters_being_forced_and_not_meeting_condition
487
+ def test_conditional_validator_filters_being_forced_and_not_meeting_condition_individually
452
488
  hash = {
453
489
  :cost => {
454
490
  :presence => {
@@ -469,8 +505,8 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
469
505
  @post.stubs(:can_validate?).returns(false)
470
506
  @post.stubs(:client_side_validation_hash).returns(hash)
471
507
  form_for(@post, :validate => true) do |f|
472
- concat f.text_field(:cost, :validate => true)
473
- concat f.text_field(:title, :validate => true)
508
+ concat f.text_field(:cost, :validate => { :presence => true })
509
+ concat f.text_field(:title, :validate => { :presence => true })
474
510
  end
475
511
 
476
512
  validators = {}
@@ -481,18 +517,18 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
481
517
  assert_equal expected, output_buffer
482
518
  end
483
519
 
484
- def test_conditional_validator_filters_being_forced_individually
520
+ def test_conditional_validator_filters_being_forced_with_procs
485
521
  hash = {
486
522
  :cost => {
487
523
  :presence => {
488
524
  :message => "can't be blank",
489
- :unless => :cannot_validate?
525
+ :unless => Proc.new { |post| post.cannot_validate? }
490
526
  }
491
527
  },
492
528
  :title => {
493
529
  :presence => {
494
530
  :message => "can't be blank",
495
- :if => :can_validate?
531
+ :if => Proc.new { |post| post.can_validate? }
496
532
  }
497
533
  }
498
534
  }
@@ -502,8 +538,8 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
502
538
  @post.stubs(:can_validate?).returns(true)
503
539
  @post.stubs(:client_side_validation_hash).returns(hash)
504
540
  form_for(@post, :validate => true) do |f|
505
- concat f.text_field(:cost, :validate => { :presence => true })
506
- concat f.text_field(:title, :validate => { :presence => true })
541
+ concat f.text_field(:cost, :validate => true)
542
+ concat f.text_field(:title, :validate => true)
507
543
  end
508
544
 
509
545
  validators = {
@@ -517,58 +553,57 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
517
553
  assert_equal expected, output_buffer
518
554
  end
519
555
 
520
- def test_conditional_validator_filters_being_forced_and_not_meeting_condition_individually
556
+ def test_conditional_validator_ignored_when_using_changed_helpers
521
557
  hash = {
522
558
  :cost => {
523
559
  :presence => {
524
560
  :message => "can't be blank",
525
- :unless => :cannot_validate?
561
+ :unless => :cost_changed?
526
562
  }
527
563
  },
528
564
  :title => {
529
565
  :presence => {
530
566
  :message => "can't be blank",
531
- :if => :can_validate?
567
+ :if => :title_changed?
532
568
  }
533
569
  }
534
570
  }
535
571
 
536
572
  @post.title = nil
537
- @post.stubs(:cannot_validate?).returns(true)
538
- @post.stubs(:can_validate?).returns(false)
539
573
  @post.stubs(:client_side_validation_hash).returns(hash)
540
574
  form_for(@post, :validate => true) do |f|
541
- concat f.text_field(:cost, :validate => { :presence => true })
542
- concat f.text_field(:title, :validate => { :presence => true })
575
+ concat f.text_field(:cost)
576
+ concat f.text_field(:title)
543
577
  end
544
578
 
545
- validators = {}
579
+ validators = {
580
+ 'post[cost]' => {:presence => {:message => "can't be blank"}},
581
+ 'post[title]' => {:presence => {:message => "can't be blank"}}
582
+ }
546
583
  expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
547
- %{<input id="post_cost" name="post[cost]" size="30" type="text" />} +
548
- %{<input id="post_title" name="post[title]" size="30" type="text" />}
584
+ %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />} +
585
+ %{<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" />}
549
586
  end
550
587
  assert_equal expected, output_buffer
551
588
  end
552
589
 
553
- def test_conditional_validator_filters_being_forced_with_procs
590
+ def test_conditional_validator_ignored_when_using_changed_helpers_and_forcing_validators
554
591
  hash = {
555
592
  :cost => {
556
593
  :presence => {
557
594
  :message => "can't be blank",
558
- :unless => Proc.new { |post| post.cannot_validate? }
595
+ :unless => :cost_changed?
559
596
  }
560
597
  },
561
598
  :title => {
562
599
  :presence => {
563
600
  :message => "can't be blank",
564
- :if => Proc.new { |post| post.can_validate? }
601
+ :if => :title_changed?
565
602
  }
566
603
  }
567
604
  }
568
605
 
569
606
  @post.title = nil
570
- @post.stubs(:cannot_validate?).returns(false)
571
- @post.stubs(:can_validate?).returns(true)
572
607
  @post.stubs(:client_side_validation_hash).returns(hash)
573
608
  form_for(@post, :validate => true) do |f|
574
609
  concat f.text_field(:cost, :validate => true)
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Rails 3 Client Side Validations - v3.1.0
2
+ * Rails 3 Client Side Validations - v3.1.1
3
3
  * https://github.com/bcardarlela/client_side_validations
4
4
  *
5
5
  * Copyright (c) 2011 Brian Cardarella
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: client_side_validations
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 3.1.0
5
+ version: 3.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Brian Cardarella
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-20 00:00:00 -04:00
13
+ date: 2011-10-25 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -304,7 +304,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
304
304
  requirements: []
305
305
 
306
306
  rubyforge_project:
307
- rubygems_version: 1.5.3
307
+ rubygems_version: 1.3.9.3
308
308
  signing_key:
309
309
  specification_version: 3
310
310
  summary: Client Side Validations