Skip to content

Commit

Permalink
Fix error message on comparison validator
Browse files Browse the repository at this point in the history
  • Loading branch information
kamipo committed Apr 24, 2021
1 parent a1afc77 commit f83dbe3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
9 changes: 0 additions & 9 deletions activemodel/lib/active_model/validations/comparability.rb
Expand Up @@ -24,15 +24,6 @@ def error_options(value, option_value)
value: value
)
end

def error_value(record, option_value)
case option_value
when Proc
option_value(record, option_value)
else
option_value
end
end
end
end
end
8 changes: 5 additions & 3 deletions activemodel/lib/active_model/validations/comparison.rb
Expand Up @@ -16,12 +16,14 @@ def check_validity!

def validate_each(record, attr_name, value)
options.slice(*COMPARE_CHECKS.keys).each do |option, raw_option_value|
option_value = option_value(record, raw_option_value)

if value.nil? || value.blank?
return record.errors.add(attr_name, :blank, **error_options(value, error_value(record, raw_option_value)))
return record.errors.add(attr_name, :blank, **error_options(value, option_value))
end

unless value.send(COMPARE_CHECKS[option], option_value(record, raw_option_value))
record.errors.add(attr_name, option, **error_options(value, error_value(record, raw_option_value)))
unless value.send(COMPARE_CHECKS[option], option_value)
record.errors.add(attr_name, option, **error_options(value, option_value))
end
rescue ArgumentError => e
record.errors.add(attr_name, e.message)
Expand Down
Expand Up @@ -151,7 +151,7 @@ def test_validates_comparison_with_proc
Topic.define_method(:requested) { Date.new(2020, 8, 1) }
Topic.validates_comparison_of :approved, greater_than_or_equal_to: Proc.new(&:requested)

assert_invalid_values([Date.new(2020, 7, 1), Date.new(2019, 7, 1), DateTime.new(2020, 7, 1, 22, 34)])
assert_invalid_values([Date.new(2020, 7, 1), Date.new(2019, 7, 1), DateTime.new(2020, 7, 1, 22, 34)], "must be greater than or equal to 2020-08-01")
assert_valid_values([Date.new(2020, 8, 2), DateTime.new(2021, 8, 1)])
ensure
Topic.remove_method :requested
Expand All @@ -161,7 +161,7 @@ def test_validates_comparison_with_method
Topic.define_method(:requested) { Date.new(2020, 8, 1) }
Topic.validates_comparison_of :approved, greater_than_or_equal_to: :requested

assert_invalid_values([Date.new(2020, 7, 1), Date.new(2019, 7, 1), DateTime.new(2020, 7, 1, 22, 34)])
assert_invalid_values([Date.new(2020, 7, 1), Date.new(2019, 7, 1), DateTime.new(2020, 7, 1, 22, 34)], "must be greater than or equal to 2020-08-01")
assert_valid_values([Date.new(2020, 8, 2), DateTime.new(2021, 8, 1)])
ensure
Topic.remove_method :requested
Expand Down
Expand Up @@ -225,7 +225,7 @@ def test_validates_numericality_with_proc
Topic.define_method(:min_approved) { 5 }
Topic.validates_numericality_of :approved, greater_than_or_equal_to: Proc.new(&:min_approved)

assert_invalid_values([3, 4])
assert_invalid_values([3, 4], "must be greater than or equal to 5")
assert_valid_values([5, 6])
ensure
Topic.remove_method :min_approved
Expand All @@ -235,7 +235,7 @@ def test_validates_numericality_with_symbol
Topic.define_method(:max_approved) { 5 }
Topic.validates_numericality_of :approved, less_than_or_equal_to: :max_approved

assert_invalid_values([6])
assert_invalid_values([6], "must be less than or equal to 5")
assert_valid_values([4, 5])
ensure
Topic.remove_method :max_approved
Expand Down

0 comments on commit f83dbe3

Please sign in to comment.