client_side_validations 3.0.14 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/client_side_validations.gemspec +11 -3
  2. data/lib/client_side_validations.rb +4 -4
  3. data/lib/client_side_validations/action_view.rb +4 -3
  4. data/lib/client_side_validations/action_view/form_builder.rb +21 -26
  5. data/lib/client_side_validations/active_model.rb +1 -2
  6. data/lib/client_side_validations/active_record/middleware.rb +2 -10
  7. data/lib/client_side_validations/formtastic.rb +3 -3
  8. data/lib/client_side_validations/middleware.rb +4 -3
  9. data/lib/client_side_validations/version.rb +1 -1
  10. data/lib/generators/client_side_validations/copy_asset_generator.rb +23 -0
  11. data/lib/generators/client_side_validations/install_generator.rb +18 -6
  12. data/lib/generators/templates/client_side_validations/README.rails.3.0 +6 -0
  13. data/lib/generators/templates/client_side_validations/README.rails.3.1 +7 -0
  14. data/lib/generators/templates/client_side_validations/initializer.rb +1 -1
  15. data/test/action_view/cases/helper.rb +29 -13
  16. data/test/action_view/cases/test_helpers.rb +31 -66
  17. data/test/action_view/cases/test_legacy_helpers.rb +1 -1
  18. data/test/active_record/cases/test_middleware.rb +19 -34
  19. data/test/active_record/models/user.rb +1 -1
  20. data/test/base_helper.rb +1 -0
  21. data/test/core_ext/cases/test_core_ext.rb +1 -0
  22. data/test/formtastic/cases/helper.rb +5 -0
  23. data/test/formtastic/cases/test_form_builder.rb +3 -3
  24. data/test/formtastic/cases/test_form_helper.rb +3 -4
  25. data/test/generators/cases/test_generators.rb +31 -0
  26. data/test/javascript/public/test/form_builders/validateFormtastic.js +1 -1
  27. data/test/javascript/server.rb +1 -1
  28. data/test/middleware/cases/helper.rb +2 -0
  29. data/test/mongo_mapper/cases/test_middleware.rb +7 -7
  30. data/test/mongoid/cases/test_middleware.rb +7 -7
  31. data/test/simple_form/cases/helper.rb +3 -0
  32. data/test/simple_form/cases/test_form_helper.rb +2 -0
  33. data/test/test_loader.rb +6 -0
  34. data/vendor/assets/javascripts/rails.validations.js +135 -136
  35. metadata +153 -163
  36. checksums.yaml +0 -7
  37. data/lib/generators/templates/client_side_validations/README +0 -7
  38. data/test/generators/cases/test_install_generator.rb +0 -15
@@ -17,18 +17,26 @@ Gem::Specification.new do |s|
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
19
 
20
- s.add_development_dependency 'rails', '~> 3.0.0'
20
+ s.add_development_dependency 'rails', '3.1.0.rc4'
21
21
  s.add_development_dependency 'sqlite3'
22
22
  s.add_development_dependency 'bson_ext'
23
23
  s.add_development_dependency 'mongoid', '~> 2.0.0'
24
24
  s.add_development_dependency 'mongo_mapper','~>0.9.0'
25
25
  s.add_development_dependency 'mocha'
26
- s.add_development_dependency 'simple_form', '~> 1.5.0'
27
- s.add_development_dependency 'formtastic', '~> 1.2.0'
26
+ s.add_development_dependency 'simple_form'
27
+ s.add_development_dependency 'formtastic', '~> 2.0.0.rc3'
28
28
 
29
29
  # For QUnit testing
30
30
  s.add_development_dependency 'sinatra', '~> 1.0'
31
31
  s.add_development_dependency 'shotgun'
32
32
  s.add_development_dependency 'thin'
33
33
  s.add_development_dependency 'json'
34
+
35
+ ruby_minor_version = RUBY_VERSION.split('.')[1].to_i
36
+ if ruby_minor_version == 8
37
+ s.add_development_dependency 'minitest'
38
+ s.add_development_dependency 'ruby-debug'
39
+ elsif ruby_minor_version == 9
40
+ s.add_development_dependency 'ruby-debug19'
41
+ end
34
42
  end
@@ -1,11 +1,11 @@
1
1
  module ClientSideValidations
2
2
  end
3
3
 
