validatious-on-rails 0.3.1
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/Rakefile +52 -0
- data/generators/validatious/templates/v2.standalone.full.min.js +7 -0
- data/generators/validatious/templates/validatious.config.js +83 -0
- data/generators/validatious/validatious_generator.rb +12 -0
- data/lib/validatious-on-rails/helpers.rb +20 -0
- data/lib/validatious-on-rails/model_validations.rb +312 -0
- data/lib/validatious-on-rails/rails/action_view_helpers.rb +114 -0
- data/lib/validatious-on-rails/rails.rb +2 -0
- data/lib/validatious-on-rails/validatious/client_side_validator.rb +14 -0
- data/lib/validatious-on-rails/validatious/remote_validator.rb +24 -0
- data/lib/validatious-on-rails/validatious/validator.rb +228 -0
- data/lib/validatious-on-rails/validatious/validators/exclusion_validator.rb +28 -0
- data/lib/validatious-on-rails/validatious/validators/format_validator.rb +33 -0
- data/lib/validatious-on-rails/validatious/validators/inclusion_validator.rb +28 -0
- data/lib/validatious-on-rails/validatious/validators/length/is_validator.rb +27 -0
- data/lib/validatious-on-rails/validatious/validators/length/maximum_validator.rb +27 -0
- data/lib/validatious-on-rails/validatious/validators/length/minimum_validator.rb +27 -0
- data/lib/validatious-on-rails/validatious/validators/numericality/equal_to_validator.rb +25 -0
- data/lib/validatious-on-rails/validatious/validators/numericality/even_validator.rb +24 -0
- data/lib/validatious-on-rails/validatious/validators/numericality/greater_than_or_equal_to_validator.rb +25 -0
- data/lib/validatious-on-rails/validatious/validators/numericality/greater_than_validator.rb +25 -0
- data/lib/validatious-on-rails/validatious/validators/numericality/less_than_or_equal_to_validator.rb +25 -0
- data/lib/validatious-on-rails/validatious/validators/numericality/less_than_validator.rb +25 -0
- data/lib/validatious-on-rails/validatious/validators/numericality/odd_validator.rb +24 -0
- data/lib/validatious-on-rails/validatious/validators/numericality/only_integer_validator.rb +24 -0
- data/lib/validatious-on-rails/validatious/validators.rb +3 -0
- data/lib/validatious-on-rails/validatious.rb +5 -0
- data/lib/validatious-on-rails.rb +41 -0
- data/rails/init.rb +2 -0
- data/test/test_helper.rb +93 -0
- data/test/validatious_on_rails/helpers_test.rb +47 -0
- data/test/validatious_on_rails/model_validations_test.rb +190 -0
- data/test/validatious_on_rails/rails/action_view_helpers_test.rb +168 -0
- data/test/validatious_on_rails/validatious/validator_test.rb +56 -0
- data/test/validatious_on_rails_test.rb +6 -0
- metadata +129 -0
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), *%w[validatious-on-rails validatious])
|
3
|
+
require File.join(File.dirname(__FILE__), *%w[validatious-on-rails model_validations])
|
4
|
+
require File.join(File.dirname(__FILE__), *%w[validatious-on-rails rails])
|
5
|
+
require File.join(File.dirname(__FILE__), *%w[validatious-on-rails helpers])
|
6
|
+
|
7
|
+
module ValidatiousOnRails # :nodoc:
|
8
|
+
|
9
|
+
extend self
|
10
|
+
|
11
|
+
# Standard error: Acts as base error class for the plugin.
|
12
|
+
#
|
13
|
+
class ValidatiousOnRailsError < ::StandardError
|
14
|
+
def initialize(message)
|
15
|
+
::Validatious.log message, :debug
|
16
|
+
super message
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
mattr_accessor :verbose
|
21
|
+
|
22
|
+
@@verbose = ::Object.const_defined?(:RAILS_ENV) ? (::RAILS_ENV.to_sym == :development) : true
|
23
|
+
|
24
|
+
# Logging helper: Internal debug-logging for the plugin.
|
25
|
+
#
|
26
|
+
def log(message, level = :info)
|
27
|
+
return unless @@verbose
|
28
|
+
level = :info if level.blank?
|
29
|
+
@@logger ||= ::Logger.new(::STDOUT)
|
30
|
+
@@logger.send(level.to_sym, message)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Alias method for: ValidatiousOnRails::Helpers#custom_validatious_validators
|
34
|
+
#
|
35
|
+
def custom_validators
|
36
|
+
Helpers.custom_validatious_validators
|
37
|
+
end
|
38
|
+
alias :include_custom_validators :custom_validators
|
39
|
+
::ActionController::Base.helper_method :custom_validators, :include_custom_validators
|
40
|
+
|
41
|
+
end
|
data/rails/init.rb
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file loads up the in-memory database needed to run the tests, and adds a few
|
4
|
+
# convenience methods.
|
5
|
+
#
|
6
|
+
|
7
|
+
begin
|
8
|
+
require File.expand_path(File.join(File.dirname(__FILE__), *%w(.. .. .. .. config environment)))
|
9
|
+
rescue LoadError
|
10
|
+
require 'rubygems'
|
11
|
+
|
12
|
+
gem 'test-unit', '1.2.3'
|
13
|
+
gem 'activerecord', '>= 1.2.3'
|
14
|
+
gem 'actionpack', '>= 1.2.3'
|
15
|
+
gem 'sqlite3-ruby', '>= 1.2.0'
|
16
|
+
|
17
|
+
require 'test/unit'
|
18
|
+
require 'active_record'
|
19
|
+
require 'action_controller'
|
20
|
+
require 'sqlite3'
|
21
|
+
end
|
22
|
+
|
23
|
+
begin
|
24
|
+
require 'context'
|
25
|
+
rescue LoadError
|
26
|
+
gem 'jeremymcanally-context', '>= 0.5.5'
|
27
|
+
require 'context'
|
28
|
+
end
|
29
|
+
|
30
|
+
begin
|
31
|
+
require 'rr'
|
32
|
+
rescue LoadError
|
33
|
+
gem 'rr', '>= 0.0.0'
|
34
|
+
require 'rr'
|
35
|
+
end
|
36
|
+
extend RR::Adapters::RRMethods
|
37
|
+
|
38
|
+
begin
|
39
|
+
require 'acts_as_fu'
|
40
|
+
rescue LoadError
|
41
|
+
gem 'nakajima-acts_as_fu', '>= 0.0.5'
|
42
|
+
require 'acts_as_fu'
|
43
|
+
end
|
44
|
+
|
45
|
+
require 'validatious-on-rails'
|
46
|
+
|
47
|
+
# TODO: Should extend Rails validators with this - to test respond_to.
|
48
|
+
module ActiveRecord
|
49
|
+
module Validations
|
50
|
+
module ClassMethods
|
51
|
+
def validates_craziness_of(*args)
|
52
|
+
#...
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Reflected_validations already freezed...ned to find a workaround.
|
59
|
+
# reflected_validatons = ActiveRecordExtensions::ValidationReflection.reflected_validations
|
60
|
+
# stub(ActiveRecordExtensions::ValidationReflection).reflected_validations {reflected_validatons + [:validates_craziness_of]}
|
61
|
+
|
62
|
+
build_model :bogus_items do
|
63
|
+
string :url
|
64
|
+
string :name
|
65
|
+
string :email
|
66
|
+
|
67
|
+
text :body
|
68
|
+
integer :variant
|
69
|
+
boolean :signed
|
70
|
+
|
71
|
+
validates_presence_of :name, :body, :variant
|
72
|
+
validates_confirmation_of :name
|
73
|
+
validates_acceptance_of :signed
|
74
|
+
validates_format_of :url,
|
75
|
+
:with => /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i,
|
76
|
+
:name => 'url', :message => 'Invalid URL.'
|
77
|
+
validates_inclusion_of :variant, :in => (1..5).to_a
|
78
|
+
validates_exclusion_of :variant, :in => (6..10).to_a
|
79
|
+
|
80
|
+
# TODO: Test: If this is a validator makro, then it should not cause any issues.
|
81
|
+
validates_craziness_of :name
|
82
|
+
end
|
83
|
+
|
84
|
+
#
|
85
|
+
# ValidationReflection seems to expect RAILS_ROOT to be defined, but it's not
|
86
|
+
# if it's tested outside of a Rails project. So, just set it to something random.
|
87
|
+
#
|
88
|
+
RAILS_ROOT = File.join(File.dirname(__FILE__)) unless defined?(RAILS_ROOT)
|
89
|
+
|
90
|
+
#
|
91
|
+
# Log file for testing only.
|
92
|
+
#
|
93
|
+
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), 'debug.log'))
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), *%w(.. test_helper)))
|
3
|
+
|
4
|
+
require 'action_view/test_case'
|
5
|
+
|
6
|
+
class HelpersTest < ::ActionView::TestCase
|
7
|
+
|
8
|
+
include ActionView::Helpers::FormHelper
|
9
|
+
|
10
|
+
attr_accessor :output_buffer
|
11
|
+
|
12
|
+
before do
|
13
|
+
@output_buffer ||= ''
|
14
|
+
end
|
15
|
+
|
16
|
+
context "helpers" do
|
17
|
+
context "custom_validatious_validators" do
|
18
|
+
|
19
|
+
test "should not output custom validators if there are none" do
|
20
|
+
helper_output = ::ValidatiousOnRails::Helpers.custom_validatious_validators
|
21
|
+
assert_equal '', helper_output.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
test "should output custom validators if they exists" do
|
25
|
+
form_for(::BogusItem.new, :url => '/bogus_items') do |f|
|
26
|
+
concat f.text_field(:url)
|
27
|
+
end
|
28
|
+
|
29
|
+
# FIXME: Not sure how to test content_for-helpers...returns nil.
|
30
|
+
concat ::ValidatiousOnRails::Helpers.custom_validatious_validators
|
31
|
+
|
32
|
+
# In parts...
|
33
|
+
# assert_match /<script.+>.*v2.Validator.*<\/script>/, output_buffer
|
34
|
+
# assert_match /<script.*id="custom_validatious_validators".*>/, helper_output
|
35
|
+
# assert_match /<script.*type="text\/javascript".*>/, helper_output
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def protect_against_forgery?
|
44
|
+
false
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), *%w(.. test_helper)))
|
3
|
+
|
4
|
+
require 'active_support/test_case'
|
5
|
+
|
6
|
+
class ModelValidationsTest < ::ActiveSupport::TestCase
|
7
|
+
|
8
|
+
include ActionView::Helpers::FormHelper
|
9
|
+
|
10
|
+
test "acceptance_of" do
|
11
|
+
validation = ::ValidatiousOnRails::ModelValidations.acceptance_of(validation(:validates_acceptance_of))
|
12
|
+
assert_equal 'required', validation[:class]
|
13
|
+
end
|
14
|
+
|
15
|
+
test "associated" do
|
16
|
+
# TODO: not implemented
|
17
|
+
end
|
18
|
+
|
19
|
+
test "confirmation_of" do
|
20
|
+
validation = ::ValidatiousOnRails::ModelValidations.confirmation_of(validation(:validates_confirmation_of))
|
21
|
+
assert_equal 'confirmation-of_name', validation[:class]
|
22
|
+
end
|
23
|
+
|
24
|
+
test "exclusion_of" do
|
25
|
+
values = (6..10).to_a
|
26
|
+
validation = ::ValidatiousOnRails::ModelValidations.exclusion_of(
|
27
|
+
validation(:validates_exclusion_of, :in => values)
|
28
|
+
)
|
29
|
+
assert_match /^exclusion-in-(\d+)/, validation[:class]
|
30
|
+
assert_match /#{values.to_json}/, validation[:validator].fn
|
31
|
+
end
|
32
|
+
|
33
|
+
test "format_of" do
|
34
|
+
pattern = /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i
|
35
|
+
validation = ::ValidatiousOnRails::ModelValidations.format_of(
|
36
|
+
validation(:validates_format_of, :with => pattern)
|
37
|
+
)
|
38
|
+
assert_match /^format-with-(\d+)/, validation[:class]
|
39
|
+
assert_match /#{pattern.inspect[1,-1]}/, validation[:validator].fn
|
40
|
+
end
|
41
|
+
|
42
|
+
test "inclusion_of" do
|
43
|
+
values = (1..5).to_a
|
44
|
+
validation = ::ValidatiousOnRails::ModelValidations.inclusion_of(
|
45
|
+
validation(:validates_inclusion_of, :in => values)
|
46
|
+
)
|
47
|
+
assert_match /^inclusion-in-(\d+)/, validation[:class]
|
48
|
+
assert_match /#{values.to_json}/, validation[:validator].fn
|
49
|
+
end
|
50
|
+
|
51
|
+
context "length_of" do
|
52
|
+
|
53
|
+
test "with :is" do
|
54
|
+
validation = ::ValidatiousOnRails::ModelValidations.length_of(
|
55
|
+
validation(:validates_length_of, :is => 2)
|
56
|
+
)
|
57
|
+
assert_equal 'length-is_2', validation[:class]
|
58
|
+
end
|
59
|
+
|
60
|
+
test "with :in" do
|
61
|
+
validation = ::ValidatiousOnRails::ModelValidations.length_of(
|
62
|
+
validation(:validates_length_of, :in => 2..10)
|
63
|
+
)
|
64
|
+
assert_equal 'length-minimum_2 length-maximum_10', validation[:class]
|
65
|
+
end
|
66
|
+
|
67
|
+
test "with :within" do
|
68
|
+
validation = ::ValidatiousOnRails::ModelValidations.length_of(
|
69
|
+
validation(:validates_length_of, :within => 2..10)
|
70
|
+
)
|
71
|
+
assert_equal 'length-minimum_2 length-maximum_10', validation[:class]
|
72
|
+
end
|
73
|
+
|
74
|
+
test "with :minimum" do
|
75
|
+
validation = ::ValidatiousOnRails::ModelValidations.length_of(
|
76
|
+
validation(:validates_length_of, :minimum => 2)
|
77
|
+
)
|
78
|
+
assert_equal 'length-minimum_2', validation[:class]
|
79
|
+
end
|
80
|
+
|
81
|
+
test "with :maximum" do
|
82
|
+
validation = ::ValidatiousOnRails::ModelValidations.length_of(
|
83
|
+
validation(:validates_length_of, :maximum => 10)
|
84
|
+
)
|
85
|
+
assert_equal 'length-maximum_10', validation[:class]
|
86
|
+
end
|
87
|
+
|
88
|
+
test "with :minimum + :maximum" do
|
89
|
+
validation = ::ValidatiousOnRails::ModelValidations.length_of(
|
90
|
+
validation(:validates_length_of, :minimum => 2, :maximum => 10)
|
91
|
+
)
|
92
|
+
assert_equal 'length-minimum_2 length-maximum_10', validation[:class]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "numericality_of" do
|
97
|
+
|
98
|
+
context ":odd/:even" do
|
99
|
+
test "with :odd only" do
|
100
|
+
validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
|
101
|
+
validation(:validates_numericality_of, :even => false, :odd => true)
|
102
|
+
)
|
103
|
+
assert_equal 'numericality-odd', validation[:class]
|
104
|
+
end
|
105
|
+
|
106
|
+
test "with :even only" do
|
107
|
+
validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
|
108
|
+
validation(:validates_numericality_of, :even => true, :odd => false)
|
109
|
+
)
|
110
|
+
assert_equal 'numericality-even', validation[:class]
|
111
|
+
end
|
112
|
+
|
113
|
+
test "with :odd and :even" do
|
114
|
+
validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
|
115
|
+
validation(:validates_numericality_of, :even => true, :odd => true)
|
116
|
+
)
|
117
|
+
assert_equal '', validation[:class]
|
118
|
+
end
|
119
|
+
|
120
|
+
test "with neither :odd or :even" do
|
121
|
+
validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
|
122
|
+
validation(:validates_numericality_of)
|
123
|
+
)
|
124
|
+
assert_equal '', validation[:class]
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
test "with :only_integer" do
|
130
|
+
validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
|
131
|
+
validation(:validates_numericality_of, :only_integer => true)
|
132
|
+
)
|
133
|
+
# Alt. more generic idea: assert_equal 'numericality-precision_0', validation[:class]
|
134
|
+
assert_equal 'numericality-only_integer', validation[:class]
|
135
|
+
end
|
136
|
+
|
137
|
+
test "with :greater_than" do
|
138
|
+
validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
|
139
|
+
validation(:validates_numericality_of, :greater_than => 2)
|
140
|
+
)
|
141
|
+
assert_equal 'numericality-greater-than_2', validation[:class]
|
142
|
+
end
|
143
|
+
|
144
|
+
test "with :greater_than_or_equal_to" do
|
145
|
+
validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
|
146
|
+
validation(:validates_numericality_of, :greater_than_or_equal_to => 2)
|
147
|
+
)
|
148
|
+
assert_equal 'numericality-greater-than-or-equal-to_2', validation[:class]
|
149
|
+
end
|
150
|
+
|
151
|
+
test "with :equal_to" do
|
152
|
+
validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
|
153
|
+
validation(:validates_numericality_of, :equal_to => 2)
|
154
|
+
)
|
155
|
+
assert_equal 'numericality-equal-to_2', validation[:class]
|
156
|
+
end
|
157
|
+
|
158
|
+
test "with :less_than" do
|
159
|
+
validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
|
160
|
+
validation(:validates_numericality_of, :less_than => 10)
|
161
|
+
)
|
162
|
+
assert_equal 'numericality-less-than_10', validation[:class]
|
163
|
+
end
|
164
|
+
|
165
|
+
test "with :less_than_or_equal_to" do
|
166
|
+
validation = ::ValidatiousOnRails::ModelValidations.numericality_of(
|
167
|
+
validation(:validates_numericality_of, :less_than_or_equal_to => 10)
|
168
|
+
)
|
169
|
+
assert_equal 'numericality-less-than-or-equal-to_10', validation[:class]
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
test "presence_of" do
|
174
|
+
validation = ::ValidatiousOnRails::ModelValidations.presence_of(validation(:validates_presence_of))
|
175
|
+
assert_equal 'required', validation[:class]
|
176
|
+
end
|
177
|
+
|
178
|
+
test "uniqueness_of" do
|
179
|
+
# TODO: not implemented
|
180
|
+
end
|
181
|
+
|
182
|
+
private
|
183
|
+
|
184
|
+
# Simulate a validation
|
185
|
+
#
|
186
|
+
def validation(macro, options = {})
|
187
|
+
::ActiveRecord::Reflection::MacroReflection.new(macro, :name, options, BogusItem.new)
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), *%w(.. .. test_helper)))
|
3
|
+
|
4
|
+
module Test::Unit::Assertions
|
5
|
+
|
6
|
+
#
|
7
|
+
# Assert that a piece of HTML includes the class name.
|
8
|
+
#
|
9
|
+
def assert_has_class(class_name, html, message = nil)
|
10
|
+
# Might need to consider this...but works a bit better.
|
11
|
+
classes = html.scan(/class="([^"]*)"/).collect { |c| c.to_s.split(' ') }.flatten
|
12
|
+
full_message = build_message(message, "<?>\nexpected to include class(es) <?>.\n", html, class_name)
|
13
|
+
|
14
|
+
assert_block(full_message) do
|
15
|
+
class_name.split(' ').all? { |cname| classes.include?(cname) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'action_view/test_case'
|
22
|
+
|
23
|
+
class FormHelperTest < ::ActionView::TestCase
|
24
|
+
|
25
|
+
include ActionView::Helpers::FormHelper
|
26
|
+
|
27
|
+
def setup
|
28
|
+
@bogus_item = BogusItem.new
|
29
|
+
end
|
30
|
+
|
31
|
+
test "attach custom javascript validations to layout" do
|
32
|
+
@content_for_validatious = nil
|
33
|
+
view_output = form_for(@bogus_item, :url => '/bogus_items') do |f|
|
34
|
+
concat f.text_field(:url)
|
35
|
+
end
|
36
|
+
assert_match /v2.Validator/, @content_for_validatious
|
37
|
+
end
|
38
|
+
|
39
|
+
test "required :text_field" do
|
40
|
+
# Using helper
|
41
|
+
assert_has_class 'required', text_field(:bogus_item, :name)
|
42
|
+
assert_has_class 'required some_other_class', text_field(:bogus_item, :name, :class => 'some_other_class')
|
43
|
+
|
44
|
+
# Using builder
|
45
|
+
assert_has_class 'required', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
46
|
+
concat f.text_field(:name)
|
47
|
+
}
|
48
|
+
assert_has_class 'required text', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
49
|
+
concat f.text_field(:name, :class => 'text')
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
test "required :password_field" do
|
54
|
+
# Using helper
|
55
|
+
assert_has_class 'required', password_field(:bogus_item, :name)
|
56
|
+
assert_has_class 'required some_other_class', password_field(:bogus_item, :name, :class => 'some_other_class')
|
57
|
+
|
58
|
+
# Using builder
|
59
|
+
assert_has_class 'required', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
60
|
+
concat f.password_field(:name)
|
61
|
+
}
|
62
|
+
assert_has_class 'required some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
63
|
+
concat f.password_field(:name, :class => 'some_other_class')
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
test "required :text_area" do
|
68
|
+
# Using helper
|
69
|
+
assert_has_class 'required', text_area(:bogus_item, :body)
|
70
|
+
assert_has_class 'required some_other_class', text_area(:bogus_item, :body, :class => 'some_other_class')
|
71
|
+
|
72
|
+
# Using builder
|
73
|
+
assert_has_class 'required', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
74
|
+
concat f.text_area(:body)
|
75
|
+
}
|
76
|
+
assert_has_class 'required some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
77
|
+
concat f.text_area(:body, :class => 'some_other_class')
|
78
|
+
}
|
79
|
+
end
|
80
|
+
|
81
|
+
test "required :check_box" do # a.k.a. "acceptance required"
|
82
|
+
# Using helper
|
83
|
+
assert_has_class 'required', check_box(:bogus_item, :signed)
|
84
|
+
assert_has_class 'required some_other_class', check_box(:bogus_item, :signed, :class => 'some_other_class')
|
85
|
+
|
86
|
+
# Using builder
|
87
|
+
assert_has_class 'required', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
88
|
+
concat f.check_box(:signed)
|
89
|
+
}
|
90
|
+
assert_has_class 'required some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
91
|
+
concat f.check_box(:signed, :class => 'some_other_class')
|
92
|
+
}
|
93
|
+
end
|
94
|
+
|
95
|
+
test "required :radio_button" do
|
96
|
+
# Using helper
|
97
|
+
assert_has_class 'required', radio_button(:bogus_item, :variant, 1)
|
98
|
+
assert_has_class 'required some_other_class', radio_button(:bogus_item, :variant, 1, :class => 'some_other_class')
|
99
|
+
|
100
|
+
assert_has_class 'required', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
101
|
+
concat f.radio_button(:variant, 1)
|
102
|
+
}
|
103
|
+
assert_has_class 'required some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
104
|
+
concat f.radio_button(:variant, 1, :class => 'some_other_class')
|
105
|
+
}
|
106
|
+
end
|
107
|
+
|
108
|
+
test "confirmation_of :name field" do
|
109
|
+
# Using helper
|
110
|
+
assert_has_class 'confirmation-of_name', text_field(:bogus_item, :name_confirmation)
|
111
|
+
assert_has_class 'confirmation-of_name some_other_class', text_field(:bogus_item, :name_confirmation, :class => 'some_other_class')
|
112
|
+
|
113
|
+
# Using builder
|
114
|
+
assert_has_class 'confirmation-of_name', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
115
|
+
concat f.text_field(:name_confirmation)
|
116
|
+
}
|
117
|
+
assert_has_class 'confirmation-of_name some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
118
|
+
concat f.text_field(:name_confirmation, :class => 'some_other_class')
|
119
|
+
}
|
120
|
+
end
|
121
|
+
|
122
|
+
test "format_of :url field" do
|
123
|
+
# Using helper
|
124
|
+
assert_has_class 'url', text_field(:bogus_item, :url)
|
125
|
+
assert_has_class 'url some_other_class', text_field(:bogus_item, :url, :class => 'some_other_class')
|
126
|
+
|
127
|
+
# Using builder
|
128
|
+
assert_has_class 'url', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
129
|
+
concat f.text_field(:url)
|
130
|
+
}
|
131
|
+
assert_has_class 'url some_other_class', form_for(@bogus_item, :url => '/bogus_items') { |f|
|
132
|
+
concat f.text_field(:url, :class => 'some_other_class')
|
133
|
+
}
|
134
|
+
assert_match /v2.Validator\.add\(.*\{.*"url"/, @content_for_validatious
|
135
|
+
end
|
136
|
+
|
137
|
+
test "exclusion_of :variant field" do
|
138
|
+
# TODO
|
139
|
+
end
|
140
|
+
|
141
|
+
test "inclusion_of :variant field" do
|
142
|
+
# TODO
|
143
|
+
end
|
144
|
+
|
145
|
+
test "regular label" do
|
146
|
+
# Using helper
|
147
|
+
assert_equal "<label for=\"bogus_item_name\">Name</label>", label(:bogus_item, :name)
|
148
|
+
end
|
149
|
+
|
150
|
+
test "label with title" do
|
151
|
+
# Using helper
|
152
|
+
assert_equal "<label for=\"bogus_item_name\" title=\"craaazy\">Name</label>",
|
153
|
+
label(:bogus_item, :name, nil, :title => "craaazy")
|
154
|
+
end
|
155
|
+
|
156
|
+
test "label without title" do
|
157
|
+
# Using helper
|
158
|
+
assert_equal "<label for=\"bogus_item_name\" title=\"Name\">Your name</label>",
|
159
|
+
label(:bogus_item, :name, "Your name")
|
160
|
+
end
|
161
|
+
|
162
|
+
private
|
163
|
+
|
164
|
+
def protect_against_forgery?
|
165
|
+
false
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), *%w(.. .. test_helper)))
|
3
|
+
|
4
|
+
require 'active_support/test_case'
|
5
|
+
|
6
|
+
class ValidatorTest < ::ActiveSupport::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@empty_validator = ValidatiousOnRails::Validatious::Validator.new('dummie')
|
10
|
+
@custom_validator = returning ValidatiousOnRails::Validatious::Validator.new('dummie') do |v|
|
11
|
+
v.message = 'Fail, fail, fail!'
|
12
|
+
v.params = ['some', 'params']
|
13
|
+
v.aliases = ['some', 'aliases']
|
14
|
+
v.accept_empty = false
|
15
|
+
v.fn = "return false;"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
test "creating an empty validator - and generate valid v2.Validator (using #to_s)" do
|
20
|
+
assert_equal 'dummie', @empty_validator.name
|
21
|
+
assert_equal '', @empty_validator.message
|
22
|
+
assert_equal ([]), @empty_validator.params
|
23
|
+
assert_equal ([]), @empty_validator.aliases
|
24
|
+
assert_equal true, @empty_validator.accept_empty
|
25
|
+
assert_equal "function(field, value, params) {return true;}", @empty_validator.fn.gsub(/\n/, '')
|
26
|
+
|
27
|
+
expected_v2_validator = 'v2.Validator.add({
|
28
|
+
acceptEmpty: true,
|
29
|
+
fn: function(field, value, params) {return true;},
|
30
|
+
name: "dummie"
|
31
|
+
});'
|
32
|
+
|
33
|
+
assert_equal expected_v2_validator.gsub(/[\n\s\t]/, ''), @empty_validator.to_s.gsub(/[\n\s\t]/, '')
|
34
|
+
end
|
35
|
+
|
36
|
+
test "creating a custom validator - and generate valid v2.Validator (using #to_s)" do
|
37
|
+
assert_equal 'dummie', @custom_validator.name
|
38
|
+
assert_equal 'Fail, fail, fail!', @custom_validator.message
|
39
|
+
assert_equal (["some", "params"]), @custom_validator.params
|
40
|
+
assert_equal (["some", "aliases"]), @custom_validator.aliases
|
41
|
+
assert_equal false, @custom_validator.accept_empty
|
42
|
+
assert_equal "function(field, value, params) {return false;}", @custom_validator.fn.gsub(/\n/, '')
|
43
|
+
|
44
|
+
expected_v2_validator = 'v2.Validator.add({
|
45
|
+
acceptEmpty: false,
|
46
|
+
aliases: ["some", "aliases"],
|
47
|
+
fn: function(field, value, params) {return false;},
|
48
|
+
message: "Fail, fail, fail!",
|
49
|
+
name: "dummie",
|
50
|
+
params: ["some", "params"]
|
51
|
+
});'
|
52
|
+
|
53
|
+
assert_equal expected_v2_validator.gsub(/[\n\s\t]/, ''), @custom_validator.to_s.gsub(/[\n\s\t]/, '')
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|