nimboids-client_side_validations 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/client_side_validations.gemspec +45 -0
  2. data/javascript/rails.validations.js +393 -0
  3. data/lib/client_side_validations.rb +8 -0
  4. data/lib/client_side_validations/action_view.rb +13 -0
  5. data/lib/client_side_validations/action_view/form_builder.rb +64 -0
  6. data/lib/client_side_validations/action_view/form_helper.rb +69 -0
  7. data/lib/client_side_validations/action_view/form_tag_helper.rb +12 -0
  8. data/lib/client_side_validations/active_model.rb +52 -0
  9. data/lib/client_side_validations/active_model/acceptance.rb +10 -0
  10. data/lib/client_side_validations/active_model/exclusion.rb +15 -0
  11. data/lib/client_side_validations/active_model/format.rb +10 -0
  12. data/lib/client_side_validations/active_model/inclusion.rb +15 -0
  13. data/lib/client_side_validations/active_model/length.rb +22 -0
  14. data/lib/client_side_validations/active_model/numericality.rb +26 -0
  15. data/lib/client_side_validations/active_model/presence.rb +10 -0
  16. data/lib/client_side_validations/active_record.rb +11 -0
  17. data/lib/client_side_validations/active_record/middleware.rb +25 -0
  18. data/lib/client_side_validations/active_record/uniqueness.rb +26 -0
  19. data/lib/client_side_validations/core_ext.rb +3 -0
  20. data/lib/client_side_validations/core_ext/range.rb +10 -0
  21. data/lib/client_side_validations/core_ext/regexp.rb +14 -0
  22. data/lib/client_side_validations/formtastic.rb +21 -0
  23. data/lib/client_side_validations/middleware.rb +83 -0
  24. data/lib/client_side_validations/mongoid.rb +9 -0
  25. data/lib/client_side_validations/mongoid/middleware.rb +20 -0
  26. data/lib/client_side_validations/mongoid/uniqueness.rb +26 -0
  27. data/lib/client_side_validations/simple_form.rb +24 -0
  28. data/lib/client_side_validations/version.rb +3 -0
  29. data/lib/generators/client_side_validations/install_generator.rb +22 -0
  30. data/lib/generators/templates/README +7 -0
  31. data/lib/generators/templates/client_side_validations.rb +14 -0
  32. data/test/action_view/cases/helper.rb +152 -0
  33. data/test/action_view/cases/test_helpers.rb +237 -0
  34. data/test/action_view/cases/test_legacy_helpers.rb +150 -0
  35. data/test/action_view/models.rb +3 -0
  36. data/test/action_view/models/comment.rb +35 -0
  37. data/test/action_view/models/post.rb +35 -0
  38. data/test/active_model/cases/helper.rb +4 -0
  39. data/test/active_model/cases/test_acceptance_validator.rb +16 -0
  40. data/test/active_model/cases/test_base.rb +11 -0
  41. data/test/active_model/cases/test_confirmation_validator.rb +16 -0
  42. data/test/active_model/cases/test_exclusion_validator.rb +20 -0
  43. data/test/active_model/cases/test_format_validator.rb +21 -0
  44. data/test/active_model/cases/test_inclusion_validator.rb +21 -0
  45. data/test/active_model/cases/test_length_validator.rb +61 -0
  46. data/test/active_model/cases/test_numericality_validator.rb +46 -0
  47. data/test/active_model/cases/test_presence_validator.rb +16 -0
  48. data/test/active_model/cases/test_validations.rb +151 -0
  49. data/test/active_model/models/person.rb +17 -0
  50. data/test/active_record/cases/helper.rb +12 -0
  51. data/test/active_record/cases/test_base.rb +11 -0
  52. data/test/active_record/cases/test_middleware.rb +150 -0
  53. data/test/active_record/cases/test_uniqueness_validator.rb +45 -0
  54. data/test/active_record/models/guid.rb +7 -0
  55. data/test/active_record/models/user.rb +10 -0
  56. data/test/base_helper.rb +6 -0
  57. data/test/core_ext/cases/test_core_ext.rb +45 -0
  58. data/test/formtastic/cases/helper.rb +2 -0
  59. data/test/formtastic/cases/test_form_builder.rb +11 -0
  60. data/test/formtastic/cases/test_form_helper.rb +22 -0
  61. data/test/generators/cases/test_install_generator.rb +15 -0
  62. data/test/javascript/config.ru +3 -0
  63. data/test/javascript/public/test/callbacks/elementAfter.js +54 -0
  64. data/test/javascript/public/test/callbacks/elementBefore.js +54 -0
  65. data/test/javascript/public/test/callbacks/elementFail.js +70 -0
  66. data/test/javascript/public/test/callbacks/elementPass.js +70 -0
  67. data/test/javascript/public/test/callbacks/formAfter.js +45 -0
  68. data/test/javascript/public/test/callbacks/formBefore.js +45 -0
  69. data/test/javascript/public/test/callbacks/formFail.js +51 -0
  70. data/test/javascript/public/test/callbacks/formPass.js +50 -0
  71. data/test/javascript/public/test/form_builders/validateForm.js +66 -0
  72. data/test/javascript/public/test/form_builders/validateFormtastic.js +54 -0
  73. data/test/javascript/public/test/form_builders/validateSimpleForm.js +57 -0
  74. data/test/javascript/public/test/settings.js +15 -0
  75. data/test/javascript/public/test/validateElement.js +144 -0
  76. data/test/javascript/public/test/validators/acceptance.js +42 -0
  77. data/test/javascript/public/test/validators/confirmation.js +25 -0
  78. data/test/javascript/public/test/validators/exclusion.js +41 -0
  79. data/test/javascript/public/test/validators/format.js +27 -0
  80. data/test/javascript/public/test/validators/inclusion.js +42 -0
  81. data/test/javascript/public/test/validators/length.js +70 -0
  82. data/test/javascript/public/test/validators/numericality.js +140 -0
  83. data/test/javascript/public/test/validators/presence.js +15 -0
  84. data/test/javascript/public/test/validators/uniqueness.js +89 -0
  85. data/test/javascript/public/vendor/jquery.metadata.js +122 -0
  86. data/test/javascript/public/vendor/qunit.css +196 -0
  87. data/test/javascript/public/vendor/qunit.js +1374 -0
  88. data/test/javascript/server.rb +78 -0
  89. data/test/javascript/views/index.erb +20 -0
  90. data/test/javascript/views/layout.erb +21 -0
  91. data/test/middleware/cases/helper.rb +15 -0
  92. data/test/middleware/cases/test_middleware.rb +8 -0
  93. data/test/mongoid/cases/helper.rb +16 -0
  94. data/test/mongoid/cases/test_base.rb +15 -0
  95. data/test/mongoid/cases/test_middleware.rb +68 -0
  96. data/test/mongoid/cases/test_uniqueness_validator.rb +44 -0
  97. data/test/mongoid/models/book.rb +8 -0
  98. data/test/simple_form/cases/helper.rb +2 -0
  99. data/test/simple_form/cases/test_form_builder.rb +14 -0
  100. data/test/simple_form/cases/test_form_helper.rb +22 -0
  101. metadata +435 -0
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+
3
+ module ClientSideValidations
4
+
5
+ module Middleware
6
+
7
+ class Validators
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ case env['PATH_INFO']
14
+ when %r{\/validators\/(\w+)\.json}
15
+ "::ClientSideValidations::Middleware::#{$1.camelize}".constantize.new(env).response
16
+ else
17
+ @app.call(env)
18
+ end
19
+ end
20
+ end
21
+
22
+ class Base
23
+ attr_accessor :request, :body, :status
24
+
25
+ def initialize(env)
26
+ self.body = ''
27
+ self.status = 200
28
+ self.request = ActionDispatch::Request.new(env)
29
+ end
30
+
31
+ def response
32
+ [status, {'Content-Type' => content_type, 'Content-Length' => body.length.to_s}, [body]]
33
+ end
34
+
35
+ def content_type
36
+ 'application/json'
37
+ end
38
+ end
39
+
40
+ class Uniqueness < Base
41
+ IGNORE_PARAMS = %w{case_sensitive id scope}
42
+
43
+ def response
44
+ if is_unique?
45
+ self.status = 404
46
+ self.body = 'true'
47
+ else
48
+ self.status = 200
49
+ self.body = 'false'
50
+ end
51
+ super
52
+ end
53
+
54
+ private
55
+
56
+ def is_unique?
57
+ resource = extract_resource
58
+ klass = resource.classify.constantize
59
+ attribute = request.params[resource].keys.first
60
+ value = request.params[resource][attribute]
61
+
62
+ if (defined?(::ActiveRecord::Base) && klass < ::ActiveRecord::Base)
63
+ middleware_klass = ClientSideValidations::ActiveRecord::Middleware
64
+ elsif (defined?(::Mongoid::Document) && klass.included_modules.include?(::Mongoid::Document))
65
+ middleware_klass = ClientSideValidations::Mongoid::Middleware
66
+ end
67
+
68
+ middleware_klass.is_unique?(klass, attribute, value, request.params)
69
+ end
70
+
71
+ def extract_resource
72
+ parent_key = (request.params.keys - IGNORE_PARAMS).first
73
+ end
74
+ end
75
+
76
+ end
77
+
78
+ class Engine < ::Rails::Engine
79
+ config.app_middleware.use ClientSideValidations::Middleware::Validators
80
+ end
81
+
82
+ end
83
+
@@ -0,0 +1,9 @@
1
+ require 'client_side_validations/active_model'
2
+ require 'client_side_validations/mongoid/middleware'
3
+
4
+ %w{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
+
@@ -0,0 +1,20 @@
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
@@ -0,0 +1,26 @@
1
+ module ClientSideValidations::Mongoid
2
+ module Uniqueness
3
+ def client_side_hash(model, attribute)
4
+ extra_options = options.except(*::ActiveModel::Errors::CALLBACKS_OPTIONS - [:on, :allow_blank])
5
+ hash = { :message => model.errors.generate_message(attribute, message_type, extra_options.except(:case_sensitive, :scope)) }
6
+ hash = hash.merge(extra_options).merge(model.new_record? ? {} : { :id => model.id })
7
+
8
+ if hash[:scope].present?
9
+ hash[:scope] = Array.wrap(hash[:scope]).inject({}) do |scope_hash, scope_item|
10
+ scope_hash.merge!(scope_item => model.send(scope_item))
11
+ end
12
+ else
13
+ hash.delete(:scope)
14
+ end
15
+
16
+ hash
17
+ end
18
+
19
+ private
20
+
21
+ def message_type
22
+ :taken
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,24 @@
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
+
@@ -0,0 +1,3 @@
1
+ module ClientSideValidations
2
+ VERSION = "3.0.3"
3
+ end
@@ -0,0 +1,22 @@
1
+ module ClientSideValidations
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../../templates', __FILE__)
5
+
6
+ desc 'Creates a ClientSideValidations initializer and copies client-side-validations.js to public/javascripts.'
7
+
8
+ def copy_initializer
9
+ copy_file 'client_side_validations.rb', 'config/initializers/client_side_validations.rb'
10
+ end
11
+
12
+ def copy_locale
13
+ copy_file '../../../javascript/rails.validations.js', 'public/javascripts/rails.validations.js'
14
+ end
15
+
16
+ def show_readme
17
+ readme 'README' if behavior == :invoke
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,7 @@
1
+ **************************************
2
+ Client Side Validations
3
+
4
+ Currently Client Side Validations only
5
+ works with jQuery. You'll need to require
6
+ jQuery in your layout file before you
7
+ require 'rails.validations.js'
@@ -0,0 +1,14 @@
1
+ # ClientSideValidations Initializer
2
+
3
+ require 'client_side_validations/simple_form' if defined?(::SimpleForm)
4
+ require 'client_side_validations/formtastic' if defined?(::Formtastic)
5
+
6
+ # Uncomment the following block if you want each input field to have the validation messages attached.
7
+ # ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
8
+ # unless html_tag =~ /^<label/
9
+ # %{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
10
+ # else
11
+ # %{<div class="field_with_errors">#{html_tag}</div>}.html_safe
12
+ # end
13
+ # end
14
+
@@ -0,0 +1,152 @@
1
+ require 'base_helper'
2
+ require 'action_view'
3
+ require 'client_side_validations/action_view'
4
+ require 'action_view/template/handlers/erb'
5
+ require 'action_view/models'
6
+
7
+ module ActionViewTestSetup
8
+ include ::ClientSideValidations::ActionView::Helpers::FormHelper
9
+ include ::ClientSideValidations::ActionView::Helpers::FormTagHelper
10
+
11
+ def form_for(*)
12
+ @output_buffer = super
13
+ end
14
+
15
+ def setup
16
+ super
17
+
18
+ # Create "label" locale for testing I18n label helpers
19
+ I18n.backend.store_translations 'label', {
20
+ :activemodel => {
21
+ :attributes => {
22
+ :post => {
23
+ :cost => "Total cost"
24
+ }
25
+ }
26
+ },
27
+ :helpers => {
28
+ :label => {
29
+ :post => {
30
+ :body => "Write entire text here"
31
+ }
32
+ }
33
+ }
34
+ }
35
+
36
+ # Create "submit" locale for testing I18n submit helpers
37
+ I18n.backend.store_translations 'submit', {
38
+ :helpers => {
39
+ :submit => {
40
+ :create => 'Create %{model}',
41
+ :update => 'Confirm %{model} changes',
42
+ :submit => 'Save changes',
43
+ :another_post => {
44
+ :update => 'Update your %{model}'
45
+ }
46
+ }
47
+ }
48
+ }
49
+
50
+ @post = Post.new
51
+ @comment = Comment.new
52
+ def @post.errors()
53
+ Class.new{
54
+ def [](field); field == "author_name" ? ["can't be empty"] : [] end
55
+ def empty?() false end
56
+ def count() 1 end
57
+ def full_messages() [ "Author name can't be empty" ] end
58
+ }.new
59
+ end
60
+ def @post.id; 123; end
61
+ def @post.id_before_type_cast; 123; end
62
+ def @post.to_param; '123'; end
63
+
64
+ @post.persisted = true
65
+ @post.title = "Hello World"
66
+ @post.author_name = ""
67
+ @post.body = "Back to the hill and over it again!"
68
+ @post.secret = 1
69
+ @post.written_on = Date.new(2004, 6, 15)
70
+ end
71
+
72
+ def url_for(object)
73
+ @url_for_options = object
74
+ if object.is_a?(Hash)
75
+ "http://www.example.com"
76
+ else
77
+ super
78
+ end
79
+ end
80
+
81
+ def snowman(method = nil)
82
+ txt = %{<div style="margin:0;padding:0;display:inline">}
83
+ txt << %{<input name="utf8" type="hidden" value="&#x2713;" />}
84
+ txt << %{<input name="_method" type="hidden" value="#{method}" />} if method
85
+ txt << %{</div>}
86
+ end
87
+
88
+ def form_text(action = "http://www.example.com", id = nil, html_class = nil, remote = nil, validators = nil)
89
+ txt = %{<form accept-charset="UTF-8" action="#{action}"}
90
+ txt << %{ data-remote="true"} if remote
91
+ txt << %{ class="#{html_class}"} if html_class
92
+ txt << %{ data-validate="true"} if validators
93
+ txt << %{ id="#{id}"} if id
94
+ txt << %{ method="post">}
95
+ end
96
+
97
+ def whole_form(action = "http://www.example.com", id = nil, html_class = nil, options = nil)
98
+ contents = block_given? ? yield : ""
99
+
100
+ if options.is_a?(Hash)
101
+ method, remote, validators = options.values_at(:method, :remote, :validators)
102
+ else
103
+ method = options
104
+ end
105
+
106
+ html = form_text(action, id, html_class, remote, validators) + snowman(method) + (contents || "") + "</form>"
107
+
108
+ if options.is_a?(Hash) && options[:validators]
109
+ html + %Q{<script>var #{id} = #{client_side_form_settings_helper.merge(:validators => validators).to_json};</script>}
110
+ else
111
+ html
112
+ end
113
+ end
114
+
115
+ protected
116
+ def comments_path(post)
117
+ "/posts/#{post.id}/comments"
118
+ end
119
+ alias_method :post_comments_path, :comments_path
120
+
121
+ def comment_path(post, comment)
122
+ "/posts/#{post.id}/comments/#{comment.id}"
123
+ end
124
+ alias_method :post_comment_path, :comment_path
125
+
126
+ def admin_comments_path(post)
127
+ "/admin/posts/#{post.id}/comments"
128
+ end
129
+ alias_method :admin_post_comments_path, :admin_comments_path
130
+
131
+ def admin_comment_path(post, comment)
132
+ "/admin/posts/#{post.id}/comments/#{comment.id}"
133
+ end
134
+ alias_method :admin_post_comment_path, :admin_comment_path
135
+
136
+ def posts_path
137
+ "/posts"
138
+ end
139
+
140
+ def post_path(post, options = {})
141
+ if options[:format]
142
+ "/posts/#{post.id}.#{options[:format]}"
143
+ else
144
+ "/posts/#{post.id}"
145
+ end
146
+ end
147
+
148
+ def protect_against_forgery?
149
+ false
150
+ end
151
+
152
+ end
@@ -0,0 +1,237 @@
1
+ require 'action_view/cases/helper'
2
+
3
+ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
4
+ include ActionViewTestSetup
5
+
6
+ cattr_accessor :field_error_proc
7
+ @@field_error_proc = Proc.new { |html_tag, instance| html_tag }
8
+
9
+ def client_side_form_settings_helper
10
+ {
11
+ :type => "ActionView::Helpers::FormBuilder",
12
+ :input_tag => %{<span id="input_tag" />},
13
+ :label_tag => %{<label id="label_tag" />}
14
+ }
15
+ end
16
+
17
+ def test_text_field
18
+ form_for(@post, :validate => true) do |f|
19
+ concat f.text_field(:cost)
20
+ end
21
+
22
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
23
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
24
+ %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />}
25
+ end
26
+ assert_equal expected, output_buffer
27
+ end
28
+
29
+ def test_password_field
30
+ form_for(@post, :validate => true) do |f|
31
+ concat f.password_field(:cost)
32
+ end
33
+
34
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
35
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
36
+ %{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="password" />}
37
+ end
38
+ assert_equal expected, output_buffer
39
+ end
40
+
41
+ def test_file_field
42
+ form_for(@post, :validate => true) do |f|
43
+ concat f.file_field(:cost)
44
+ end
45
+
46
+ validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
47
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
48
+ %{<input data-validate="true" id="post_cost" name="post[cost]" 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
+ end
237
+