mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 15:22:58 +00:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e711941744 | ||
|
|
5bed56e180 | ||
|
|
c3a591a5d8 | ||
|
|
6265b2d804 | ||
|
|
341a481231 | ||
|
|
d6ddae9977 | ||
|
|
0ea0201051 | ||
|
|
b874e786b6 | ||
|
|
b356363791 | ||
|
|
e9eb812c9e | ||
|
|
353f154330 | ||
|
|
ccfad007d1 | ||
|
|
f3c0dd6144 |
@@ -1,3 +1,11 @@
|
||||
= 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
|
||||
|
||||
= 2.3.0 [2010-02-04]
|
||||
- Backwards incompatible change to :equal_to option. Fixed error message clash with :equal_to option which exists in Rails already. Option is now :is_at.
|
||||
- Fixed I18n support so it returns missing translation message instead of error
|
||||
|
||||
11
README.rdoc
11
README.rdoc
@@ -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:
|
||||
|
||||
|
||||
12
Rakefile
12
Rakefile
@@ -7,10 +7,6 @@ require 'lib/validates_timeliness/version'
|
||||
|
||||
GEM = "validates_timeliness"
|
||||
GEM_VERSION = ValidatesTimeliness::VERSION
|
||||
AUTHOR = "Adam Meehan"
|
||||
EMAIL = "adam.meehan@gmail.com"
|
||||
HOMEPAGE = "http://github.com/adzap/validates_timeliness"
|
||||
SUMMARY = "Date and time validation plugin for Rails 2.x which allows custom formats"
|
||||
|
||||
spec = Gem::Specification.new do |s|
|
||||
s.name = GEM
|
||||
@@ -19,11 +15,11 @@ spec = Gem::Specification.new do |s|
|
||||
s.rubyforge_project = "validatestime"
|
||||
s.has_rdoc = true
|
||||
s.extra_rdoc_files = ["README.rdoc", "LICENSE", "TODO", "CHANGELOG"]
|
||||
s.summary = SUMMARY
|
||||
s.summary = "Date and time validation plugin for Rails 2.x which allows custom formats"
|
||||
s.description = s.summary
|
||||
s.author = AUTHOR
|
||||
s.email = EMAIL
|
||||
s.homepage = HOMEPAGE
|
||||
s.author = "Adam Meehan"
|
||||
s.email = "adam.meehan@gmail.com"
|
||||
s.homepage = "http://github.com/adzap/validates_timeliness"
|
||||
|
||||
s.require_path = 'lib'
|
||||
s.autorequire = GEM
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ module ValidatesTimeliness
|
||||
|
||||
def write_date_time_attribute(attr_name, value, type, time_zone_aware)
|
||||
@attributes_cache["_#{attr_name}_before_type_cast"] = value
|
||||
|
||||
value = ValidatesTimeliness::Parser.parse(value, type)
|
||||
|
||||
if value && type != :date
|
||||
@@ -49,16 +48,22 @@ module ValidatesTimeliness
|
||||
|
||||
columns_hash.each do |name, column|
|
||||
if [:date, :time, :datetime].include?(column.type)
|
||||
time_zone_aware = create_time_zone_conversion_attribute?(name, column) rescue false
|
||||
|
||||
method_name = "#{name}="
|
||||
next if instance_method_already_implemented?(method_name)
|
||||
|
||||
time_zone_aware = create_time_zone_conversion_attribute?(name, column) rescue false
|
||||
define_method(method_name) do |value|
|
||||
write_date_time_attribute(name, value, column.type, time_zone_aware)
|
||||
write_date_time_attribute(name, value, column.type, time_zone_aware)
|
||||
end
|
||||
timeliness_methods << method_name
|
||||
end
|
||||
end
|
||||
|
||||
# Hack to get around instance_method_already_implemented? caching the
|
||||
# methods in the ivar. It then appears to subsequent calls that the
|
||||
# methods defined here, have not been and get defined again.
|
||||
@_defined_class_methods = nil
|
||||
|
||||
define_attribute_methods_without_timeliness
|
||||
# add generated methods which is a Set object hence no += method
|
||||
timeliness_methods.each {|attr| generated_methods << attr }
|
||||
|
||||
@@ -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
|
||||
|
||||
18
lib/validates_timeliness/locale/en.new.yml
Normal file
18
lib/validates_timeliness/locale/en.new.yml
Normal 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'
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
module ValidatesTimeliness
|
||||
VERSION = "2.3.0"
|
||||
VERSION = "2.3.2"
|
||||
end
|
||||
|
||||
@@ -6,10 +6,11 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
|
||||
end
|
||||
|
||||
it "should define and call write method on first assign" do
|
||||
Person.class_eval { @generated_methods = Set.new }
|
||||
Person.send(:undef_method, :birth_date=)
|
||||
@person.should_receive(:write_date_time_attribute)
|
||||
@person.birth_date = "2000-01-01"
|
||||
Person.class_eval { @generated_methods = Set.new; @_defined_class_methods = nil }
|
||||
Person.send(:undef_method, :birth_date=) if Person.instance_methods.include?('birth_date=')
|
||||
person = Person.new
|
||||
person.should_receive(:write_date_time_attribute)
|
||||
person.birth_date = "2000-01-01"
|
||||
end
|
||||
|
||||
it "should call write_date_time_attribute when time attribute assigned value" do
|
||||
@@ -80,7 +81,6 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
|
||||
|
||||
it "should not save invalid date value to database" do
|
||||
time_string = "2000-01-32 02:03:04"
|
||||
@person = Person.new
|
||||
@person.birth_date_and_time = time_string
|
||||
@person.save
|
||||
@person.reload
|
||||
@@ -92,7 +92,6 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
|
||||
it "should return time object from database in default timezone" do
|
||||
ActiveRecord::Base.default_timezone = :utc
|
||||
time_string = "2000-01-01 09:00:00"
|
||||
@person = Person.new
|
||||
@person.birth_date_and_time = time_string
|
||||
@person.save
|
||||
@person.reload
|
||||
@@ -111,7 +110,6 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
|
||||
it "should return time object from database in correct timezone" do
|
||||
Time.zone = 'Melbourne'
|
||||
time_string = "2000-06-01 09:00:00"
|
||||
@person = Person.new
|
||||
@person.birth_date_and_time = time_string
|
||||
@person.save
|
||||
@person.reload
|
||||
@@ -123,7 +121,6 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
|
||||
it "should return correct date value after new value assigned" do
|
||||
today = Date.today
|
||||
tomorrow = Date.today + 1.day
|
||||
@person = Person.new
|
||||
@person.birth_date = today
|
||||
@person.birth_date.should == today
|
||||
@person.birth_date = tomorrow
|
||||
@@ -133,11 +130,28 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
|
||||
it "should update date attribute on existing object" do
|
||||
today = Date.today
|
||||
tomorrow = Date.today + 1.day
|
||||
@person = Person.create(:birth_date => today)
|
||||
@person.birth_date = tomorrow
|
||||
@person.save!
|
||||
@person.reload
|
||||
@person.birth_date.should == tomorrow
|
||||
person = Person.create(:birth_date => today)
|
||||
person.birth_date = tomorrow
|
||||
person.save!
|
||||
person.reload
|
||||
person.birth_date.should == tomorrow
|
||||
end
|
||||
|
||||
describe "attribute writer" do
|
||||
|
||||
it "should be able to be overridden in class" do
|
||||
Person.class_eval { attr_accessor :birth_date }
|
||||
person = Person.new
|
||||
person.birth_date = 'overriden'
|
||||
person.birth_date.should == 'overriden'
|
||||
end
|
||||
|
||||
after do
|
||||
Person.class_eval do
|
||||
undef_method(:birth_date)
|
||||
undef_method(:birth_date=)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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}")
|
||||
|
||||
@@ -7,6 +7,10 @@ describe ValidatesTimeliness::Parser do
|
||||
it "should return time object for valid time string" 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
|
||||
@@ -19,12 +23,12 @@ describe ValidatesTimeliness::Parser do
|
||||
it "should return Time object when passed a Time object" do
|
||||
parse(Time.now, :datetime).should be_kind_of(Time)
|
||||
end
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -513,8 +521,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 +625,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 +718,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
|
||||
|
||||
@@ -2,27 +2,27 @@
|
||||
|
||||
Gem::Specification.new do |s|
|
||||
s.name = %q{validates_timeliness}
|
||||
s.version = "2.2.2"
|
||||
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{2009-09-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/version.rb", "lib/validates_timeliness/action_view", "lib/validates_timeliness/action_view/instance_tag.rb", "lib/validates_timeliness/locale", "lib/validates_timeliness/locale/en.yml", "lib/validates_timeliness/validation_methods.rb", "lib/validates_timeliness/matcher.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/parser.rb", "lib/validates_timeliness/formats.rb", "lib/validates_timeliness/validator.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.rb", "spec/validator_spec.rb", "spec/action_view", "spec/action_view/instance_tag_spec.rb", "spec/ginger_scenarios.rb", "spec/spec_helper.rb", "spec/formats_spec.rb", "spec/active_record", "spec/active_record/attribute_methods_spec.rb", "spec/active_record/multiparameter_attributes_spec.rb", "spec/time_travel", "spec/time_travel/time_travel.rb", "spec/time_travel/time_extensions.rb", "spec/time_travel/MIT-LICENSE", "spec/parser_spec.rb", "spec/spec", "spec/spec/rails", "spec/spec/rails/matchers", "spec/spec/rails/matchers/validate_timeliness_spec.rb", "spec/resources", "spec/resources/person.rb", "spec/resources/sqlite_patch.rb", "spec/resources/schema.rb", "spec/resources/application.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.4}
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user