af-client_side_validations 3.1.4.af1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/client_side_validations.gemspec +34 -0
- data/lib/client_side_validations/action_view/form_builder.rb +160 -0
- data/lib/client_side_validations/action_view/form_helper.rb +92 -0
- data/lib/client_side_validations/action_view/form_tag_helper.rb +12 -0
- data/lib/client_side_validations/action_view.rb +14 -0
- data/lib/client_side_validations/active_model/acceptance.rb +10 -0
- data/lib/client_side_validations/active_model/exclusion.rb +15 -0
- data/lib/client_side_validations/active_model/format.rb +10 -0
- data/lib/client_side_validations/active_model/inclusion.rb +15 -0
- data/lib/client_side_validations/active_model/length.rb +24 -0
- data/lib/client_side_validations/active_model/numericality.rb +31 -0
- data/lib/client_side_validations/active_model/presence.rb +10 -0
- data/lib/client_side_validations/active_model.rb +60 -0
- data/lib/client_side_validations/active_record/middleware.rb +33 -0
- data/lib/client_side_validations/active_record/uniqueness.rb +28 -0
- data/lib/client_side_validations/active_record.rb +11 -0
- data/lib/client_side_validations/core_ext/range.rb +10 -0
- data/lib/client_side_validations/core_ext/regexp.rb +14 -0
- data/lib/client_side_validations/core_ext.rb +3 -0
- data/lib/client_side_validations/engine.rb +6 -0
- data/lib/client_side_validations/files.rb +8 -0
- data/lib/client_side_validations/formtastic.rb +21 -0
- data/lib/client_side_validations/middleware.rb +81 -0
- data/lib/client_side_validations/mongo_mapper/middleware.rb +20 -0
- data/lib/client_side_validations/mongo_mapper/uniqueness.rb +28 -0
- data/lib/client_side_validations/mongo_mapper.rb +9 -0
- data/lib/client_side_validations/mongoid/middleware.rb +20 -0
- data/lib/client_side_validations/mongoid/uniqueness.rb +28 -0
- data/lib/client_side_validations/mongoid.rb +9 -0
- data/lib/client_side_validations/simple_form.rb +24 -0
- data/lib/client_side_validations/version.rb +3 -0
- data/lib/client_side_validations.rb +13 -0
- data/lib/generators/client_side_validations/copy_asset_generator.rb +36 -0
- data/lib/generators/client_side_validations/install_generator.rb +45 -0
- data/lib/generators/templates/client_side_validations/README.rails.3.0 +6 -0
- data/lib/generators/templates/client_side_validations/README.rails.3.1 +7 -0
- data/lib/generators/templates/client_side_validations/initializer.rb +14 -0
- data/test/action_view/cases/helper.rb +176 -0
- data/test/action_view/cases/test_helpers.rb +666 -0
- data/test/action_view/cases/test_legacy_helpers.rb +217 -0
- data/test/action_view/models/comment.rb +35 -0
- data/test/action_view/models/post.rb +35 -0
- data/test/action_view/models.rb +3 -0
- data/test/active_model/cases/helper.rb +4 -0
- data/test/active_model/cases/test_acceptance_validator.rb +16 -0
- data/test/active_model/cases/test_base.rb +11 -0
- data/test/active_model/cases/test_confirmation_validator.rb +16 -0
- data/test/active_model/cases/test_exclusion_validator.rb +20 -0
- data/test/active_model/cases/test_format_validator.rb +21 -0
- data/test/active_model/cases/test_inclusion_validator.rb +21 -0
- data/test/active_model/cases/test_length_validator.rb +61 -0
- data/test/active_model/cases/test_numericality_validator.rb +46 -0
- data/test/active_model/cases/test_presence_validator.rb +16 -0
- data/test/active_model/cases/test_validations.rb +175 -0
- data/test/active_model/models/person.rb +17 -0
- data/test/active_record/cases/helper.rb +12 -0
- data/test/active_record/cases/test_base.rb +11 -0
- data/test/active_record/cases/test_middleware.rb +175 -0
- data/test/active_record/cases/test_uniqueness_validator.rb +50 -0
- data/test/active_record/models/guid.rb +7 -0
- data/test/active_record/models/user.rb +14 -0
- data/test/base_helper.rb +7 -0
- data/test/core_ext/cases/test_core_ext.rb +46 -0
- data/test/formtastic/cases/helper.rb +7 -0
- data/test/formtastic/cases/test_form_builder.rb +11 -0
- data/test/formtastic/cases/test_form_helper.rb +21 -0
- data/test/generators/cases/test_generators.rb +70 -0
- data/test/javascript/config.ru +3 -0
- data/test/javascript/public/test/callbacks/elementAfter.js +54 -0
- data/test/javascript/public/test/callbacks/elementBefore.js +54 -0
- data/test/javascript/public/test/callbacks/elementFail.js +70 -0
- data/test/javascript/public/test/callbacks/elementPass.js +70 -0
- data/test/javascript/public/test/callbacks/formAfter.js +45 -0
- data/test/javascript/public/test/callbacks/formBefore.js +45 -0
- data/test/javascript/public/test/callbacks/formFail.js +51 -0
- data/test/javascript/public/test/callbacks/formPass.js +50 -0
- data/test/javascript/public/test/form_builders/validateForm.js +66 -0
- data/test/javascript/public/test/form_builders/validateFormtastic.js +54 -0
- data/test/javascript/public/test/form_builders/validateNestedForm.js +66 -0
- data/test/javascript/public/test/form_builders/validateSimpleForm.js +57 -0
- data/test/javascript/public/test/settings.js +15 -0
- data/test/javascript/public/test/validateElement.js +209 -0
- data/test/javascript/public/test/validators/acceptance.js +42 -0
- data/test/javascript/public/test/validators/confirmation.js +25 -0
- data/test/javascript/public/test/validators/exclusion.js +41 -0
- data/test/javascript/public/test/validators/format.js +27 -0
- data/test/javascript/public/test/validators/inclusion.js +42 -0
- data/test/javascript/public/test/validators/length.js +76 -0
- data/test/javascript/public/test/validators/numericality.js +158 -0
- data/test/javascript/public/test/validators/presence.js +21 -0
- data/test/javascript/public/test/validators/uniqueness.js +107 -0
- data/test/javascript/public/vendor/jquery.metadata.js +122 -0
- data/test/javascript/public/vendor/qunit.css +196 -0
- data/test/javascript/public/vendor/qunit.js +1374 -0
- data/test/javascript/server.rb +84 -0
- data/test/javascript/views/index.erb +20 -0
- data/test/javascript/views/layout.erb +21 -0
- data/test/middleware/cases/helper.rb +18 -0
- data/test/middleware/cases/test_middleware.rb +8 -0
- data/test/mongo_mapper/cases/helper.rb +9 -0
- data/test/mongo_mapper/cases/test_base.rb +15 -0
- data/test/mongo_mapper/cases/test_middleware.rb +77 -0
- data/test/mongo_mapper/cases/test_uniqueness_validator.rb +50 -0
- data/test/mongo_mapper/models/magazine.rb +11 -0
- data/test/mongoid/cases/helper.rb +16 -0
- data/test/mongoid/cases/test_base.rb +15 -0
- data/test/mongoid/cases/test_middleware.rb +77 -0
- data/test/mongoid/cases/test_uniqueness_validator.rb +49 -0
- data/test/mongoid/models/book.rb +12 -0
- data/test/simple_form/cases/helper.rb +5 -0
- data/test/simple_form/cases/test_form_builder.rb +14 -0
- data/test/simple_form/cases/test_form_helper.rb +24 -0
- data/test/test_loader.rb +6 -0
- data/vendor/assets/javascripts/rails.validations.js +418 -0
- metadata +436 -0
|
@@ -0,0 +1,666 @@
|
|
|
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_with_nested_attributes
|
|
212
|
+
form_for(@post, :validate => true) do |f|
|
|
213
|
+
concat f.fields_for(:comments, [@comment]) { |c|
|
|
214
|
+
concat c.text_field(:title)
|
|
215
|
+
}
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
validators = {'post[comments_attributes][][title]' => {:presence => {:message => "can't be blank"}}}
|
|
219
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
220
|
+
%{<input data-validate="true" id="post_comments_attributes_0_title" name="post[comments_attributes][0][title]" size="30" type="text" />}
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
assert_equal expected, output_buffer
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def test_nested_fields_for_with_nested_attributes_with_child_index
|
|
227
|
+
form_for(@post, :validate => true) do |f|
|
|
228
|
+
concat f.fields_for(:comments, [Comment.new], :child_index => '__INDEX__') { |c|
|
|
229
|
+
concat c.text_field(:title)
|
|
230
|
+
}
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
validators = {'post[comments_attributes][][title]' => {:presence => {:message => "can't be blank"}}}
|
|
234
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
235
|
+
%{<input data-validate="true" id="post_comments_attributes___INDEX___title" name="post[comments_attributes][__INDEX__][title]" size="30" type="text" />}
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
assert_equal expected, output_buffer
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def test_nested_fields_for_dont_overwrite_validation_with_inheritance
|
|
243
|
+
form_for(@post, :validate => true) do |f|
|
|
244
|
+
concat f.fields_for(:comment, @comment, :validate => false) { |c|
|
|
245
|
+
concat c.text_field(:title)
|
|
246
|
+
}
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => {}) do
|
|
250
|
+
%{<input id="post_comment_title" name="post[comment][title]" size="30" type="text" />}
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
assert_equal expected, output_buffer
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
def test_with_custom_id_for_form
|
|
257
|
+
form_for(@post, :validate => true, :html => { :id => 'some_form' }) do |f|
|
|
258
|
+
concat f.text_field(:cost)
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
|
|
262
|
+
expected = whole_form("/posts/123", "some_form", "edit_post", :method => "put", :validators => validators) do
|
|
263
|
+
%{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />}
|
|
264
|
+
end
|
|
265
|
+
assert_equal expected, output_buffer
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def test_ignore_an_individual_validator
|
|
269
|
+
hash = {
|
|
270
|
+
:cost => {
|
|
271
|
+
:presence => {
|
|
272
|
+
:message => "can't be blank"
|
|
273
|
+
},
|
|
274
|
+
:format => {
|
|
275
|
+
:with => /.+/,
|
|
276
|
+
:message => "is invalid"
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
@post.stubs(:client_side_validation_hash).returns(hash)
|
|
281
|
+
validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
|
|
282
|
+
form_for(@post, :validate => true) do |f|
|
|
283
|
+
concat f.text_field(:cost, :validate => { :format => false })
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
|
|
287
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
288
|
+
%{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />}
|
|
289
|
+
end
|
|
290
|
+
assert_equal expected, output_buffer
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
def test_ignore_many_validators
|
|
294
|
+
hash = {
|
|
295
|
+
:cost => {
|
|
296
|
+
:presence => {
|
|
297
|
+
:message => "can't be blank"
|
|
298
|
+
},
|
|
299
|
+
:format => {
|
|
300
|
+
:with => /.+/,
|
|
301
|
+
:message => "is invalid"
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
@post.stubs(:client_side_validation_hash).returns(hash)
|
|
306
|
+
validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
|
|
307
|
+
form_for(@post, :validate => true) do |f|
|
|
308
|
+
concat f.text_field(:cost, :validate => { :presence => false, :format => false })
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
validators = {}
|
|
312
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
313
|
+
%{<input id="post_cost" name="post[cost]" size="30" type="text" />}
|
|
314
|
+
end
|
|
315
|
+
assert_equal expected, output_buffer
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def test_select
|
|
319
|
+
form_for(@post, :validate => true) do |f|
|
|
320
|
+
concat f.select(:cost, [])
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
|
|
324
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
325
|
+
%{<select data-validate="true" id="post_cost" name="post[cost]"></select>}
|
|
326
|
+
end
|
|
327
|
+
assert_equal expected, output_buffer
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
def test_select_multiple
|
|
331
|
+
form_for(@post, :validate => true) do |f|
|
|
332
|
+
concat f.select(:cost, [], {}, :multiple => true)
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
validators = {'post[cost][]' => {:presence => {:message => "can't be blank"}}}
|
|
336
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
337
|
+
%{<select data-validate="true" id="post_cost" multiple="multiple" name="post[cost][]"></select>}
|
|
338
|
+
end
|
|
339
|
+
assert_equal expected, output_buffer
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
def test_collection_select
|
|
343
|
+
form_for(@post, :validate => true) do |f|
|
|
344
|
+
concat f.collection_select(:cost, [], :id, :name)
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
|
|
348
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
349
|
+
%{<select data-validate="true" id="post_cost" name="post[cost]"></select>}
|
|
350
|
+
end
|
|
351
|
+
assert_equal expected, output_buffer
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def test_grouped_collection_select
|
|
355
|
+
form_for(@post, :validate => true) do |f|
|
|
356
|
+
concat f.grouped_collection_select(:cost, [], :group_method, :group_label_method, :id, :name)
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
|
|
360
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
361
|
+
%{<select data-validate="true" id="post_cost" name="post[cost]"></select>}
|
|
362
|
+
end
|
|
363
|
+
assert_equal expected, output_buffer
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
def test_time_zone_select
|
|
367
|
+
zones = mock('TimeZones')
|
|
368
|
+
zones.stubs(:all).returns([])
|
|
369
|
+
form_for(@post, :validate => true) do |f|
|
|
370
|
+
concat f.time_zone_select(:cost, nil, :model => zones)
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
|
|
374
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
375
|
+
%{<select data-validate="true" id="post_cost" name="post[cost]"></select>}
|
|
376
|
+
end
|
|
377
|
+
assert_equal expected, output_buffer
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
def test_conditional_validators_should_be_filtered
|
|
381
|
+
hash = {
|
|
382
|
+
:cost => {
|
|
383
|
+
:presence => {
|
|
384
|
+
:message => "can't be blank",
|
|
385
|
+
:unless => :cannot_validate?
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
:title => {
|
|
389
|
+
:presence => {
|
|
390
|
+
:message => "can't be blank",
|
|
391
|
+
:if => :can_validate?
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
@post.title = nil
|
|
397
|
+
@post.stubs(:cannot_validate?).returns(false)
|
|
398
|
+
@post.stubs(:can_validate?).returns(true)
|
|
399
|
+
@post.stubs(:client_side_validation_hash).returns(hash)
|
|
400
|
+
form_for(@post, :validate => true) do |f|
|
|
401
|
+
concat f.text_field(:cost)
|
|
402
|
+
concat f.text_field(:title)
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
validators = {}
|
|
406
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
407
|
+
%{<input id="post_cost" name="post[cost]" size="30" type="text" />} +
|
|
408
|
+
%{<input id="post_title" name="post[title]" size="30" type="text" />}
|
|
409
|
+
end
|
|
410
|
+
assert_equal expected, output_buffer
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
def test_conditional_validator_filters_being_forced
|
|
414
|
+
hash = {
|
|
415
|
+
:cost => {
|
|
416
|
+
:presence => {
|
|
417
|
+
:message => "can't be blank",
|
|
418
|
+
:unless => :cannot_validate?
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
:title => {
|
|
422
|
+
:presence => {
|
|
423
|
+
:message => "can't be blank",
|
|
424
|
+
:if => :can_validate?
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
@post.title = nil
|
|
430
|
+
@post.stubs(:cannot_validate?).returns(false)
|
|
431
|
+
@post.stubs(:can_validate?).returns(true)
|
|
432
|
+
@post.stubs(:client_side_validation_hash).returns(hash)
|
|
433
|
+
form_for(@post, :validate => true) do |f|
|
|
434
|
+
concat f.text_field(:cost, :validate => true)
|
|
435
|
+
concat f.text_field(:title, :validate => true)
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
validators = {
|
|
439
|
+
'post[cost]' => {:presence => {:message => "can't be blank"}},
|
|
440
|
+
'post[title]' => {:presence => {:message => "can't be blank"}}
|
|
441
|
+
}
|
|
442
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
443
|
+
%{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />} +
|
|
444
|
+
%{<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" />}
|
|
445
|
+
end
|
|
446
|
+
assert_equal expected, output_buffer
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
def test_conditional_validator_filters_being_forced_and_not_meeting_condition
|
|
450
|
+
hash = {
|
|
451
|
+
:cost => {
|
|
452
|
+
:presence => {
|
|
453
|
+
:message => "can't be blank",
|
|
454
|
+
:unless => :cannot_validate?
|
|
455
|
+
}
|
|
456
|
+
},
|
|
457
|
+
:title => {
|
|
458
|
+
:presence => {
|
|
459
|
+
:message => "can't be blank",
|
|
460
|
+
:if => :can_validate?
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
@post.title = nil
|
|
466
|
+
@post.stubs(:cannot_validate?).returns(true)
|
|
467
|
+
@post.stubs(:can_validate?).returns(false)
|
|
468
|
+
@post.stubs(:client_side_validation_hash).returns(hash)
|
|
469
|
+
form_for(@post, :validate => true) do |f|
|
|
470
|
+
concat f.text_field(:cost, :validate => true)
|
|
471
|
+
concat f.text_field(:title, :validate => true)
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
validators = {}
|
|
475
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
476
|
+
%{<input id="post_cost" name="post[cost]" size="30" type="text" />} +
|
|
477
|
+
%{<input id="post_title" name="post[title]" size="30" type="text" />}
|
|
478
|
+
end
|
|
479
|
+
assert_equal expected, output_buffer
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
def test_conditional_validator_filters_being_forced_individually
|
|
483
|
+
hash = {
|
|
484
|
+
:cost => {
|
|
485
|
+
:presence => {
|
|
486
|
+
:message => "can't be blank",
|
|
487
|
+
:unless => :cannot_validate?
|
|
488
|
+
}
|
|
489
|
+
},
|
|
490
|
+
:title => {
|
|
491
|
+
:presence => {
|
|
492
|
+
:message => "can't be blank",
|
|
493
|
+
:if => :can_validate?
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
@post.title = nil
|
|
499
|
+
@post.stubs(:cannot_validate?).returns(false)
|
|
500
|
+
@post.stubs(:can_validate?).returns(true)
|
|
501
|
+
@post.stubs(:client_side_validation_hash).returns(hash)
|
|
502
|
+
form_for(@post, :validate => true) do |f|
|
|
503
|
+
concat f.text_field(:cost, :validate => { :presence => true })
|
|
504
|
+
concat f.text_field(:title, :validate => { :presence => true })
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
validators = {
|
|
508
|
+
'post[cost]' => {:presence => {:message => "can't be blank"}},
|
|
509
|
+
'post[title]' => {:presence => {:message => "can't be blank"}}
|
|
510
|
+
}
|
|
511
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
512
|
+
%{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />} +
|
|
513
|
+
%{<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" />}
|
|
514
|
+
end
|
|
515
|
+
assert_equal expected, output_buffer
|
|
516
|
+
end
|
|
517
|
+
|
|
518
|
+
def test_conditional_validator_filters_being_forced_and_not_meeting_condition_individually
|
|
519
|
+
hash = {
|
|
520
|
+
:cost => {
|
|
521
|
+
:presence => {
|
|
522
|
+
:message => "can't be blank",
|
|
523
|
+
:unless => :cannot_validate?
|
|
524
|
+
}
|
|
525
|
+
},
|
|
526
|
+
:title => {
|
|
527
|
+
:presence => {
|
|
528
|
+
:message => "can't be blank",
|
|
529
|
+
:if => :can_validate?
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
@post.title = nil
|
|
535
|
+
@post.stubs(:cannot_validate?).returns(true)
|
|
536
|
+
@post.stubs(:can_validate?).returns(false)
|
|
537
|
+
@post.stubs(:client_side_validation_hash).returns(hash)
|
|
538
|
+
form_for(@post, :validate => true) do |f|
|
|
539
|
+
concat f.text_field(:cost, :validate => { :presence => true })
|
|
540
|
+
concat f.text_field(:title, :validate => { :presence => true })
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
validators = {}
|
|
544
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
545
|
+
%{<input id="post_cost" name="post[cost]" size="30" type="text" />} +
|
|
546
|
+
%{<input id="post_title" name="post[title]" size="30" type="text" />}
|
|
547
|
+
end
|
|
548
|
+
assert_equal expected, output_buffer
|
|
549
|
+
end
|
|
550
|
+
|
|
551
|
+
def test_conditional_validator_filters_being_forced_with_procs
|
|
552
|
+
hash = {
|
|
553
|
+
:cost => {
|
|
554
|
+
:presence => {
|
|
555
|
+
:message => "can't be blank",
|
|
556
|
+
:unless => Proc.new { |post| post.cannot_validate? }
|
|
557
|
+
}
|
|
558
|
+
},
|
|
559
|
+
:title => {
|
|
560
|
+
:presence => {
|
|
561
|
+
:message => "can't be blank",
|
|
562
|
+
:if => Proc.new { |post| post.can_validate? }
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
@post.title = nil
|
|
568
|
+
@post.stubs(:cannot_validate?).returns(false)
|
|
569
|
+
@post.stubs(:can_validate?).returns(true)
|
|
570
|
+
@post.stubs(:client_side_validation_hash).returns(hash)
|
|
571
|
+
form_for(@post, :validate => true) do |f|
|
|
572
|
+
concat f.text_field(:cost, :validate => true)
|
|
573
|
+
concat f.text_field(:title, :validate => true)
|
|
574
|
+
end
|
|
575
|
+
|
|
576
|
+
validators = {
|
|
577
|
+
'post[cost]' => {:presence => {:message => "can't be blank"}},
|
|
578
|
+
'post[title]' => {:presence => {:message => "can't be blank"}}
|
|
579
|
+
}
|
|
580
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
581
|
+
%{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />} +
|
|
582
|
+
%{<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" />}
|
|
583
|
+
end
|
|
584
|
+
assert_equal expected, output_buffer
|
|
585
|
+
end
|
|
586
|
+
|
|
587
|
+
def test_conditional_validator_ignored_when_using_changed_helpers
|
|
588
|
+
hash = {
|
|
589
|
+
:cost => {
|
|
590
|
+
:presence => {
|
|
591
|
+
:message => "can't be blank",
|
|
592
|
+
:unless => :cost_changed?
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
:title => {
|
|
596
|
+
:presence => {
|
|
597
|
+
:message => "can't be blank",
|
|
598
|
+
:if => :title_changed?
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
@post.title = nil
|
|
604
|
+
@post.stubs(:client_side_validation_hash).returns(hash)
|
|
605
|
+
form_for(@post, :validate => true) do |f|
|
|
606
|
+
concat f.text_field(:cost)
|
|
607
|
+
concat f.text_field(:title)
|
|
608
|
+
end
|
|
609
|
+
|
|
610
|
+
validators = {
|
|
611
|
+
'post[cost]' => {:presence => {:message => "can't be blank"}},
|
|
612
|
+
'post[title]' => {:presence => {:message => "can't be blank"}}
|
|
613
|
+
}
|
|
614
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
615
|
+
%{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />} +
|
|
616
|
+
%{<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" />}
|
|
617
|
+
end
|
|
618
|
+
assert_equal expected, output_buffer
|
|
619
|
+
end
|
|
620
|
+
|
|
621
|
+
def test_conditional_validator_ignored_when_using_changed_helpers_and_forcing_validators
|
|
622
|
+
hash = {
|
|
623
|
+
:cost => {
|
|
624
|
+
:presence => {
|
|
625
|
+
:message => "can't be blank",
|
|
626
|
+
:unless => :cost_changed?
|
|
627
|
+
}
|
|
628
|
+
},
|
|
629
|
+
:title => {
|
|
630
|
+
:presence => {
|
|
631
|
+
:message => "can't be blank",
|
|
632
|
+
:if => :title_changed?
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
@post.title = nil
|
|
638
|
+
@post.stubs(:client_side_validation_hash).returns(hash)
|
|
639
|
+
form_for(@post, :validate => true) do |f|
|
|
640
|
+
concat f.text_field(:cost, :validate => true)
|
|
641
|
+
concat f.text_field(:title, :validate => true)
|
|
642
|
+
end
|
|
643
|
+
|
|
644
|
+
validators = {
|
|
645
|
+
'post[cost]' => {:presence => {:message => "can't be blank"}},
|
|
646
|
+
'post[title]' => {:presence => {:message => "can't be blank"}}
|
|
647
|
+
}
|
|
648
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
|
649
|
+
%{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />} +
|
|
650
|
+
%{<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" />}
|
|
651
|
+
end
|
|
652
|
+
assert_equal expected, output_buffer
|
|
653
|
+
end
|
|
654
|
+
|
|
655
|
+
def test_pushing_script_to_content_for
|
|
656
|
+
form_for(@post, :validate => :post) do |f|
|
|
657
|
+
concat f.text_field(:cost)
|
|
658
|
+
end
|
|
659
|
+
|
|
660
|
+
validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
|
|
661
|
+
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="✓" /><input name="_method" type="hidden" value="put" /></div><input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" /></form>}
|
|
662
|
+
assert_equal expected, output_buffer
|
|
663
|
+
assert_equal build_script_tag(nil, "edit_post_123", validators), content_for(:post)
|
|
664
|
+
end
|
|
665
|
+
end
|
|
666
|
+
|