client_side_validations 3.1.5 → 3.2.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/client_side_validations.gemspec +1 -9
  2. data/lib/client_side_validations.rb +6 -2
  3. data/lib/client_side_validations/action_view/form_builder.rb +4 -2
  4. data/lib/client_side_validations/action_view/form_helper.rb +15 -7
  5. data/lib/client_side_validations/active_model.rb +5 -2
  6. data/lib/client_side_validations/active_record.rb +2 -1
  7. data/lib/client_side_validations/active_record/middleware.rb +15 -2
  8. data/lib/client_side_validations/active_record/uniqueness.rb +0 -1
  9. data/lib/client_side_validations/core_ext/regexp.rb +1 -1
  10. data/lib/client_side_validations/middleware.rb +40 -16
  11. data/lib/client_side_validations/version.rb +1 -1
  12. data/lib/generators/templates/client_side_validations/initializer.rb +0 -3
  13. data/vendor/assets/javascripts/rails.validations.js +135 -170
  14. metadata +22 -329
  15. data/lib/client_side_validations/formtastic.rb +0 -21
  16. data/lib/client_side_validations/mongo_mapper.rb +0 -9
  17. data/lib/client_side_validations/mongo_mapper/middleware.rb +0 -20
  18. data/lib/client_side_validations/mongo_mapper/uniqueness.rb +0 -29
  19. data/lib/client_side_validations/mongoid.rb +0 -9
  20. data/lib/client_side_validations/mongoid/format.rb +0 -10
  21. data/lib/client_side_validations/mongoid/length.rb +0 -24
  22. data/lib/client_side_validations/mongoid/middleware.rb +0 -20
  23. data/lib/client_side_validations/mongoid/presence.rb +0 -10
  24. data/lib/client_side_validations/mongoid/uniqueness.rb +0 -29
  25. data/lib/client_side_validations/simple_form.rb +0 -24
  26. data/lib/generators/templates/client_side_validations/README.rails.3.0 +0 -6
  27. data/test/action_view/cases/helper.rb +0 -176
  28. data/test/action_view/cases/test_helpers.rb +0 -635
  29. data/test/action_view/cases/test_legacy_helpers.rb +0 -217
  30. data/test/action_view/models.rb +0 -3
  31. data/test/action_view/models/comment.rb +0 -35
  32. data/test/action_view/models/post.rb +0 -35
  33. data/test/active_model/cases/helper.rb +0 -4
  34. data/test/active_model/cases/test_acceptance_validator.rb +0 -16
  35. data/test/active_model/cases/test_base.rb +0 -11
  36. data/test/active_model/cases/test_confirmation_validator.rb +0 -16
  37. data/test/active_model/cases/test_exclusion_validator.rb +0 -20
  38. data/test/active_model/cases/test_format_validator.rb +0 -21
  39. data/test/active_model/cases/test_inclusion_validator.rb +0 -21
  40. data/test/active_model/cases/test_length_validator.rb +0 -61
  41. data/test/active_model/cases/test_numericality_validator.rb +0 -46
  42. data/test/active_model/cases/test_presence_validator.rb +0 -16
  43. data/test/active_model/cases/test_validations.rb +0 -175
  44. data/test/active_model/models/person.rb +0 -17
  45. data/test/active_record/cases/helper.rb +0 -12
  46. data/test/active_record/cases/test_base.rb +0 -11
  47. data/test/active_record/cases/test_middleware.rb +0 -175
  48. data/test/active_record/cases/test_uniqueness_validator.rb +0 -56
  49. data/test/active_record/models/guid.rb +0 -7
  50. data/test/active_record/models/user.rb +0 -14
  51. data/test/base_helper.rb +0 -21
  52. data/test/core_ext/cases/test_core_ext.rb +0 -46
  53. data/test/formtastic/cases/helper.rb +0 -7
  54. data/test/formtastic/cases/test_form_builder.rb +0 -11
  55. data/test/formtastic/cases/test_form_helper.rb +0 -21
  56. data/test/generators/cases/test_generators.rb +0 -70
  57. data/test/javascript/config.ru +0 -3
  58. data/test/javascript/public/test/callbacks/elementAfter.js +0 -54
  59. data/test/javascript/public/test/callbacks/elementBefore.js +0 -54
  60. data/test/javascript/public/test/callbacks/elementFail.js +0 -70
  61. data/test/javascript/public/test/callbacks/elementPass.js +0 -70
  62. data/test/javascript/public/test/callbacks/formAfter.js +0 -45
  63. data/test/javascript/public/test/callbacks/formBefore.js +0 -45
  64. data/test/javascript/public/test/callbacks/formFail.js +0 -51
  65. data/test/javascript/public/test/callbacks/formPass.js +0 -50
  66. data/test/javascript/public/test/form_builders/validateForm.js +0 -66
  67. data/test/javascript/public/test/form_builders/validateFormtastic.js +0 -54
  68. data/test/javascript/public/test/form_builders/validateNestedForm.js +0 -66
  69. data/test/javascript/public/test/form_builders/validateSimpleForm.js +0 -57
  70. data/test/javascript/public/test/settings.js +0 -15
  71. data/test/javascript/public/test/validateElement.js +0 -179
  72. data/test/javascript/public/test/validators/acceptance.js +0 -42
  73. data/test/javascript/public/test/validators/confirmation.js +0 -25
  74. data/test/javascript/public/test/validators/exclusion.js +0 -41
  75. data/test/javascript/public/test/validators/format.js +0 -27
  76. data/test/javascript/public/test/validators/inclusion.js +0 -42
  77. data/test/javascript/public/test/validators/length.js +0 -76
  78. data/test/javascript/public/test/validators/numericality.js +0 -158
  79. data/test/javascript/public/test/validators/presence.js +0 -21
  80. data/test/javascript/public/test/validators/uniqueness.js +0 -107
  81. data/test/javascript/public/vendor/jquery.metadata.js +0 -122
  82. data/test/javascript/public/vendor/qunit.css +0 -196
  83. data/test/javascript/public/vendor/qunit.js +0 -1374
  84. data/test/javascript/server.rb +0 -84
  85. data/test/javascript/views/index.erb +0 -20
  86. data/test/javascript/views/layout.erb +0 -21
  87. data/test/middleware/cases/helper.rb +0 -7
  88. data/test/middleware/cases/test_middleware.rb +0 -8
  89. data/test/mongo_mapper/cases/helper.rb +0 -9
  90. data/test/mongo_mapper/cases/test_base.rb +0 -15
  91. data/test/mongo_mapper/cases/test_middleware.rb +0 -77
  92. data/test/mongo_mapper/cases/test_uniqueness_validator.rb +0 -55
  93. data/test/mongo_mapper/models/magazine.rb +0 -11
  94. data/test/mongoid/cases/helper.rb +0 -12
  95. data/test/mongoid/cases/test_base.rb +0 -15
  96. data/test/mongoid/cases/test_format_validator.rb +0 -21
  97. data/test/mongoid/cases/test_length_validator.rb +0 -60
  98. data/test/mongoid/cases/test_middleware.rb +0 -77
  99. data/test/mongoid/cases/test_presence_validator.rb +0 -16
  100. data/test/mongoid/cases/test_uniqueness_validator.rb +0 -54
  101. data/test/mongoid/models/book.rb +0 -12
  102. data/test/simple_form/cases/helper.rb +0 -5
  103. data/test/simple_form/cases/test_form_builder.rb +0 -14
  104. data/test/simple_form/cases/test_form_helper.rb +0 -24
  105. data/test/test_loader.rb +0 -6
