Skip to content

Commit 692d6e0

Browse files
committed
Merge pull request #13 from cryo28/master
Russian locale and dates interpolated into validation error messages are localized themselves by using I18n.localize
2 parents 586d810 + e78ada4 commit 692d6e0

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

.rvmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rvm --create use ruby-1.9.2@date_validator
1+
rvm use ruby-1.9.2@date_validator --create

lib/active_model/validations/date_validator.rb

+22-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ def validate_each(record, attr_name, value)
6262
end
6363

6464
unless is_time?(option_value) && value.to_i.send(CHECKS[option], option_value.to_i)
65-
record.errors.add(attr_name, option, options.merge(:value => original_value, :date => original_option_value))
65+
record.errors.add(attr_name, option, options.merge(
66+
:value => original_value,
67+
:date => (I18n.localize(original_option_value) rescue original_option_value)
68+
))
6669
end
6770
end
6871
end
@@ -73,5 +76,23 @@ def is_time?(object)
7376
object.is_a?(Time) || (defined?(Date) and object.is_a?(Date)) || (defined?(ActiveSupport::TimeWithZone) and object.is_a?(ActiveSupport::TimeWithZone))
7477
end
7578
end
79+
80+
module HelperMethods
81+
# Validates whether the value of the specified attribute is a validate Date
82+
#
83+
# class Person < ActiveRecord::Base
84+
# validates_date_of :payment_date, :after => :packaging_date
85+
# validates_date_of :expiration_date, :before => Proc.new { Time.now }
86+
# end
87+
#
88+
# Configuration options:
89+
# * <tt>:after</tt> - check that a Date is after the specified one.
90+
# * <tt>:before</tt> - check that a Date is before the specified one.
91+
# * <tt>:after_or_equal_to</tt> - check that a Date is after or equal to the specified one.
92+
# * <tt>:before_or_equal_to</tt> - check that a Date is before or equal to the specified one.
93+
def validates_date_of(*attr_names)
94+
validates_with DateValidator, _merge_attributes(attr_names)
95+
end
96+
end
7697
end
7798
end

locales/ru.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ru:
2+
errors:
3+
messages:
4+
not_a_date: "неверный формат"
5+
after: "должна быть позднее чем %{date}"
6+
after_or_equal_to: "должна равняться или быть позднее чем %{date}"
7+
before: "должна быть ранее чем %{date}"
8+
before_or_equal_to: "должна равняться или быть ранее чем %{date}"

spec/date_validator_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module Validations
5757

5858
model = TestRecord.new(model_date)
5959
model.should_not be_valid
60-
model.errors[:expiration_date].should eq(["must be " + check.to_s.gsub('_',' ') + " #{now}"])
60+
model.errors[:expiration_date].should eq(["must be " + check.to_s.gsub('_',' ') + " #{I18n.localize(now)}"])
6161
end
6262
end
6363
end

0 commit comments

Comments
 (0)