4
- require 'client_side_validations/active_model' if defined?(::ActiveModel)
4
+ require 'client_side_validations/active_model' if defined?(::ActiveModel)
5
5
  require 'client_side_validations/active_record' if defined?(::ActiveRecord)
6
- require 'client_side_validations/mongoid' if defined?(::Mongoid)
7
- require 'client_side_validations/mongo_mapper' if defined?(::MongoMapper)
8
- require 'client_side_validations/action_view'
6
+ require 'client_side_validations/mongoid' if defined?(::Mongoid)
7
+ require 'client_side_validations/mongo_mapper' if defined?(::MongoMapper)
8
+ require 'client_side_validations/action_view' if defined?(::ActionView)
9
9
  if defined?(::Rails)
10
10
  require 'client_side_validations/middleware'
11
11
  require 'client_side_validations/engine'
@@ -3,9 +3,10 @@ module ClientSideValidations::ActionView
3
3
  end
4
4
  end
5
5
 
6
- require "client_side_validations/action_view/form_helper"
7
- require "client_side_validations/action_view/form_tag_helper"
8
- require "client_side_validations/action_view/form_builder"
6
+ require 'client_side_validations/core_ext'
7
+ require 'client_side_validations/action_view/form_helper'
8
+ require 'client_side_validations/action_view/form_tag_helper'
9
+ require 'client_side_validations/action_view/form_builder'
9
10
 
10
11
  ActionView::Base.send(:include, ClientSideValidations::ActionView::Helpers::FormHelper)
11
12
  ActionView::Base.send(:include, ClientSideValidations::ActionView::Helpers::FormTagHelper)
