Compare commits

..

4 Commits
3.0.1 ... 3.0.2

Author SHA1 Message Date
Adam Meehan
46eafe31a1 version 3.0.2 2010-12-04 16:40:13 +11:00
Adam Meehan
f17a799b5c updated Timeliness to 0.3.2 for zone and offset in string support 2010-12-04 16:34:34 +11:00
Adam Meehan
3bf364a395 reluctantly overriding whole execute_callstack_for_multiparameter_attributes method
this fixes issue for Date column types. Damn method is just too
unwieldly and should be refactored in Rails.
2010-12-04 16:29:05 +11:00
Adam Meehan
bfcab52c22 can use super for some InstanceTag methods
cleans up for DateTimeSelect extension a little
2010-11-02 10:13:13 +11:00
9 changed files with 106 additions and 53 deletions

View File

@@ -1,3 +1,7 @@
= 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)

View File

@@ -11,10 +11,10 @@ gem 'rspec_tag_matchers'
gem 'ruby-debug'
group :mongoid do
gem 'mongoid', '2.0.0.beta.17'
gem 'mongoid', '2.0.0.beta.19'
gem 'bson_ext', '1.0.4'
end
group :active_record do
gem 'sqlite3-ruby', :require => 'sqlite3'
end
end

View File

@@ -1,8 +1,8 @@
PATH
remote: .
specs:
validates_timeliness (3.0.0)
timeliness (~> 0.1.0)
validates_timeliness (3.0.2)
timeliness (~> 0.3.2)
GEM
remote: http://rubygems.org/
@@ -37,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)
@@ -64,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)
@@ -101,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)
@@ -115,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)
@@ -123,5 +123,5 @@ DEPENDENCIES
ruby-debug
sqlite3-ruby
timecop
timeliness (~> 0.1.0)
timeliness (~> 0.3.2)
validates_timeliness!

View File

@@ -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.'

View File

@@ -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)|

View File

@@ -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

View File

@@ -1,3 +1,3 @@
module ValidatesTimeliness
VERSION = '3.0.1'
VERSION = '3.0.2'
end

View File

@@ -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

View File

@@ -2,11 +2,11 @@
Gem::Specification.new do |s|
s.name = %q{validates_timeliness}
s.version = "3.0.1"
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-11-02}
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