mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 15:22:58 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
46eafe31a1 | ||
|
|
f17a799b5c | ||
|
|
3bf364a395 | ||
|
|
bfcab52c22 | ||
|
|
20c0aaa793 | ||
|
|
a756b66f75 | ||
|
|
889b5a9b07 | ||
|
|
0e3f56e26d | ||
|
|
7ca662ada8 | ||
|
|
03effb9e52 |
@@ -1,3 +1,10 @@
|
||||
= 3.0.2 [2010-12-04]
|
||||
* Fix AR multiparameter extension for Date columns
|
||||
* Update to Timeliness 0.3.2 for zone abbreviation and offset support
|
||||
|
||||
= 3.0.1 [2010-11-02]
|
||||
* Generate timeliness write methods in an included module to allow overriding in model class (josevalim)
|
||||
|
||||
= 3.0.0 [2010-10-18]
|
||||
* Rails 3 and ActiveModel compatibility
|
||||
* Uses ActiveModel::EachValidator as validator base class.
|
||||
|
||||
26
Gemfile
26
Gemfile
@@ -1,16 +1,20 @@
|
||||
source 'http://rubygems.org'
|
||||
|
||||
gem 'timeliness', '~> 0.1.1'
|
||||
gemspec
|
||||
|
||||
group :test do
|
||||
gem 'ZenTest'
|
||||
gem 'rails', '3.0.0'
|
||||
gem 'sqlite3-ruby', :require => 'sqlite3'
|
||||
gem 'mongoid', '2.0.0.beta.17'
|
||||
gem 'ZenTest'
|
||||
gem 'rails', '3.0.0'
|
||||
gem 'rspec', '>= 2.0.0.beta.17'
|
||||
gem 'rspec-rails', '>= 2.0.0.beta.17'
|
||||
gem 'timecop'
|
||||
gem 'rspec_tag_matchers'
|
||||
gem 'ruby-debug'
|
||||
|
||||
group :mongoid do
|
||||
gem 'mongoid', '2.0.0.beta.19'
|
||||
gem 'bson_ext', '1.0.4'
|
||||
gem 'rspec', '>= 2.0.0.beta.17'
|
||||
gem 'rspec-rails', '>= 2.0.0.beta.17'
|
||||
gem 'timecop'
|
||||
gem 'rspec_tag_matchers'
|
||||
gem 'ruby-debug'
|
||||
end
|
||||
|
||||
group :active_record do
|
||||
gem 'sqlite3-ruby', :require => 'sqlite3'
|
||||
end
|
||||
|
||||
39
Gemfile.lock
39
Gemfile.lock
@@ -1,3 +1,9 @@
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
validates_timeliness (3.0.2)
|
||||
timeliness (~> 0.3.2)
|
||||
|
||||
GEM
|
||||
remote: http://rubygems.org/
|
||||
specs:
|
||||
@@ -31,26 +37,26 @@ GEM
|
||||
activesupport (3.0.0)
|
||||
arel (1.0.1)
|
||||
activesupport (~> 3.0.0)
|
||||
bson (1.0.4)
|
||||
bson (1.1.1)
|
||||
bson_ext (1.0.4)
|
||||
builder (2.1.2)
|
||||
columnize (0.3.1)
|
||||
diff-lcs (1.1.2)
|
||||
erubis (2.6.6)
|
||||
abstract (>= 1.0.0)
|
||||
i18n (0.4.1)
|
||||
i18n (0.4.2)
|
||||
linecache (0.43)
|
||||
mail (2.2.6.1)
|
||||
mail (2.2.9)
|
||||
activesupport (>= 2.3.6)
|
||||
mime-types
|
||||
treetop (>= 1.4.5)
|
||||
i18n (~> 0.4.1)
|
||||
mime-types (~> 1.16)
|
||||
treetop (~> 1.4.8)
|
||||
mime-types (1.16)
|
||||
mongo (1.0.7)
|
||||
bson (>= 1.0.4)
|
||||
mongoid (2.0.0.beta.17)
|
||||
activemodel (~> 3.0.0)
|
||||
bson (= 1.0.4)
|
||||
mongo (= 1.0.7)
|
||||
mongo (1.0.9)
|
||||
bson (>= 1.0.5)
|
||||
mongoid (2.0.0.beta.19)
|
||||
activemodel (~> 3.0)
|
||||
mongo (= 1.0.9)
|
||||
tzinfo (~> 0.3.22)
|
||||
will_paginate (~> 3.0.pre)
|
||||
nokogiri (1.4.3.1)
|
||||
@@ -58,7 +64,7 @@ GEM
|
||||
rack (1.2.1)
|
||||
rack-mount (0.6.13)
|
||||
rack (>= 1.0.0)
|
||||
rack-test (0.5.4)
|
||||
rack-test (0.5.6)
|
||||
rack (>= 1.0)
|
||||
rails (3.0.0)
|
||||
actionmailer (= 3.0.0)
|
||||
@@ -95,9 +101,9 @@ GEM
|
||||
ruby-debug-base (0.10.3)
|
||||
linecache (>= 0.3)
|
||||
sqlite3-ruby (1.3.1)
|
||||
thor (0.14.1)
|
||||
thor (0.14.3)
|
||||
timecop (0.3.5)
|
||||
timeliness (0.1.1)
|
||||
timeliness (0.3.2)
|
||||
treetop (1.4.8)
|
||||
polyglot (>= 0.3.1)
|
||||
tzinfo (0.3.23)
|
||||
@@ -109,7 +115,7 @@ PLATFORMS
|
||||
DEPENDENCIES
|
||||
ZenTest
|
||||
bson_ext (= 1.0.4)
|
||||
mongoid (= 2.0.0.beta.17)
|
||||
mongoid (= 2.0.0.beta.19)
|
||||
rails (= 3.0.0)
|
||||
rspec (>= 2.0.0.beta.17)
|
||||
rspec-rails (>= 2.0.0.beta.17)
|
||||
@@ -117,4 +123,5 @@ DEPENDENCIES
|
||||
ruby-debug
|
||||
sqlite3-ruby
|
||||
timecop
|
||||
timeliness (~> 0.1.1)
|
||||
timeliness (~> 0.3.2)
|
||||
validates_timeliness!
|
||||
|
||||
53
README.rdoc
53
README.rdoc
@@ -18,9 +18,9 @@ If you a looking for the old version for Rails 2.x go here[http://github.com/adz
|
||||
|
||||
* Only Rails date/time validation plugin offering complete validation (See ORM/ODM support)
|
||||
|
||||
* Adds extensions to fix Rails date/time select issues
|
||||
* Adds extensions to fix Rails date/time select issues (See Extensions)
|
||||
|
||||
* Uses extensible date/time parser ({timeliness gem}[http://github.com/adzap/timeliness])
|
||||
* Uses extensible date/time parser (Using {timeliness gem}[http://github.com/adzap/timeliness]. See Plugin Parser)
|
||||
|
||||
* Supports I18n for the error messages
|
||||
|
||||
@@ -31,7 +31,7 @@ As plugin (from master)
|
||||
|
||||
rails plugin install git://github.com/adzap/validates_timeliness.git
|
||||
|
||||
As gem (in beta)
|
||||
As gem
|
||||
|
||||
# in Gemfile
|
||||
gem 'validates_timeliness', '~> 3.0.0'
|
||||
@@ -46,6 +46,8 @@ Then run
|
||||
This creates configuration initializer and locale files. In the initializer, you there are a number of config
|
||||
options to customize the plugin.
|
||||
|
||||
NOTE: You may wish to enable the plugin parser and the extensions to start. Please read those sections first.
|
||||
|
||||
|
||||
== Examples
|
||||
|
||||
@@ -175,7 +177,8 @@ You can also use validation options for custom error messages. The following opt
|
||||
:after_message
|
||||
:on_or_after_message
|
||||
|
||||
Note: There is no :between_message option. The between error message should be defined using the :on_or_before and :on_or_after messages.
|
||||
Note: There is no :between_message option. The between error message should be defined using the
|
||||
:on_or_before and :on_or_after messages.
|
||||
|
||||
It is highly recommended you use the I18n system for error messages.
|
||||
|
||||
@@ -254,36 +257,42 @@ To turn them on/off:
|
||||
config.ignore_restriction_errors = true
|
||||
|
||||
|
||||
=== Display Invalid Values in Select Helpers
|
||||
|
||||
The plugin offers an extension for ActionView to allowing invalid
|
||||
date and time values to be redisplayed to the user as feedback, instead of
|
||||
a blank field which happens by default in Rails. Though the date helpers make this a
|
||||
pretty rare occurrence, given the select dropdowns for each date/time component, but
|
||||
it may be something of interest.
|
||||
|
||||
To activate it, put this in an initializer:
|
||||
|
||||
# in the setup block
|
||||
config.enable_date_time_select_extension!
|
||||
|
||||
== Extensions
|
||||
|
||||
=== Strict Parsing for Select Helpers
|
||||
|
||||
When using date/time select helpers, the component values are handled by ActiveRecord using
|
||||
the Time class to instantiate them into a time value. But this mean that some invalid dates,
|
||||
the Time class to instantiate them into a time value. This means that some invalid dates,
|
||||
such as 31st June, are shifted forward and treated as valid. To handle these cases in a strict
|
||||
way you can enable the plugin handler to treat them as invalid dates.
|
||||
way, you can enable the plugin extension to treat them as invalid dates.
|
||||
|
||||
To activate it, put this in an initializer:
|
||||
To activate it, uncomment this line in the initializer:
|
||||
|
||||
# in the setup block
|
||||
config.enable_multiparameter_extension!
|
||||
|
||||
|
||||
== Credits
|
||||
=== Display Invalid Values in Select Helpers
|
||||
|
||||
* Adam Meehan (adam.meehan@gmail.com, http://github.com/adzap)
|
||||
The plugin offers an extension for ActionView to allowing invalid date and time values to be
|
||||
redisplayed to the user as feedback, instead of a blank field which happens by default in
|
||||
Rails. Though the date helpers make this a pretty rare occurrence, given the select dropdowns
|
||||
for each date/time component, but it may be something of interest.
|
||||
|
||||
To activate it, uncomment this line in the initializer:
|
||||
|
||||
# in the setup block
|
||||
config.enable_date_time_select_extension!
|
||||
|
||||
|
||||
== Contributors
|
||||
|
||||
To see the generous people who have contributed code, take a look at the {contributors list}[http://github.com/adzap/validates_timeliness/contributors].
|
||||
|
||||
|
||||
== Maintainers
|
||||
|
||||
* {Adam Meehan}[http://github.com/adzap]
|
||||
|
||||
|
||||
== License
|
||||
|
||||
2
Rakefile
2
Rakefile
@@ -22,7 +22,7 @@ spec = Gem::Specification.new do |s|
|
||||
s.homepage = "http://github.com/adzap/validates_timeliness"
|
||||
s.require_path = 'lib'
|
||||
s.files = %w(validates_timeliness.gemspec LICENSE CHANGELOG.rdoc README.rdoc Rakefile) + Dir.glob("{lib,spec}/**/*")
|
||||
s.add_runtime_dependency 'timeliness', '~> 0.1.0'
|
||||
s.add_runtime_dependency 'timeliness', '~> 0.3.2'
|
||||
end
|
||||
|
||||
desc 'Default: run specs.'
|
||||
|
||||
@@ -42,7 +42,7 @@ module ValidatesTimeliness
|
||||
super
|
||||
end
|
||||
EOV
|
||||
class_eval(method_body, __FILE__, line)
|
||||
generated_timeliness_methods.module_eval(method_body, __FILE__, line)
|
||||
end
|
||||
|
||||
def define_timeliness_before_type_cast_method(attr_name)
|
||||
@@ -51,7 +51,11 @@ module ValidatesTimeliness
|
||||
_timeliness_raw_value_for('#{attr_name}')
|
||||
end
|
||||
EOV
|
||||
class_eval(method_body, __FILE__, line)
|
||||
generated_timeliness_methods.module_eval(method_body, __FILE__, line)
|
||||
end
|
||||
|
||||
def generated_timeliness_methods
|
||||
@generated_timeliness_methods ||= Module.new.tap { |m| include(m) }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ module ValidatesTimeliness
|
||||
|
||||
included do
|
||||
alias_method_chain :datetime_selector, :timeliness
|
||||
alias_method_chain :value, :timeliness
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
@@ -22,13 +21,13 @@ module ValidatesTimeliness
|
||||
datetime_selector_without_timeliness(*args)
|
||||
end
|
||||
|
||||
def value_with_timeliness(object)
|
||||
def value(object)
|
||||
unless @timeliness_date_or_time_tag && @template_object.params[@object_name]
|
||||
return value_without_timeliness(object)
|
||||
return super
|
||||
end
|
||||
|
||||
pairs = @template_object.params[@object_name].select {|k,v| k =~ /^#{@method_name}\(/ }
|
||||
return value_without_timeliness(object) if pairs.empty?
|
||||
return super if pairs.empty?
|
||||
|
||||
values = [nil] * 6
|
||||
pairs.map do |(param, value)|
|
||||
|
||||
@@ -3,26 +3,64 @@ module ValidatesTimeliness
|
||||
module MultiparameterHandler
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
# Stricter handling of date and time values from multiparameter
|
||||
# assignment from the date/time select view helpers
|
||||
|
||||
included do
|
||||
alias_method_chain :instantiate_time_object, :timeliness
|
||||
alias_method_chain :execute_callstack_for_multiparameter_attributes, :timeliness
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Stricter handling of date and time values from multiparameter
|
||||
# assignment from the date/time select view helpers
|
||||
#
|
||||
def instantiate_time_object_with_timeliness(name, values)
|
||||
unless Date.valid_civil?(*values[0..2])
|
||||
value = [values[0], *values[1..2].map {|s| s.to_s.rjust(2,"0")} ].join("-")
|
||||
value += ' ' + values[3..5].map {|s| s.to_s.rjust(2, "0") }.join(":") unless values[3..5].empty?
|
||||
return value
|
||||
end
|
||||
def invalid_multiparameter_date_or_time_as_string(values)
|
||||
value = [values[0], *values[1..2].map {|s| s.to_s.rjust(2,"0")} ].join("-")
|
||||
value += ' ' + values[3..5].map {|s| s.to_s.rjust(2, "0") }.join(":") unless values[3..5].empty?
|
||||
value
|
||||
end
|
||||
|
||||
if self.class.send(:create_time_zone_conversion_attribute?, name, column_for_attribute(name))
|
||||
Time.zone.local(*values)
|
||||
def instantiate_time_object_with_timeliness(name, values)
|
||||
if Date.valid_civil?(*values[0..2])
|
||||
instantiate_time_object_without_timeliness(name, values)
|
||||
else
|
||||
Time.time_with_datetime_fallback(self.class.default_timezone, *values)
|
||||
invalid_multiparameter_date_or_time_as_string(values)
|
||||
end
|
||||
end
|
||||
|
||||
def instantiate_date_object(name, values)
|
||||
values = values.map { |v| v.nil? ? 1 : v }
|
||||
Date.new(*values)
|
||||
rescue ArgumentError => ex
|
||||
invalid_multiparameter_date_or_time_as_string(values)
|
||||
end
|
||||
|
||||
def execute_callstack_for_multiparameter_attributes_with_timeliness(callstack)
|
||||
errors = []
|
||||
callstack.each do |name, values_with_empty_parameters|
|
||||
begin
|
||||
klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass
|
||||
values = values_with_empty_parameters.reject { |v| v.nil? }
|
||||
|
||||
if values.empty?
|
||||
send(name + "=", nil)
|
||||
else
|
||||
|
||||
value = if Time == klass
|
||||
instantiate_time_object(name, values)
|
||||
elsif Date == klass
|
||||
instantiate_date_object(name, values_with_empty_parameters)
|
||||
else
|
||||
klass.new(*values)
|
||||
end
|
||||
|
||||
send(name + "=", value)
|
||||
end
|
||||
rescue => ex
|
||||
errors << ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name)
|
||||
end
|
||||
end
|
||||
unless errors.empty?
|
||||
raise ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
module ValidatesTimeliness
|
||||
VERSION = '3.0.0'
|
||||
VERSION = '3.0.2'
|
||||
end
|
||||
|
||||
@@ -75,6 +75,13 @@ class Employee < ActiveRecord::Base
|
||||
validates_time :birth_time
|
||||
validates_datetime :birth_datetime
|
||||
define_attribute_methods
|
||||
|
||||
attr_accessor :redefined_birth_date_called
|
||||
|
||||
def birth_date=(value)
|
||||
self.redefined_birth_date_called = true
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
Rspec.configure do |c|
|
||||
|
||||
@@ -34,6 +34,12 @@ describe ValidatesTimeliness::AttributeMethods do
|
||||
r._timeliness_raw_value_for(:birth_datetime).should == date_string
|
||||
end
|
||||
|
||||
it 'should not overwrite user defined methods' do
|
||||
e = Employee.new
|
||||
e.birth_date = '2010-01-01'
|
||||
e.redefined_birth_date_called.should be_true
|
||||
end
|
||||
|
||||
context "with plugin parser" do
|
||||
class PersonWithParser
|
||||
include TestModel
|
||||
|
||||
@@ -3,19 +3,31 @@ require 'spec_helper'
|
||||
describe ValidatesTimeliness::Extensions::MultiparameterHandler do
|
||||
let(:employee) { Employee.new }
|
||||
|
||||
it 'should return string value for invalid dates' do
|
||||
instantiate_time_object('birth_date', [2000, 2, 31]).should == '2000-02-31'
|
||||
context "time column" do
|
||||
it 'should return string value for invalid date portion' do
|
||||
multiparameter_attribute(:birth_datetime, [2000, 2, 31, 12, 0, 0])
|
||||
employee.birth_datetime_before_type_cast.should == '2000-02-31 12:00:00'
|
||||
end
|
||||
|
||||
it 'should return Time value for valid datetimes' do
|
||||
multiparameter_attribute(:birth_datetime, [2000, 2, 28, 12, 0, 0])
|
||||
employee.birth_datetime_before_type_cast.should be_kind_of(Time)
|
||||
end
|
||||
end
|
||||
|
||||
it 'should return string value for invalid datetimes' do
|
||||
instantiate_time_object('birth_datetime', [2000, 2, 31, 12, 0, 0]).should == '2000-02-31 12:00:00'
|
||||
end
|
||||
|
||||
it 'should return Time value for valid datetimes' do
|
||||
instantiate_time_object('birth_datetime', [2000, 2, 28, 12, 0, 0]).should be_kind_of(Time)
|
||||
context "date column" do
|
||||
it 'should return string value for invalid date' do
|
||||
multiparameter_attribute(:birth_date, [2000, 2, 31])
|
||||
employee.birth_date_before_type_cast.should == '2000-02-31'
|
||||
end
|
||||
|
||||
it 'should return Date value for valid date' do
|
||||
multiparameter_attribute(:birth_date, [2000, 2, 28])
|
||||
employee.birth_date_before_type_cast.should be_kind_of(Date)
|
||||
end
|
||||
end
|
||||
|
||||
def instantiate_time_object(name, values)
|
||||
employee.send(:instantiate_time_object, name, values)
|
||||
def multiparameter_attribute(name, values)
|
||||
employee.send(:execute_callstack_for_multiparameter_attributes, name.to_s => values)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
Gem::Specification.new do |s|
|
||||
s.name = %q{validates_timeliness}
|
||||
s.version = "3.0.0"
|
||||
s.version = "3.0.2"
|
||||
|
||||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
||||
s.authors = ["Adam Meehan"]
|
||||
s.date = %q{2010-10-18}
|
||||
s.date = %q{2010-12-04}
|
||||
s.description = %q{Date and time validation plugin for Rails which allows custom formats}
|
||||
s.email = %q{adam.meehan@gmail.com}
|
||||
s.extra_rdoc_files = ["README.rdoc", "CHANGELOG.rdoc", "LICENSE"]
|
||||
@@ -22,11 +22,11 @@ Gem::Specification.new do |s|
|
||||
s.specification_version = 3
|
||||
|
||||
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
||||
s.add_runtime_dependency(%q<timeliness>, ["~> 0.1.0"])
|
||||
s.add_runtime_dependency(%q<timeliness>, ["~> 0.3.2"])
|
||||
else
|
||||
s.add_dependency(%q<timeliness>, ["~> 0.1.0"])
|
||||
s.add_dependency(%q<timeliness>, ["~> 0.3.2"])
|
||||
end
|
||||
else
|
||||
s.add_dependency(%q<timeliness>, ["~> 0.1.0"])
|
||||
s.add_dependency(%q<timeliness>, ["~> 0.3.2"])
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user