@@ -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(method, options[:validate])
81
+ if @options[:validate] && options[:validate] != false && validators = filter_validators(@object.client_side_validation_hash[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(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
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
91
91
  if has_filter_for_validator?(validator, filters)
92
92
  if filter_validator?(validator, filters)
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)
93
+ filtered_validators.delete(validator.first)
94
+ elsif force_validator_despite_conditional?(validator, filters) && !can_run_validator?(validator)
95
+ filtered_validators.delete(validator.first)
96
96
  end
97
97
  else
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)
98
+ if validator.last.key?(:if) || validator.last.key?(:unless)
99
+ filtered_validators.delete(validator.first)
100
100
  end
101
101
  end
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
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
105
105
  end
106
106
 
107
- unfiltered_validators.empty? ? nil : unfiltered_validators
107
+ filtered_validators.empty? ? nil : filtered_validators
108
108
  end
109
109
  end
110
110
 
@@ -120,38 +120,33 @@ module ClientSideValidations::ActionView::Helpers
120
120
  filters == true || filters[validator.first] == true
121
121
  end
122
122
 
123
- def can_run_validator?(validator, method)
123
+ def can_run_validator?(validator)
124
124
  result = true
125
- if_result = run_if_validator(validator.last[:if], method)
126
- unless_result = run_unless_validator(validator.last[:unless], method)
125
+ if_result = can_run_if_validator?(validator.last[:if])
126
+ unless_result = can_run_unless_validator?(validator.last[:unless])
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 run_if_validator(conditional, method)
132
+ def can_run_if_validator?(conditional)
133
133
  if conditional
134
134
  if conditional.is_a?(Symbol)
135
- conditional_method_is_change_method?(conditional, method) ? true : !!@object.send(conditional)
135
+ !!@object.send(conditional)
136
136
  else
137
137
  !!conditional.call(@object)
138
138
  end
139
139
  end
140
140
  end
141
141
 
142
- def run_unless_validator(conditional, method)
142
+ def can_run_unless_validator?(conditional)
143
143
  if conditional
144
144
  if conditional.is_a?(Symbol)
145
- conditional_method_is_change_method?(conditional, method) ? true : !@object.send(conditional)
145
+ !@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
155
151
  end
156
152
  end
157
-
@@ -5,8 +5,7 @@ module ClientSideValidations::ActiveModel
5
5
 
6
6
  def client_side_hash(model, attribute)
7
7
  options = self.options.dup
8
- { :message => model.errors.generate_message(attribute, message_type, options) }.
9
- merge(options.except(*::ActiveModel::Errors::CALLBACKS_OPTIONS - [:allow_blank, :if, :unless]))
8
+ { :message => model.errors.generate_message(attribute, message_type, options) }.merge(options.except(*::ActiveModel::Errors::CALLBACKS_OPTIONS - [:allow_blank, :if, :unless]))
10
9
  end
11
10
 
12
11
  private
@@ -2,7 +2,6 @@ module ClientSideValidations::ActiveRecord
2
2
  class Middleware
3
3
 
4
4
  def self.is_unique?(klass, attribute, value, params)
5
- value = type_cast_value(klass, attribute, value)
6
5
  column = klass.columns_hash[attribute.to_s]
7
6
  value = column.limit ? value.to_s.mb_chars[0, column.limit] : value.to_s if column.text?
8
7
 
@@ -24,18 +23,11 @@ module ClientSideValidations::ActiveRecord
24
23
  relation = relation.and(t.primary_key.not_eq(params[:id])) if params[:id]
25
24
  end
26
25
 
27
- (params[:scope] || {}).each do |attribute, value|
28
- value = type_cast_value(klass, attribute, value)
29
- relation = relation.and(t[attribute].eq(value))
26
+ (params[:scope] || {}).each do |key, value|
27
+ relation = relation.and(t[key].eq(value))
30
28
  end
31
29
 
32
30
  !klass.where(relation).exists?
33
31
  end
34
-
35
- private
36
-
37
- def self.type_cast_value(klass, attribute, value)
38
- klass.columns_hash[attribute].type_cast(value)
39
- end
40
32
  end
41
33
  end
@@ -1,13 +1,13 @@
1
1
  module ClientSideValidations
2
2
  module Formtastic
3
- module SemanticFormBuilder
3
+ module FormBuilder
4
4
 
5
5
  def self.included(base)
6
6
  base.class_eval do
7
7
  def self.client_side_form_settings(options, form_helper)
8
8
  {
9
9
  :type => self.to_s,
10
- :inline_error_class => ::Formtastic::SemanticFormBuilder.default_inline_error_class
10
+ :inline_error_class => ::Formtastic::FormBuilder.default_inline_error_class
11
11
  }
12
12
  end
13
13
  end
@@ -17,5 +17,5 @@ module ClientSideValidations
17
17
  end
18
18
  end
19
19
 
20
- Formtastic::SemanticFormBuilder.send(:include, ClientSideValidations::Formtastic::SemanticFormBuilder)
20
+ Formtastic::FormBuilder.send(:include, ClientSideValidations::Formtastic::FormBuilder)
21
21
 
@@ -1,9 +1,10 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'client_side_validations/core_ext'
4
+
3
5
  module ClientSideValidations
4
6
 
5
7
  module Middleware
6
-
7
8
  class Validators
8
9
  def initialize(app)
9
10
  @app = app
@@ -11,7 +12,7 @@ module ClientSideValidations
11
12
 
12
13
  def call(env)
13
14
  case env['PATH_INFO']
14
- when %r{\/validators\/(\w+)\.json}
15
+ when %r{\/validators\/(\w+)}
15
16
  "::ClientSideValidations::Middleware::#{$1.camelize}".constantize.new(env).response
16
17
  else
17
18
  @app.call(env)
@@ -74,7 +75,7 @@ module ClientSideValidations
74
75
  parent_key = (request.params.keys - IGNORE_PARAMS).first
75
76
  end
76
77
  end
77
-
78
78
  end
79
+
79
80
  end
80
81
 
@@ -1,3 +1,3 @@
1
1
  module ClientSideValidations
2
- VERSION = '3.0.14'
2
+ VERSION = '3.1.0'
3
3
  end
@@ -0,0 +1,23 @@
1
+ module ClientSideValidations
2
+ module Generators
3
+ class CopyAssetGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../../../../vendor/assets/javascripts', __FILE__)
5
+ if Rails.version >= '3.1'
6
+ desc 'Creates a ClientSideValidations initializer and copies rails.validations.js to app/assets/javascripts.'
7
+ else
8
+ desc 'Creates a ClientSideValidations initializer and copies rails.validations.js to public/javascripts.'
9
+ end
10
+
11
+ def copy_javascript_asset
12
+ if Rails.version >= '3.1'
13
+ destination = 'app/assets/javascripts'
14
+ else
15
+ destination = 'public/javascripts'
16
+ end
17
+
18
+ copy_file 'rails.validations.js', "#{destination}/rails.validations.js"
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -1,20 +1,32 @@
1
1
  module ClientSideValidations
2
2
  module Generators
3
3
  class InstallGenerator < Rails::Generators::Base
4
- source_root File.expand_path('../../templates', __FILE__)
4
+ source_root File.expand_path('../../templates/client_side_validations', __FILE__)
5
5
 
6
- desc 'Creates a ClientSideValidations initializer and copies client-side-validations.js to public/javascripts.'
6
+ if Rails.version >= '3.1'
7
+ desc 'Creates a ClientSideValidations initializer.'
8
+ else
9
+ desc 'Creates a ClientSideValidations initializer and copies rails.validations.js to public/javascripts.'
10
+ end
7
11
 
8
12
  def copy_initializer
9
- copy_file 'client_side_validations/initializer.rb', 'config/initializers/client_side_validations.rb'
13
+ copy_file 'initializer.rb', 'config/initializers/client_side_validations.rb'
10
14
  end
11
15
 
12
- def copy_locale
13
- copy_file '../../../vendor/assets/javascripts/rails.validations.js', 'public/javascripts/rails.validations.js'
16
+ def copy_javascript_asset
17
+ if Rails.version >= '3.0'
18
+ if Rails.version < '3.1'
19
+ copy_file '../../../../vendor/assets/javascripts/rails.validations.js', 'public/javascripts/rails.validations.js'
20
+ end
21
+ end
14
22
  end
15
23
 
16
24
  def show_readme
17
- readme 'client_side_validations/README' if behavior == :invoke
25
+ if Rails.version >= '3.1'
26
+ readme 'README.rails.3.1' if behavior == :invoke
27
+ else
28
+ readme 'README.rails.3.0' if behavior == :invoke
29
+ end
18
30
  end
19
31
  end
20
32
  end
@@ -0,0 +1,6 @@
1
+ *********************
2
+ ClientSideValidations
3
+ *********************
4
+
5
+ Client Side Validations only works with jQuery.
6
+ You'll need to include jQuery in your layout file before you include 'rails.validations.js'
@@ -0,0 +1,7 @@
1
+ *********************
2
+ ClientSideValidations
3
+ *********************
4
+
5
+ In your app/assets/javascripts/application.js file add the following:
6
+
7
+ //= require rails.validations
@@ -1,7 +1,7 @@
1
1
  # ClientSideValidations Initializer
2
2
 
3
3
  require 'client_side_validations/simple_form' if defined?(::SimpleForm)
4
- require 'client_side_validations/formtastic' if defined?(::Formtastic)
4
+ require 'client_side_validations/formtastic' if defined?(::Formtastic)
5
5
 
6
6
  # Uncomment the following block if you want each input field to have the validation messages attached.
7
7
  # ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
@@ -1,8 +1,14 @@
1
1
  require 'base_helper'
2
2
  require 'action_view'
3
- require 'client_side_validations/action_view'
4
- require 'action_view/template/handlers/erb'
3
+ require 'action_view/template'
5
4
  require 'action_view/models'
5
+ require 'client_side_validations/action_view'
6
+
7
+ module ActionController
8
+ class Base
9
+ include ActionDispatch::Routing::RouteSet.new.url_helpers
10
+ end
11
+ end
6
12
 
7
13
  module ActionViewTestSetup
8
14
  include ::ClientSideValidations::ActionView::Helpers::FormHelper
@@ -12,6 +18,19 @@ module ActionViewTestSetup
12
18
  @output_buffer = super
13
19
  end
14
20
 
21
+ Routes = ActionDispatch::Routing::RouteSet.new
22
+ Routes.draw do
23
+ resources :posts do
24
+ resources :comments
25
+ end
26
+ end
27
+
28
+ def _routes
29
+ Routes
30
+ end
31
+
32
+ include Routes.url_helpers
33
+
15
34
  def setup
16
35
  super
17
36
 
@@ -68,15 +87,10 @@ module ActionViewTestSetup
68
87
  @post.secret = 1
69
88
  @post.written_on = Date.new(2004, 6, 15)
70
89
 
71
- @_content_for = Hash.new { |h,k| h[k] = ActiveSupport::SafeBuffer.new }
72
- end
73
-
74
- def url_for(object)
75
- @url_for_options = object
76
- if object.is_a?(Hash)
77
- "http://www.example.com"
90
+ if defined?(ActionView::OutputFlow)
91
+ @view_flow = ActionView::OutputFlow.new
78
92
  else
79
- super
93
+ @_content_for = Hash.new { |h,k| h[k] = ActiveSupport::SafeBuffer.new }
80
94
  end
81
95
  end
82
96
 
@@ -87,11 +101,12 @@ module ActionViewTestSetup
87
101
  txt << %{</div>}
88
102
  end
89
103
 
90
- def form_text(action = "http://www.example.com", id = nil, html_class = nil, remote = nil, validators = nil)
104
+ def form_text(action = "http://www.example.com", id = nil, html_class = nil, remote = nil, validators = nil, file = nil)
91
105
  txt = %{<form accept-charset="UTF-8" action="#{action}"}
92
106
  txt << %{ data-remote="true"} if remote
93
107
  txt << %{ class="#{html_class}"} if html_class
94
108
  txt << %{ data-validate="true"} if validators
109
+ txt << %{ enctype="multipart/form-data"} if file
95
110
  txt << %{ id="#{id}"} if id
96
111
  txt << %{ method="post"}
97
112
  txt << %{ novalidate="novalidate"} if validators
@@ -102,12 +117,12 @@ module ActionViewTestSetup
102
117
  contents = block_given? ? yield : ""
103
118
 
104
119
  if options.is_a?(Hash)
105
- method, remote, validators = options.values_at(:method, :remote, :validators)
120
+ method, remote, validators, file = options.values_at(:method, :remote, :validators, :file)
106
121
  else
107
122
  method = options
108
123
  end
109
124
 
110
- html = form_text(action, id, html_class, remote, validators) + snowman(method) + (contents || "") + "</form>"
125
+ html = form_text(action, id, html_class, remote, validators, file) + snowman(method) + (contents || "") + "</form>"
111
126
 
112
127
  if options.is_a?(Hash) && options[:validators]
113
128
  build_script_tag(html, id, options[:validators])
@@ -158,3 +173,4 @@ module ActionViewTestSetup
158
173
  end
159
174
 
160
175
  end
176
+
@@ -44,7 +44,7 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
44
44
  end
45
45
 
46
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
47
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators, :file => true) do
48
48
  %{<input data-validate="true" id="post_cost" name="post[cost]" type="file" />}
