Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add support for proc or lambda as an option for FormatValidator

  • Loading branch information...
commit 6c8a2d57cb462c790ae01b15f803e17491111f22 1 parent 2296d25
@nashby authored
View
10 lib/simple_form/components/min_max.rb
@@ -17,17 +17,17 @@ 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
@@ -35,7 +35,7 @@ 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)
View
10 lib/simple_form/components/pattern.rb
@@ -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
View
5 test/inputs/string_input_test.rb
@@ -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
1  test/support/models.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.