Compare commits

...

9 Commits
2.3.1 ... v2.3

Author SHA1 Message Date
Adam Meehan
014bb76622 remove :equal_to deprecation message 2011-02-26 13:08:11 +11:00
Adam Meehan
3533ce0702 exit restriction validation when combined :with_time or :with_date value is nil 2011-02-26 13:07:02 +11:00
Adam Meehan
e711941744 version 2.3.2 2010-11-07 11:04:42 +11:00
Adam Meehan
5bed56e180 fixes for latest I18n interpolation token detection 2010-11-07 10:59:16 +11:00
Adam Meehan
c3a591a5d8 fix bad spec 2010-11-06 19:08:25 +11:00
Adam Meehan
6265b2d804 fix parsing of datetimes with timezone (thanks sigi) 2010-11-06 19:05:50 +11:00
Adam Meehan
341a481231 install instructions for v2 2010-09-17 15:34:03 +10:00
Adam Meehan
d6ddae9977 don't allow 0 or > 12 for meridian am hour 2010-09-17 14:52:39 +10:00
Adam Meehan
0ea0201051 put install plugin below gem 2010-07-27 07:32:42 +10:00
14 changed files with 91 additions and 44 deletions

View File

@@ -1,3 +1,8 @@
= 2.3.2 [2010-11-07]
- Fixed parser for string with timezone offset (thanks sigi)
- Support for latest I18n interpolation tokens in locale file
- Fixed parser allowing an hour over 12 for AM meridian
= 2.3.1 [2010-03-19]
- Fixed bug where custom attribute writer method for date/times were being overriden

View File

@@ -31,18 +31,17 @@ time string.
== INSTALLATION:
As plugin (from master)
./script/plugin install git://github.com/adzap/validates_timeliness.git
As gem
sudo gem install validates_timeliness
gem install validates_timeliness -v '~> 2.3'
# in environment.rb
config.gem 'validates_timeliness'
config.gem 'validates_timeliness', :version => '~> 2.3'
As plugin (from master)
./script/plugin install git://github.com/adzap/validates_timeliness.git -r v2.3
== USAGE:

View File

@@ -2,10 +2,18 @@ require 'validates_timeliness/formats'
require 'validates_timeliness/parser'
require 'validates_timeliness/validator'
require 'validates_timeliness/validation_methods'
require 'validates_timeliness/active_record/attribute_methods'
require 'validates_timeliness/active_record/multiparameter_attributes'
require 'validates_timeliness/action_view/instance_tag'
begin
i18n_path = $:.grep(/active_support\/vendor\/i18n-/)
if i18n_path.empty?
require 'i18n/version'
else
require i18n_path[0] + '/version'
end
rescue LoadError
end if defined?(I18n)
module ValidatesTimeliness
@@ -15,7 +23,9 @@ module ValidatesTimeliness
mattr_accessor :use_time_zones
self.use_time_zones = false
LOCALE_PATH = File.expand_path(File.dirname(__FILE__) + '/validates_timeliness/locale/en.yml')
I18N_LATEST = defined?(I18n::VERSION) && I18n::VERSION >= '0.4.0'
locale_file = I18N_LATEST ? 'en.new.yml' : 'en.old.yml'
LOCALE_PATH = File.expand_path(File.join(File.dirname(__FILE__),'validates_timeliness','locale',locale_file))
class << self

View File

@@ -209,6 +209,8 @@ module ValidatesTimeliness
values[0..2] = dummy_date_for_time_type if type == :time
return values
end
rescue
nil
end
# Delete formats of specified type. Error raised if format not found.
@@ -255,6 +257,7 @@ module ValidatesTimeliness
hour = hour.to_i
return hour if meridian.nil?
if meridian.delete('.').downcase == 'am'
raise if hour == 0 || hour > 12
hour == 12 ? 0 : hour
else
hour == 12 ? hour : hour + 12

View File

@@ -0,0 +1,18 @@
en:
activerecord:
errors:
messages:
invalid_date: "is not a valid date"
invalid_time: "is not a valid time"
invalid_datetime: "is not a valid datetime"
is_at: "must be at %{restriction}"
before: "must be before %{restriction}"
on_or_before: "must be on or before %{restriction}"
after: "must be after %{restriction}"
on_or_after: "must be on or after %{restriction}"
between: "must be between %{earliest} and %{latest}"
validates_timeliness:
error_value_formats:
date: '%Y-%m-%d'
time: '%H:%M:%S'
datetime: '%Y-%m-%d %H:%M:%S'