49
49
  end
50
50
  assert_equal expected, output_buffer
@@ -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_validators_should_be_filtered
349
+ def test_conditional_validator_filters
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_validator_filters_being_forced
382
+ def test_conditional_validators_should_be_filtered
383
383
  hash = {
384
384
  :cost => {
385
385
  :presence => {
@@ -400,44 +400,8 @@ 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, :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)
403
+ concat f.text_field(:cost)
404
+ concat f.text_field(:title)
441
405
  end
442
406
 
443
407
  validators = {}
@@ -448,7 +412,7 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
448
412
  assert_equal expected, output_buffer
449
413
  end
450
414
 
451
- def test_conditional_validator_filters_being_forced_individually
415
+ def test_conditional_validator_filters_being_forced
452
416
  hash = {
453
417
  :cost => {
454
418
  :presence => {
@@ -469,8 +433,8 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
469
433
  @post.stubs(:can_validate?).returns(true)
470
434
  @post.stubs(:client_side_validation_hash).returns(hash)
471
435
  form_for(@post, :validate => true) do |f|
472
- concat f.text_field(:cost, :validate => { :presence => true })
473
- concat f.text_field(:title, :validate => { :presence => true })
436
+ concat f.text_field(:cost, :validate => true)
437
+ concat f.text_field(:title, :validate => true)
474
438
  end
475
439
 
476
440
  validators = {
@@ -484,7 +448,7 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
484
448
  assert_equal expected, output_buffer
485
449
  end
486
450
 
487
- def test_conditional_validator_filters_being_forced_and_not_meeting_condition_individually
451
+ def test_conditional_validator_filters_being_forced_and_not_meeting_condition
488
452
  hash = {
489
453
  :cost => {
490
454
  :presence => {
@@ -505,8 +469,8 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
505
469
  @post.stubs(:can_validate?).returns(false)
506
470
  @post.stubs(:client_side_validation_hash).returns(hash)
507
471
  form_for(@post, :validate => true) do |f|
508
- concat f.text_field(:cost, :validate => { :presence => true })
509
- concat f.text_field(:title, :validate => { :presence => true })
472
+ concat f.text_field(:cost, :validate => true)
473
+ concat f.text_field(:title, :validate => true)
510
474
  end
511
475
 
512
476
  validators = {}
@@ -517,18 +481,18 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
517
481
  assert_equal expected, output_buffer
518
482
  end
519
483
 
520
- def test_conditional_validator_filters_being_forced_with_procs
484
+ def test_conditional_validator_filters_being_forced_individually
521
485
  hash = {
522
486
  :cost => {
523
487
  :presence => {
524
488
  :message => "can't be blank",
525
- :unless => Proc.new { |post| post.cannot_validate? }
489
+ :unless => :cannot_validate?
526
490
  }
527
491
  },
528
492
  :title => {
529
493
  :presence => {
530
494
  :message => "can't be blank",
531
- :if => Proc.new { |post| post.can_validate? }
495
+ :if => :can_validate?
532
496
  }
533
497
  }
534
498
  }
@@ -538,8 +502,8 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
538
502
  @post.stubs(:can_validate?).returns(true)
539
503
  @post.stubs(:client_side_validation_hash).returns(hash)
540
504
  form_for(@post, :validate => true) do |f|
541
- concat f.text_field(:cost, :validate => true)
542
- concat f.text_field(:title, :validate => true)
505
+ concat f.text_field(:cost, :validate => { :presence => true })
506
+ concat f.text_field(:title, :validate => { :presence => true })
543
507
  end
544
508
 
545
509
  validators = {
@@ -553,57 +517,58 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
553
517
  assert_equal expected, output_buffer
554
518
  end
555
519
 
556
- def test_conditional_validator_ignored_when_using_changed_helpers
520
+ def test_conditional_validator_filters_being_forced_and_not_meeting_condition_individually
557
521
  hash = {
558
522
  :cost => {
559
523
  :presence => {
560
524
  :message => "can't be blank",
561
- :unless => :cost_changed?
525
+ :unless => :cannot_validate?
562
526
  }
563
527
  },
564
528
  :title => {
565
529
  :presence => {
566
530
  :message => "can't be blank",
567
- :if => :title_changed?
531
+ :if => :can_validate?
568
532
  }
569
533
  }
570
534
  }
571
535
 
572
536
  @post.title = nil
537
+ @post.stubs(:cannot_validate?).returns(true)
538
+ @post.stubs(:can_validate?).returns(false)
573
539
  @post.stubs(:client_side_validation_hash).returns(hash)
574
540
  form_for(@post, :validate => true) do |f|
575
- concat f.text_field(:cost)
576
- concat f.text_field(:title)
541
+ concat f.text_field(:cost, :validate => { :presence => true })
542
+ concat f.text_field(:title, :validate => { :presence => true })
577
543
  end
578
544
 
579
- validators = {
580
- 'post[cost]' => {:presence => {:message => "can't be blank"}},
581
- 'post[title]' => {:presence => {:message => "can't be blank"}}
582
- }
545
+ validators = {}
583
546
  expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
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" />}
547
+ %{<input id="post_cost" name="post[cost]" size="30" type="text" />} +
548
+ %{<input id="post_title" name="post[title]" size="30" type="text" />}
586
549
  end
587
550
  assert_equal expected, output_buffer
588
551
  end
589
552
 
590
- def test_conditional_validator_ignored_when_using_changed_helpers_and_forcing_validators
553
+ def test_conditional_validator_filters_being_forced_with_procs
591
554
  hash = {
592
555
  :cost => {
593
556
  :presence => {
594
557
  :message => "can't be blank",
595
- :unless => :cost_changed?
558
+ :unless => Proc.new { |post| post.cannot_validate? }
596
559
  }
597
560
  },
598
561
  :title => {
599
562
  :presence => {
600
563
  :message => "can't be blank",
601
- :if => :title_changed?
564
+ :if => Proc.new { |post| post.can_validate? }
602
565
  }
603
566
  }
604
567
  }
605
568
 
606
569
  @post.title = nil
570
+ @post.stubs(:cannot_validate?).returns(false)
571
+ @post.stubs(:can_validate?).returns(true)
607
572
  @post.stubs(:client_side_validation_hash).returns(hash)
608
573
  form_for(@post, :validate => true) do |f|
609
574
  concat f.text_field(:cost, :validate => true)