Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't use .to_i to avoid different usec being considered equal #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/active_model/validations/date_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def validate_each(record, attr_name, value)
value = value.to_datetime if value.is_a?(Date)
end

unless is_time?(option_value) && value.to_i.send(CHECKS[option], option_value.to_i)
unless is_time?(option_value) && value.send(CHECKS[option], option_value)
record.errors.add(attr_name, :"date_#{option}", **options.merge(
value: original_value,
date: (I18n.localize(original_option_value) rescue original_option_value)
Expand Down
4 changes: 2 additions & 2 deletions test/date_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ module Validations

describe _context do
[:after, :before, :after_or_equal_to, :before_or_equal_to, :equal_to].each do |check|
now = Time.now.to_datetime
now = Time.now.to_datetime.change(usec: 0)

model_date = case check
when :after then must_be == :valid ? now + 21000 : now - 1
when :before then must_be == :valid ? now - 21000 : now + 1
when :after_or_equal_to then must_be == :valid ? now : now - 21000
when :before_or_equal_to then must_be == :valid ? now : now + 21000
when :equal_to then must_be == :valid ? now : now + 21000
when :equal_to then must_be == :valid ? now : now.change(usec: 1)
end

it "ensures that an attribute is #{must_be} when #{must_be == :valid ? 'respecting' : 'offending' } the #{check} check" do
Expand Down