@@ -1,9 +0,0 @@
1
- require 'client_side_validations/active_model'
2
- require 'client_side_validations/mongo_mapper/middleware'
3
-
4
- %w{uniqueness}.each do |validator|
5
- require "client_side_validations/mongo_mapper/#{validator}"
6
- validator.capitalize!
7
- eval "MongoMapper::Plugins::Validations::#{validator}Validator.send(:include, ClientSideValidations::MongoMapper::#{validator})"
8
- end
9
-
@@ -1,20 +0,0 @@
1
- module ClientSideValidations::MongoMapper
2
- class Middleware
3
-
4
- # Still need to handle embedded documents
5
- def self.is_unique?(klass, attribute, value, params)
6
- if params[:case_sensitive] == 'false'
7
- value = Regexp.new("^#{Regexp.escape(value.to_s)}$", Regexp::IGNORECASE)
8
- end
9
-
10
- criteria = klass.where(attribute => value)
11
- criteria = criteria.where(:_id => {'$ne' => BSON::ObjectId(params[:id])}) if params[:id]
12
-
13
- (params[:scope] || {}).each do |key, value|
14
- criteria = criteria.where(key => value)
15
- end
16
-
17
- !criteria.exists?
18
- end
19
- end
20
- end
@@ -1,29 +0,0 @@
1
- module ClientSideValidations::MongoMapper
2
- module Uniqueness
3
- def client_side_hash(model, attribute)
4
- hash = {}
5
- hash[:message] = model.errors.generate_message(attribute, message_type, options.except(:scope))
6
- hash[:case_sensitive] = options[:case_sensitive] if options.key?(:case_sensitive)
7
- hash[:allow_blank] = options[:allow_blank] if options.key?(:allow_blank)
8
- hash[:id] = model.id unless model.new_record?
9
- if options.key?(:scope) && options[:scope].present?
10
- hash[:scope] = Array.wrap(options[:scope]).inject({}) do |scope_hash, scope_item|
11
- scope_hash.merge!(scope_item => model.send(scope_item))
12
- end
13
- end
14
-
15
- unless model.class.name.demodulize == model.class.name
16
- hash[:class] = model.class.name.underscore
17
- end
18
-
19
- hash
20
- end
21
-
22
- private
23
-
24
- def message_type
25
- :taken
26
- end
27
- end
28
- end
29
-
@@ -1,9 +0,0 @@
1
- require 'client_side_validations/active_model'
2
- require 'client_side_validations/mongoid/middleware'
3
-
4
- %w{format length presence uniqueness}.each do |validator|
5
- require "client_side_validations/mongoid/#{validator}"
6
- validator.capitalize!
7
- eval "Mongoid::Validations::#{validator}Validator.send(:include, ClientSideValidations::Mongoid::#{validator})"
8
- end
9
-
@@ -1,10 +0,0 @@
1
- module ClientSideValidations::Mongoid
2
- module Format
3
- private
4
-
5
- def message_type
6
- :invalid
7
- end
8
- end
9
- end
10
-
@@ -1,24 +0,0 @@
1
- module ClientSideValidations::Mongoid
2
- module Length
3
-
4
- def client_side_hash(model, attribute)
5
- options = self.options.dup
6
- hash = { :messages => {} }
7
- hash[:js_tokenizer] = options[:js_tokenizer] if options[:js_tokenizer]
8
- hash[:allow_blank] = true if options[:allow_blank]
9
-
10
- self.class::MESSAGES.each do |option, message_type|
11
- if count = options[option]
12
- options[:message] = options[message_type]
13
- options.delete(:message) if options[:message].nil?
14
- hash[:messages][option] = model.errors.generate_message(attribute, message_type, options.merge(:count => count))
15
- hash[option] = count
16
- end
17
- end
18
-
19
- hash
20
- end
21
-
22
- end
23
- end
24
-
@@ -1,20 +0,0 @@
1
- module ClientSideValidations::Mongoid
2
- class Middleware
3
-
4
- # Still need to handle embedded documents
5
- def self.is_unique?(klass, attribute, value, params)
6
- if params[:case_sensitive] == 'false'
7
- value = Regexp.new("^#{Regexp.escape(value.to_s)}$", Regexp::IGNORECASE)
8
- end
9
-
10
- criteria = klass.where(attribute => value)
11
- criteria = criteria.where(:_id => {'$ne' => BSON::ObjectId(params[:id])}) if params[:id]
12
-
13
- (params[:scope] || {}).each do |key, value|
14
- criteria = criteria.where(key => value)
15
- end
16
-
17
- !criteria.exists?
18
- end
19
- end
20
- end
@@ -1,10 +0,0 @@
1
- module ClientSideValidations::Mongoid
2
- module Presence
3
- private
4
-
5
- def message_type
6
- :blank
7
- end
8
- end
9
- end
10
-
@@ -1,29 +0,0 @@
1
- module ClientSideValidations::Mongoid
2
- module Uniqueness
3
- def client_side_hash(model, attribute)
4
- hash = {}
5
- hash[:message] = model.errors.generate_message(attribute, message_type, options.except(:scope))
6
- hash[:case_sensitive] = options[:case_sensitive] if options.key?(:case_sensitive)
7
- hash[:allow_blank] = options[:allow_blank] if options.key?(:allow_blank)
8
- hash[:id] = model.id unless model.new_record?
9
- if options.key?(:scope) && options[:scope].present?
10
- hash[:scope] = Array.wrap(options[:scope]).inject({}) do |scope_hash, scope_item|
11
- scope_hash.merge!(scope_item => model.send(scope_item))
12
- end
13
- end
14
-
15
- unless model.class.name.demodulize == model.class.name
16
- hash[:class] = model.class.name.underscore
17
- end
18
-
19
- hash
20
- end
21
-
22
- private
23
-
24
- def message_type
25
- :taken
26
- end
27
- end
28
- end
29
-
@@ -1,24 +0,0 @@
1
- module ClientSideValidations
2
- module SimpleForm
3
- module FormBuilder
4
-
5
- def self.included(base)
6
- base.class_eval do
7
- def self.client_side_form_settings(options, form_helper)
8
- {
9
- :type => self.to_s,
10
- :error_class => ::SimpleForm.error_class,
11
- :error_tag => ::SimpleForm.error_tag,
12
- :wrapper_error_class => ::SimpleForm.wrapper_error_class,
13
- :wrapper_tag => ::SimpleForm.wrapper_tag
14
- }
15
- end
16
- end
17
- end
18
-
19
- end
20
- end
21
- end
22
-
23
- SimpleForm::FormBuilder.send(:include, ClientSideValidations::SimpleForm::FormBuilder)
24
-
@@ -1,6 +0,0 @@
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'
@@ -1,176 +0,0 @@
1
- require 'base_helper'
2
- require 'action_view'
3
- require 'action_view/template'
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
12
-
13
- module ActionViewTestSetup
14
- include ::ClientSideValidations::ActionView::Helpers::FormHelper
15
- include ::ClientSideValidations::ActionView::Helpers::FormTagHelper
16
-
17
- def form_for(*)
18
- @output_buffer = super
19
- end
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
-
34
- def setup
35
- super
36
-
37
- # Create "label" locale for testing I18n label helpers
38
- I18n.backend.store_translations 'label', {
39
- :activemodel => {
40
- :attributes => {
41
- :post => {
42
- :cost => "Total cost"
43
- }
44
- }
45
- },
46
- :helpers => {
47
- :label => {
48
- :post => {
49
- :body => "Write entire text here"
50
- }
51
- }
52
- }
53
- }
54
-
55
- # Create "submit" locale for testing I18n submit helpers
56
- I18n.backend.store_translations 'submit', {
57
- :helpers => {
58
- :submit => {
59
- :create => 'Create %{model}',
60
- :update => 'Confirm %{model} changes',
61
- :submit => 'Save changes',
62
- :another_post => {
63
- :update => 'Update your %{model}'
64
- }
65
- }
66
- }
67
- }
68
-
69
- @post = Post.new
70
- @comment = Comment.new
71
- def @post.errors()
72
- Class.new{
73
- def [](field); field == "author_name" ? ["can't be empty"] : [] end
74
- def empty?() false end
75
- def count() 1 end
76
- def full_messages() [ "Author name can't be empty" ] end
77
- }.new
78
- end
79
- def @post.id; 123; end
80
- def @post.id_before_type_cast; 123; end
81
- def @post.to_param; '123'; end
82
-
83
- @post.persisted = true
84
- @post.title = "Hello World"
85
- @post.author_name = ""
86
- @post.body = "Back to the hill and over it again!"
87
- @post.secret = 1
88
- @post.written_on = Date.new(2004, 6, 15)
89
-
90
- if defined?(ActionView::OutputFlow)
91
- @view_flow = ActionView::OutputFlow.new
92
- else
93
- @_content_for = Hash.new { |h,k| h[k] = ActiveSupport::SafeBuffer.new }
94
- end
95
- end
96
-
97
- def snowman(method = nil)
98
- txt = %{<div style="margin:0;padding:0;display:inline">}
99
- txt << %{<input name="utf8" type="hidden" value="&#x2713;" />}
100
- txt << %{<input name="_method" type="hidden" value="#{method}" />} if method
101
- txt << %{</div>}
102
- end
103
-
104
- def form_text(action = "http://www.example.com", id = nil, html_class = nil, remote = nil, validators = nil, file = nil)
105
- txt = %{<form accept-charset="UTF-8" action="#{action}"}
106
- txt << %{ data-remote="true"} if remote
107
- txt << %{ class="#{html_class}"} if html_class
108
- txt << %{ data-validate="true"} if validators
109
- txt << %{ enctype="multipart/form-data"} if file
110
- txt << %{ id="#{id}"} if id
111
- txt << %{ method="post"}
112
- txt << %{ novalidate="novalidate"} if validators
113
- txt << %{>}
114
- end
115
-
116
- def whole_form(action = "http://www.example.com", id = nil, html_class = nil, options = nil)
117
- contents = block_given? ? yield : ""
118
-
119
- if options.is_a?(Hash)
120
- method, remote, validators, file = options.values_at(:method, :remote, :validators, :file)
121
- else
122
- method = options
123
- end
124
-
125
- html = form_text(action, id, html_class, remote, validators, file) + snowman(method) + (contents || "") + "</form>"
126
-
127
- if options.is_a?(Hash) && options[:validators]
128
- build_script_tag(html, id, options[:validators])
129
- else
130
- html
131
- end
132
- end
133
-
134
- def build_script_tag(html, id, validators)
135
- (html || "") + %Q{<script>window['#{id}'] = #{client_side_form_settings_helper.merge(:validators => validators).to_json};</script>}
136
- end
137
-
138
- protected
139
- def comments_path(post)
140
- "/posts/#{post.id}/comments"
141
- end
142
- alias_method :post_comments_path, :comments_path
143
-
144
- def comment_path(post, comment)
145
- "/posts/#{post.id}/comments/#{comment.id}"
146
- end
147
- alias_method :post_comment_path, :comment_path
148
-
149
- def admin_comments_path(post)
150
- "/admin/posts/#{post.id}/comments"
151
- end
152
- alias_method :admin_post_comments_path, :admin_comments_path
153
-
154
- def admin_comment_path(post, comment)
155
- "/admin/posts/#{post.id}/comments/#{comment.id}"
156
- end
157
- alias_method :admin_post_comment_path, :admin_comment_path
158
-
159
- def posts_path
160
- "/posts"
161
- end
162
-
163
- def post_path(post, options = {})
164
- if options[:format]
165
- "/posts/#{post.id}.#{options[:format]}"
166
- else
167
- "/posts/#{post.id}"
168
- end
169
- end
170
-
171
- def protect_against_forgery?
172
- false
173
- end
174
-
175
- end
176
-
@@ -1,635 +0,0 @@
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, :file => true) do
48
- %{<input data-validate="true" id="post_cost" name="post[cost]" 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_search_field
66
- form_for(@post, :validate => true) do |f|
67
- concat f.search_field(: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 data-validate="true" id="post_cost" name="post[cost]" size="30" type="search" />}
73
- end
74
- assert_equal expected, output_buffer
75
- end
76
-
77
- def test_telephone_field
78
- form_for(@post, :validate => true) do |f|
79
- concat f.telephone_field(:cost)
80
- end
81
-
82
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
83
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
84
- %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="tel" />}
85
- end
86
- assert_equal expected, output_buffer
87
- end
88
-
89
- def test_phone_field
90
- form_for(@post, :validate => true) do |f|
91
- concat f.phone_field(:cost)
92
- end
93
-
94
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
95
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
96
- %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="tel" />}
97
- end
98
- assert_equal expected, output_buffer
99
- end
100
-
101
- def test_url_field
102
- form_for(@post, :validate => true) do |f|
103
- concat f.url_field(:cost)
104
- end
105
-
106
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
107
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
108
- %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="url" />}
109
- end
110
- assert_equal expected, output_buffer
111
- end
112
-
113
- def test_email_field
114
- form_for(@post, :validate => true) do |f|
115
- concat f.email_field(:cost)
116
- end
117
-
118
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
119
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
120
- %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="email" />}
121
- end
122
- assert_equal expected, output_buffer
123
- end
124
-
125
- def test_number_field
126
- form_for(@post, :validate => true) do |f|
127
- concat f.number_field(:cost)
128
- end
129
-
130
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
131
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
132
- %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="number" />}
133
- end
134
- assert_equal expected, output_buffer
135
- end
136
-
137
- def test_range_field
138
- form_for(@post, :validate => true) do |f|
139
- concat f.range_field(:cost)
140
- end
141
-
142
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
143
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
144
- %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="range" />}
145
- end
146
- assert_equal expected, output_buffer
147
- end
148
-
149
- def test_check_box
150
- form_for(@post, :validate => true) do |f|
151
- concat f.check_box(:cost)
152
- end
153
-
154
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
155
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
156
- %{<input name="post[cost]" type="hidden" value="0" />} +
157
- %{<input data-validate="true" id="post_cost" name="post[cost]" type="checkbox" value="1" />}
158
- end
159
- assert_equal expected, output_buffer
160
- end
161
-
162
- def test_radio_button
163
- form_for(@post, :validate => true) do |f|
164
- concat f.radio_button(:cost, "10")
165
- end
166
-
167
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
168
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
169
- %{<input data-validate="true" id="post_cost_10" name="post[cost]" type="radio" value="10" />}
170
- end
171
- assert_equal expected, output_buffer
172
- end
173
-
174
- def test_field_without_validations
175
- form_for(@post, :validate => true) do |f|
176
- concat f.text_field(:title)
177
- end
178
-
179
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => {}) do
180
- %{<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />}
181
- end
182
- assert_equal expected, output_buffer
183
- end
184
-
185
- def test_text_field_with_validations_turned_off
186
- form_for(@post, :validate => true) do |f|
187
- concat f.text_field(:cost, :validate => false)
188
- end
189
-
190
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => {}) do
191
- %{<input id="post_cost" name="post[cost]" size="30" type="text" />}
192
- end
193
- assert_equal expected, output_buffer
194
- end
195
-
196
- def test_nested_fields_for_inherit_validation_settings
197
- form_for(@post, :validate => true) do |f|
198
- concat f.fields_for(:comment, @comment) { |c|
199
- concat c.text_field(:title)
200
- }
201
- end
202
-
203
- validators = {'post[comment][title]' => {:presence => {:message => "can't be blank"}}}
204
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
205
- %{<input data-validate="true" id="post_comment_title" name="post[comment][title]" size="30" type="text" />}
206
- end
207
-
208
- assert_equal expected, output_buffer
209
- end
210
-
211
- def test_nested_fields_for_dont_overwrite_validation_with_inheritance
212
- form_for(@post, :validate => true) do |f|
213
- concat f.fields_for(:comment, @comment, :validate => false) { |c|
214
- concat c.text_field(:title)
215
- }
216
- end
217
-
218
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => {}) do
219
- %{<input id="post_comment_title" name="post[comment][title]" size="30" type="text" />}
220
- end
221
-
222
- assert_equal expected, output_buffer
223
- end
224
-
225
- def test_with_custom_id_for_form
226
- form_for(@post, :validate => true, :html => { :id => 'some_form' }) do |f|
227
- concat f.text_field(:cost)
228
- end
229
-
230
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
231
- expected = whole_form("/posts/123", "some_form", "edit_post", :method => "put", :validators => validators) do
232
- %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />}
233
- end
234
- assert_equal expected, output_buffer
235
- end
236
-
237
- def test_ignore_an_individual_validator
238
- hash = {
239
- :cost => {
240
- :presence => {
241
- :message => "can't be blank"
242
- },
243
- :format => {
244
- :with => /.+/,
245
- :message => "is invalid"
246
- }
247
- }
248
- }
249
- @post.stubs(:client_side_validation_hash).returns(hash)
250
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
251
- form_for(@post, :validate => true) do |f|
252
- concat f.text_field(:cost, :validate => { :format => false })
253
- end
254
-
255
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
256
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
257
- %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />}
258
- end
259
- assert_equal expected, output_buffer
260
- end
261
-
262
- def test_ignore_many_validators
263
- hash = {
264
- :cost => {
265
- :presence => {
266
- :message => "can't be blank"
267
- },
268
- :format => {
269
- :with => /.+/,
270
- :message => "is invalid"
271
- }
272
- }
273
- }
274
- @post.stubs(:client_side_validation_hash).returns(hash)
275
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
276
- form_for(@post, :validate => true) do |f|
277
- concat f.text_field(:cost, :validate => { :presence => false, :format => false })
278
- end
279
-
280
- validators = {}
281
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
282
- %{<input id="post_cost" name="post[cost]" size="30" type="text" />}
283
- end
284
- assert_equal expected, output_buffer
285
- end
286
-
287
- def test_select
288
- form_for(@post, :validate => true) do |f|
289
- concat f.select(:cost, [])
290
- end
291
-
292
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
293
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
294
- %{<select data-validate="true" id="post_cost" name="post[cost]"></select>}
295
- end
296
- assert_equal expected, output_buffer
297
- end
298
-
299
- def test_select_multiple
300
- form_for(@post, :validate => true) do |f|
301
- concat f.select(:cost, [], {}, :multiple => true)
302
- end
303
-
304
- validators = {'post[cost][]' => {:presence => {:message => "can't be blank"}}}
305
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
306
- %{<select data-validate="true" id="post_cost" multiple="multiple" name="post[cost][]"></select>}
307
- end
308
- assert_equal expected, output_buffer
309
- end
310
-
311
- def test_collection_select
312
- form_for(@post, :validate => true) do |f|
313
- concat f.collection_select(:cost, [], :id, :name)
314
- end
315
-
316
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
317
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
318
- %{<select data-validate="true" id="post_cost" name="post[cost]"></select>}
319
- end
320
- assert_equal expected, output_buffer
321
- end
322
-
323
- def test_grouped_collection_select
324
- form_for(@post, :validate => true) do |f|
325
- concat f.grouped_collection_select(:cost, [], :group_method, :group_label_method, :id, :name)
326
- end
327
-
328
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
329
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
330
- %{<select data-validate="true" id="post_cost" name="post[cost]"></select>}
331
- end
332
- assert_equal expected, output_buffer
333
- end
334
-
335
- def test_time_zone_select
336
- zones = mock('TimeZones')
337
- zones.stubs(:all).returns([])
338
- form_for(@post, :validate => true) do |f|
339
- concat f.time_zone_select(:cost, nil, :model => zones)
340
- end
341
-
342
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
343
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
344
- %{<select data-validate="true" id="post_cost" name="post[cost]"></select>}
345
- end
346
- assert_equal expected, output_buffer
347
- end
348
-
349
- def test_conditional_validators_should_be_filtered
350
- hash = {
351
- :cost => {
352
- :presence => {
353
- :message => "can't be blank",
354
- :unless => :cannot_validate?
355
- }
356
- },
357
- :title => {
358
- :presence => {
359
- :message => "can't be blank",
360
- :if => :can_validate?
361
- }
362
- }
363
- }
364
-
365
- @post.title = nil
366
- @post.stubs(:cannot_validate?).returns(false)
367
- @post.stubs(:can_validate?).returns(true)
368
- @post.stubs(:client_side_validation_hash).returns(hash)
369
- form_for(@post, :validate => true) do |f|
370
- concat f.text_field(:cost)
371
- concat f.text_field(:title)
372
- end
373
-
374
- validators = {}
375
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
376
- %{<input id="post_cost" name="post[cost]" size="30" type="text" />} +
377
- %{<input id="post_title" name="post[title]" size="30" type="text" />}
378
- end
379
- assert_equal expected, output_buffer
380
- end
381
-
382
- def test_conditional_validator_filters_being_forced
383
- hash = {
384
- :cost => {
385
- :presence => {
386
- :message => "can't be blank",
387
- :unless => :cannot_validate?
388
- }
389
- },
390
- :title => {
391
- :presence => {
392
- :message => "can't be blank",
393
- :if => :can_validate?
394
- }
395
- }
396
- }
397
-
398
- @post.title = nil
399
- @post.stubs(:cannot_validate?).returns(false)
400
- @post.stubs(:can_validate?).returns(true)
401
- @post.stubs(:client_side_validation_hash).returns(hash)
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)
441
- end
442
-
443
- validators = {}
444
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
445
- %{<input id="post_cost" name="post[cost]" size="30" type="text" />} +
446
- %{<input id="post_title" name="post[title]" size="30" type="text" />}
447
- end
448
- assert_equal expected, output_buffer
449
- end
450
-
451
- def test_conditional_validator_filters_being_forced_individually
452
- hash = {
453
- :cost => {
454
- :presence => {
455
- :message => "can't be blank",
456
- :unless => :cannot_validate?
457
- }
458
- },
459
- :title => {
460
- :presence => {
461
- :message => "can't be blank",
462
- :if => :can_validate?
463
- }
464
- }
465
- }
466
-
467
- @post.title = nil
468
- @post.stubs(:cannot_validate?).returns(false)
469
- @post.stubs(:can_validate?).returns(true)
470
- @post.stubs(:client_side_validation_hash).returns(hash)
471
- 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 })
474
- end
475
-
476
- validators = {
477
- 'post[cost]' => {:presence => {:message => "can't be blank"}},
478
- 'post[title]' => {:presence => {:message => "can't be blank"}}
479
- }
480
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
481
- %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />} +
482
- %{<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" />}
483
- end
484
- assert_equal expected, output_buffer
485
- end
486
-
487
- def test_conditional_validator_filters_being_forced_and_not_meeting_condition_individually
488
- hash = {
489
- :cost => {
490
- :presence => {
491
- :message => "can't be blank",
492
- :unless => :cannot_validate?
493
- }
494
- },
495
- :title => {
496
- :presence => {
497
- :message => "can't be blank",
498
- :if => :can_validate?
499
- }
500
- }
501
- }
502
-
503
- @post.title = nil
504
- @post.stubs(:cannot_validate?).returns(true)
505
- @post.stubs(:can_validate?).returns(false)
506
- @post.stubs(:client_side_validation_hash).returns(hash)
507
- 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 })
510
- end
511
-
512
- validators = {}
513
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
514
- %{<input id="post_cost" name="post[cost]" size="30" type="text" />} +
515
- %{<input id="post_title" name="post[title]" size="30" type="text" />}
516
- end
517
- assert_equal expected, output_buffer
518
- end
519
-
520
- def test_conditional_validator_filters_being_forced_with_procs
521
- hash = {
522
- :cost => {
523
- :presence => {
524
- :message => "can't be blank",
525
- :unless => Proc.new { |post| post.cannot_validate? }
526
- }
527
- },
528
- :title => {
529
- :presence => {
530
- :message => "can't be blank",
531
- :if => Proc.new { |post| post.can_validate? }
532
- }
533
- }
534
- }
535
-
536
- @post.title = nil
537
- @post.stubs(:cannot_validate?).returns(false)
538
- @post.stubs(:can_validate?).returns(true)
539
- @post.stubs(:client_side_validation_hash).returns(hash)
540
- form_for(@post, :validate => true) do |f|
541
- concat f.text_field(:cost, :validate => true)
542
- concat f.text_field(:title, :validate => true)
543
- end
544
-
545
- validators = {
546
- 'post[cost]' => {:presence => {:message => "can't be blank"}},
547
- 'post[title]' => {:presence => {:message => "can't be blank"}}
548
- }
549
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
550
- %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />} +
551
- %{<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" />}
552
- end
553
- assert_equal expected, output_buffer
554
- end
555
-
556
- def test_conditional_validator_ignored_when_using_changed_helpers
557
- hash = {
558
- :cost => {
559
- :presence => {
560
- :message => "can't be blank",
561
- :unless => :cost_changed?
562
- }
563
- },
564
- :title => {
565
- :presence => {
566
- :message => "can't be blank",
567
- :if => :title_changed?
568
- }
569
- }
570
- }
571
-
572
- @post.title = nil
573
- @post.stubs(:client_side_validation_hash).returns(hash)
574
- form_for(@post, :validate => true) do |f|
575
- concat f.text_field(:cost)
576
- concat f.text_field(:title)
577
- end
578
-
579
- validators = {
580
- 'post[cost]' => {:presence => {:message => "can't be blank"}},
581
- 'post[title]' => {:presence => {:message => "can't be blank"}}
582
- }
583
- 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" />}
586
- end
587
- assert_equal expected, output_buffer
588
- end
589
-
590
- def test_conditional_validator_ignored_when_using_changed_helpers_and_forcing_validators
591
- hash = {
592
- :cost => {
593
- :presence => {
594
- :message => "can't be blank",
595
- :unless => :cost_changed?
596
- }
597
- },
598
- :title => {
599
- :presence => {
600
- :message => "can't be blank",
601
- :if => :title_changed?
602
- }
603
- }
604
- }
605
-
606
- @post.title = nil
607
- @post.stubs(:client_side_validation_hash).returns(hash)
608
- form_for(@post, :validate => true) do |f|
609
- concat f.text_field(:cost, :validate => true)
610
- concat f.text_field(:title, :validate => true)
611
- end
612
-
613
- validators = {
614
- 'post[cost]' => {:presence => {:message => "can't be blank"}},
615
- 'post[title]' => {:presence => {:message => "can't be blank"}}
616
- }
617
- expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
618
- %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />} +
619
- %{<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" />}
620
- end
621
- assert_equal expected, output_buffer
622
- end
623
-
624
- def test_pushing_script_to_content_for
625
- form_for(@post, :validate => :post) do |f|
626
- concat f.text_field(:cost)
627
- end
628
-
629
- validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
630
- expected = %{<form accept-charset="UTF-8" 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="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" /></div><input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" /></form>}
631
- assert_equal expected, output_buffer
632
- assert_equal build_script_tag(nil, "edit_post_123", validators), content_for(:post)
633
- end
634
- end
635
-