View File

@@ -13,7 +13,7 @@ module ValidatesTimeliness
if type == :date
Date.new(*time_array[0..2]) rescue nil
else
make_time(time_array[0..7])
make_time(time_array[0..6])
end
end

View File

@@ -1,4 +1,3 @@
#TODO remove deprecated option :equal_to
module ValidatesTimeliness
class Validator
@@ -8,7 +7,6 @@ module ValidatesTimeliness
RESTRICTION_METHODS = {
:is_at => :==,
:equal_to => :==,
:before => :<,
:after => :>,
:on_or_before => :<=,
@@ -60,6 +58,7 @@ module ValidatesTimeliness
def validate_restrictions(record, attr_name, value)
if configuration[:with_time] || configuration[:with_date]
value = combine_date_and_time(value, record)
return if value.nil?
end
value = self.class.type_cast_value(value, implied_type, configuration[:ignore_usec])
@@ -142,12 +141,6 @@ module ValidatesTimeliness
end
def validate_options(options)
if options.key?(:equal_to)
::ActiveSupport::Deprecation.warn("ValidatesTimeliness :equal_to option is deprecated due to clash with a default Rails option. Use :is_at instead. You will need to fix any I18n error message references to this option date/time attributes now.")
options[:is_at] = options.delete(:equal_to)
options[:is_at_message] = options.delete(:equal_to_message)
end
invalid_for_type = ([:time, :date, :datetime] - [type]).map {|k| "invalid_#{k}_message".to_sym }
invalid_for_type << :with_date unless type == :time
invalid_for_type << :with_time unless type == :date

View File

@@ -1,3 +1,3 @@
module ValidatesTimeliness
VERSION = "2.3.1"
VERSION = "2.3.2"
end

View File

@@ -102,6 +102,13 @@ describe ValidatesTimeliness::Formats do
time_array.should == [2000,1,1,12,13,14,0]
end
it "should return nil if time hour is out of range for AM meridian" do
time_array = formats.parse('13:14 am', :time, :strict => true)
time_array.should == nil
time_array = formats.parse('00:14 am', :time, :strict => true)
time_array.should == nil
end
it "should return date array from time string" do
time_array = formats.parse('2000-02-01', :date, :strict => true)
time_array.should == [2000,2,1,0,0,0,0]

View File

@@ -9,7 +9,7 @@
# ginger spec
#
Ginger.configure do |config|
rails_versions = ['2.0.2', '2.1.2', '2.2.2', '2.3.3', '2.3.4', '2.3.5']
rails_versions = ['2.0.2', '2.1.2', '2.2.2', '2.3.3', '2.3.4', '2.3.5', '2.3.6', '2.3.9']
rails_versions.each do |v|
g = Ginger::Scenario.new("Rails #{v}")

View File

@@ -8,6 +8,10 @@ describe ValidatesTimeliness::Parser do
parse("2000-01-01 12:13:14", :datetime).should be_kind_of(Time)
end
it "should return Time object for ISO 8601 string with time zone" do
parse("2000-01-01T12:23:42+09:00", :datetime).should be_kind_of(Time)
end
it "should return nil for time string with invalid date part" do
parse("2000-02-30 12:13:14", :datetime).should be_nil
end
@@ -23,8 +27,8 @@ describe ValidatesTimeliness::Parser do
if RAILS_VER >= '2.1'
it "should convert time string into current timezone" do
Time.zone = 'Melbourne'
time = parse("2000-01-01 12:13:14", :datetime)
Time.zone.utc_offset.should == 10.hours
time = parse("2000-06-01 12:13:14", :datetime)
time.utc_offset.should == 10.hours
end
end

View File

@@ -3,6 +3,14 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe ValidatesTimeliness::Validator do
attr_accessor :person, :validator
if ValidatesTimeliness::I18N_LATEST
I18N_REGEXP = /\%\{\w*\}/
I18N_INTERPOLATION = '%{%s}'
else
I18N_REGEXP = /\{\{\w*\}\}/
I18N_INTERPOLATION = '{{%s}}'
end
before :all do
# freezes time using time_travel plugin
Time.now = Time.utc(2000, 1, 1, 0, 0, 0)
@@ -17,10 +25,6 @@ describe ValidatesTimeliness::Validator do
end
describe "option keys validation" do
before(:all) do
ActiveSupport::Deprecation.silenced = true
end
before do
keys = ValidatesTimeliness::Validator::VALID_OPTION_KEYS - [:invalid_date_message, :invalid_time_message, :with_date, :with_time]
@valid_options = keys.inject({}) {|hash, opt| hash[opt] = nil; hash }
@@ -34,15 +38,6 @@ describe ValidatesTimeliness::Validator do
it "should not raise error if option keys are valid" do
lambda { Person.validates_datetime(@valid_options) }.should_not raise_error(ArgumentError)
end
it "should display deprecation notice for :equal_to" do
::ActiveSupport::Deprecation.should_receive(:warn)
Person.validates_datetime :equal_to => Time.now
end
after(:all) do
ActiveSupport::Deprecation.silenced = false
end
end
describe "evaluate_option_value" do
@@ -421,7 +416,6 @@ describe ValidatesTimeliness::Validator do
end
describe "instance with :with_time option" do
it "should validate date attribute as datetime combining value of :with_time against restrictions " do
configure_validator(:type => :date, :with_time => '12:31', :on_or_before => Time.mktime(2000,1,1,12,30))
validate_with(:birth_date, "2000-01-01")
@@ -434,6 +428,12 @@ describe ValidatesTimeliness::Validator do
should_have_no_error(:birth_date, :on_or_before)
end
it "should skip restriction validation if :with_time value evaluates to nil" do
configure_validator(:type => :date, :with_time => lambda { nil }, :on_or_before => Time.mktime(2000,1,1,12,30))
validate_with(:birth_date, "2000-01-01")
should_have_no_error(:birth_date, :on_or_before)
end
it "should should ignore usec value on combined value if :ignore_usec option is true" do
configure_validator(:type => :date, :with_time => Time.mktime(2000,1,1,12,30,0,500), :is_at => Time.mktime(2000,1,1,12,30), :ignore_usec => true)
validate_with(:birth_date, "2000-01-01")
@@ -455,6 +455,12 @@ describe ValidatesTimeliness::Validator do
should_have_no_error(:birth_time, :on_or_before)
end
it "should skip restriction validation if :with_date value is nil" do
configure_validator(:type => :time, :with_date => lambda { nil }, :on_or_before => Time.mktime(2000,1,1,12,30))
validate_with(:birth_time, "12:30")
should_have_no_error(:birth_time, :on_or_before)
end
it "should should ignore usec value on combined value if :ignore_usec option is true" do
configure_validator(:type => :time, :with_date => Date.new(2000,1,1), :on_or_before => Time.mktime(2000,1,1,12,30), :ignore_usec => true)
validate_with(:birth_time, Time.mktime(2000,1,1,12,30,0,50))
@@ -513,8 +519,9 @@ describe ValidatesTimeliness::Validator do
describe "localized error messages" do
before(:all) do
message = "retfa #{I18N_INTERPOLATION}" % 'restriction'
translations = {
:activerecord => {:errors => {:messages => { :after => 'retfa {{restriction}}' }}},
:activerecord => {:errors => {:messages => { :after => message }}},
:validates_timeliness => {:error_value_formats => {}}
}
I18n.backend.store_translations 'zz', translations
@@ -616,7 +623,8 @@ describe ValidatesTimeliness::Validator do
describe "I18n" do
it "should use global default if locale format missing" do
I18n.backend.store_translations 'zz', :activerecord => {:errors => {:messages => { :after => 'after {{restriction}}' }}}
message = "after #{I18N_INTERPOLATION}" % 'restriction'
I18n.backend.store_translations 'zz', :activerecord => {:errors => {:messages => { :after => message }}}
I18n.locale = :zz
configure_validator(:type => :datetime, :after => 1.day.from_now)
validate_with(:birth_date_and_time, Time.now)
@@ -708,6 +716,6 @@ describe ValidatesTimeliness::Validator do
def error_messages
return @error_messages if defined?(@error_messages)
messages = defined?(I18n) ? I18n.t('activerecord.errors.messages') : validator.send(:error_messages)
@error_messages = messages.inject({}) {|h, (k, v)| h[k] = v.sub(/ (\%s|\{\{\w*\}\}).*/, ''); h }
@error_messages = messages.inject({}) {|h, (k, v)| h[k] = v.sub(/ (\%s|#{I18N_REGEXP}).*/, ''); h }
end
end

View File

@@ -2,27 +2,27 @@
Gem::Specification.new do |s|
s.name = %q{validates_timeliness}
s.version = "2.3.1"
s.version = "2.3.2"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Adam Meehan"]
s.autorequire = %q{validates_timeliness}
s.date = %q{2010-03-19}
s.date = %q{2010-11-07}
s.description = %q{Date and time validation plugin for Rails 2.x which allows custom formats}
s.email = %q{adam.meehan@gmail.com}
s.extra_rdoc_files = ["README.rdoc", "LICENSE", "TODO", "CHANGELOG"]
s.files = ["LICENSE", "README.rdoc", "Rakefile", "TODO", "CHANGELOG", "lib/validates_timeliness", "lib/validates_timeliness/action_view", "lib/validates_timeliness/action_view/instance_tag.rb", "lib/validates_timeliness/active_record", "lib/validates_timeliness/active_record/attribute_methods.rb", "lib/validates_timeliness/active_record/multiparameter_attributes.rb", "lib/validates_timeliness/formats.rb", "lib/validates_timeliness/locale", "lib/validates_timeliness/locale/en.yml", "lib/validates_timeliness/matcher.rb", "lib/validates_timeliness/parser.rb", "lib/validates_timeliness/spec", "lib/validates_timeliness/spec/rails", "lib/validates_timeliness/spec/rails/matchers", "lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb", "lib/validates_timeliness/validation_methods.rb", "lib/validates_timeliness/validator.rb", "lib/validates_timeliness/version.rb", "lib/validates_timeliness.rb", "spec/action_view", "spec/action_view/instance_tag_spec.rb", "spec/active_record", "spec/active_record/attribute_methods_spec.rb", "spec/active_record/multiparameter_attributes_spec.rb", "spec/formats_spec.rb", "spec/ginger_scenarios.rb", "spec/parser_spec.rb", "spec/resources", "spec/resources/application.rb", "spec/resources/person.rb", "spec/resources/schema.rb", "spec/resources/sqlite_patch.rb", "spec/spec", "spec/spec/rails", "spec/spec/rails/matchers", "spec/spec/rails/matchers/validate_timeliness_spec.rb", "spec/spec_helper.rb", "spec/time_travel", "spec/time_travel/MIT-LICENSE", "spec/time_travel/time_extensions.rb", "spec/time_travel/time_travel.rb", "spec/validator_spec.rb"]
s.files = ["LICENSE", "README.rdoc", "Rakefile", "TODO", "CHANGELOG", "lib/validates_timeliness", "lib/validates_timeliness/action_view", "lib/validates_timeliness/action_view/instance_tag.rb", "lib/validates_timeliness/active_record", "lib/validates_timeliness/active_record/attribute_methods.rb", "lib/validates_timeliness/active_record/multiparameter_attributes.rb", "lib/validates_timeliness/formats.rb", "lib/validates_timeliness/locale", "lib/validates_timeliness/locale/en.new.yml", "lib/validates_timeliness/locale/en.old.yml", "lib/validates_timeliness/matcher.rb", "lib/validates_timeliness/parser.rb", "lib/validates_timeliness/spec", "lib/validates_timeliness/spec/rails", "lib/validates_timeliness/spec/rails/matchers", "lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb", "lib/validates_timeliness/validation_methods.rb", "lib/validates_timeliness/validator.rb", "lib/validates_timeliness/version.rb", "lib/validates_timeliness.rb", "spec/action_view", "spec/action_view/instance_tag_spec.rb", "spec/active_record", "spec/active_record/attribute_methods_spec.rb", "spec/active_record/multiparameter_attributes_spec.rb", "spec/formats_spec.rb", "spec/ginger_scenarios.rb", "spec/parser_spec.rb", "spec/resources", "spec/resources/application.rb", "spec/resources/person.rb", "spec/resources/schema.rb", "spec/resources/sqlite_patch.rb", "spec/spec", "spec/spec/rails", "spec/spec/rails/matchers", "spec/spec/rails/matchers/validate_timeliness_spec.rb", "spec/spec_helper.rb", "spec/time_travel", "spec/time_travel/MIT-LICENSE", "spec/time_travel/time_extensions.rb", "spec/time_travel/time_travel.rb", "spec/validator_spec.rb"]
s.homepage = %q{http://github.com/adzap/validates_timeliness}
s.require_paths = ["lib"]
s.rubyforge_project = %q{validatestime}
s.rubygems_version = %q{1.3.5}
s.rubygems_version = %q{1.3.7}
s.summary = %q{Date and time validation plugin for Rails 2.x which allows custom formats}
if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
else
end
else