Skip to content

Commit

Permalink
Merge pull request codegram#13 from cryo28/master
Browse files Browse the repository at this point in the history
Russian locale and dates interpolated into validation error messages are localized themselves by using I18n.localize
  • Loading branch information
oriolgual committed Jun 8, 2011
2 parents 586d810 + e78ada4 commit 692d6e0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rvm --create use ruby-1.9.2@date_validator
rvm use ruby-1.9.2@date_validator --create
23 changes: 22 additions & 1 deletion lib/active_model/validations/date_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def validate_each(record, attr_name, value)
end

unless is_time?(option_value) && value.to_i.send(CHECKS[option], option_value.to_i)
record.errors.add(attr_name, option, options.merge(:value => original_value, :date => original_option_value))
record.errors.add(attr_name, option, options.merge(
:value => original_value,
:date => (I18n.localize(original_option_value) rescue original_option_value)
))
end
end
end
Expand All @@ -73,5 +76,23 @@ def is_time?(object)
object.is_a?(Time) || (defined?(Date) and object.is_a?(Date)) || (defined?(ActiveSupport::TimeWithZone) and object.is_a?(ActiveSupport::TimeWithZone))
end
end

module HelperMethods
# Validates whether the value of the specified attribute is a validate Date
#
# class Person < ActiveRecord::Base
# validates_date_of :payment_date, :after => :packaging_date
# validates_date_of :expiration_date, :before => Proc.new { Time.now }
# end
#
# Configuration options:
# * <tt>:after</tt> - check that a Date is after the specified one.
# * <tt>:before</tt> - check that a Date is before the specified one.
# * <tt>:after_or_equal_to</tt> - check that a Date is after or equal to the specified one.
# * <tt>:before_or_equal_to</tt> - check that a Date is before or equal to the specified one.
def validates_date_of(*attr_names)
validates_with DateValidator, _merge_attributes(attr_names)
end
end
end
end
8 changes: 8 additions & 0 deletions locales/ru.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ru:
errors:
messages:
not_a_date: "неверный формат"
after: "должна быть позднее чем %{date}"
after_or_equal_to: "должна равняться или быть позднее чем %{date}"
before: "должна быть ранее чем %{date}"
before_or_equal_to: "должна равняться или быть ранее чем %{date}"
2 changes: 1 addition & 1 deletion spec/date_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module Validations

model = TestRecord.new(model_date)
model.should_not be_valid
model.errors[:expiration_date].should eq(["must be " + check.to_s.gsub('_',' ') + " #{now}"])
model.errors[:expiration_date].should eq(["must be " + check.to_s.gsub('_',' ') + " #{I18n.localize(now)}"])
end
end
end
Expand Down

0 comments on commit 692d6e0

Please sign in to comment.