Permalink
Browse files

add support for proc or lambda as an option for FormatValidator

  • Loading branch information...
1 parent 2296d25 commit 6c8a2d57cb462c790ae01b15f803e17491111f22 @nashby committed Jan 1, 2012
@@ -17,25 +17,25 @@ def integer?
def minimum_value(validator_options)
if integer? && validator_options.key?(:greater_than)
- evaluate_validator_option(validator_options[:greater_than]) + 1
+ evaluate_numericality_validator_option(validator_options[:greater_than]) + 1
else
- evaluate_validator_option(validator_options[:greater_than_or_equal_to])
+ evaluate_numericality_validator_option(validator_options[:greater_than_or_equal_to])
end
end
def maximum_value(validator_options)
if integer? && validator_options.key?(:less_than)
- evaluate_validator_option(validator_options[:less_than]) - 1
+ evaluate_numericality_validator_option(validator_options[:less_than]) - 1
else
- evaluate_validator_option(validator_options[:less_than_or_equal_to])
+ evaluate_numericality_validator_option(validator_options[:less_than_or_equal_to])
end
end
def find_numericality_validator
find_validator(ActiveModel::Validations::NumericalityValidator)
end
- def evaluate_validator_option(option)
+ def evaluate_numericality_validator_option(option)
if option.is_a?(Numeric)
option
elsif option.is_a?(Symbol)
@@ -12,7 +12,7 @@ def pattern
def pattern_source
if options[:pattern] == true
if pattern_validator = find_pattern_validator
- pattern_validator.options[:with].source
+ evaluate_format_validator_option(pattern_validator.options[:with]).source
end
else
options[:pattern]
@@ -22,6 +22,14 @@ def pattern_source
def find_pattern_validator
find_validator(ActiveModel::Validations::FormatValidator)
end
+
+ def evaluate_format_validator_option(option)
+ if option.is_a?(Regexp)
+ option
+ elsif option.respond_to?(:call)
+ option.call(object)
+ end
+ end
end
end
end
@@ -67,6 +67,11 @@ class StringInputTest < ActionView::TestCase
assert_select 'input[pattern="\w+"]'
end
+ test 'input should infer pattern from attributes using proc' do
+ with_input_for @other_validating_user, :name, :string
+ assert_select 'input[pattern="\w+"]'
+ end
+
test 'input should infer pattern from attributes when pattern is true when default is false' do
swap_wrapper do
with_input_for @other_validating_user, :country, :string
View
@@ -189,6 +189,7 @@ class OtherValidatingUser < User
:only_integer => true
validates_format_of :country, :with => /\w+/
+ validates_format_of :name, :with => Proc.new { /\w+/ }
end
class HashBackedAuthor < Hash

0 comments on commit 6c8a2d5

Please sign in to comment.