mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-24 23:06:42 +00:00
changed method to check type and compose string for specific type to properly validate
refactored specs
This commit is contained in:
parent
057014fcb2
commit
85ac2bfc69
@ -1,33 +1,46 @@
|
|||||||
module ValidatesTimeliness
|
module ValidatesTimeliness
|
||||||
module MultiparameterAttributes
|
module MultiparameterAttributes
|
||||||
|
|
||||||
def time_array_to_string(time_array)
|
def self.included(base)
|
||||||
"%04d-%02d-%02d %02d:%02d:%02d" % time_array
|
base.alias_method_chain :execute_callstack_for_multiparameter_attributes, :timeliness
|
||||||
|
end
|
||||||
|
|
||||||
|
def time_array_to_string(time_array, type)
|
||||||
|
case type
|
||||||
|
when :time
|
||||||
|
"%02d:%02d:%02d" % time_array[3..5]
|
||||||
|
when :date
|
||||||
|
"%04d-%02d-%02d" % time_array[0..2]
|
||||||
|
when :datetime
|
||||||
|
"%04d-%02d-%02d %02d:%02d:%02d" % time_array
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Overrides AR method to store multiparameter time and dates as
|
# Overrides AR method to store multiparameter time and dates as string
|
||||||
# ISO datetime string for later validation
|
# allowing validation later.
|
||||||
def execute_callstack_for_multiparameter_attributes(callstack)
|
def execute_callstack_for_multiparameter_attributes_with_timeliness(callstack)
|
||||||
errors = []
|
errors = []
|
||||||
callstack.each do |name, values|
|
callstack.each do |name, values|
|
||||||
klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass
|
klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass
|
||||||
if values.empty?
|
if values.empty?
|
||||||
send(name + "=", nil)
|
send(name + "=", nil)
|
||||||
else
|
else
|
||||||
|
column = column_for_attribute(name)
|
||||||
begin
|
begin
|
||||||
value = if Time == klass || Date == klass
|
value = if [:date, :time, :datetime].include?(column.type)
|
||||||
time_array_to_string(values)
|
time_array_to_string(values, column.type)
|
||||||
else
|
else
|
||||||
klass.new(*values)
|
klass.new(*values)
|
||||||
end
|
end
|
||||||
send("#{name}=", value)
|
send(name + "=", value)
|
||||||
rescue => ex
|
rescue => ex
|
||||||
errors << AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name)
|
errors << ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
unless errors.empty?
|
unless errors.empty?
|
||||||
raise MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
|
puts errors.inspect
|
||||||
|
raise ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,37 +1,47 @@
|
|||||||
require File.dirname(__FILE__) + '/spec_helper'
|
require File.dirname(__FILE__) + '/spec_helper'
|
||||||
|
|
||||||
describe ValidatesTimeliness::MultiparameterAttributes do
|
describe ValidatesTimeliness::MultiparameterAttributes do
|
||||||
include ValidatesTimeliness::MultiparameterAttributes
|
def obj
|
||||||
|
@obj ||= Person.new
|
||||||
|
end
|
||||||
|
|
||||||
class AttributeAssignmentError; def initialize(*args); end; end
|
it "should convert array for datetime type into datetime string" do
|
||||||
class MultiparameterAssignmentErrors; def initialize(*args); end; end
|
time_string = obj.time_array_to_string([2000,2,1,9,10,11], :datetime)
|
||||||
|
|
||||||
before do
|
|
||||||
self.class.stub!(:reflect_on_aggregation).and_return(nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should convert time array into string" do
|
|
||||||
time_string = time_array_to_string([2000,2,1,9,10,11])
|
|
||||||
time_string.should == "2000-02-01 09:10:11"
|
time_string.should == "2000-02-01 09:10:11"
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "execute_callstack_for_multiparameter_attributes" do
|
it "should convert array for date type into date string" do
|
||||||
before do
|
time_string = obj.time_array_to_string([2000,2,1,0,0,0], :date)
|
||||||
@date_array = [2000,2,1,9,10,11]
|
time_string.should == "2000-02-01"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should store time string for a Time class column" do
|
it "should convert array for time type into time string" do
|
||||||
self.stub!(:column_for_attribute).and_return( mock('Column', :klass => Time) )
|
time_string = obj.time_array_to_string([2000,1,1,9,10,11], :time)
|
||||||
self.should_receive(:birth_date_and_time=).once.with("2000-02-01 09:10:11")
|
time_string.should == "09:10:11"
|
||||||
callstack = {'birth_date_and_time' => @date_array}
|
end
|
||||||
execute_callstack_for_multiparameter_attributes(callstack)
|
|
||||||
|
describe "execute_callstack_for_multiparameter_attributes" do
|
||||||
|
before do
|
||||||
|
@callstack = {
|
||||||
|
'birth_date_and_time' => [2000,2,1,9,10,11],
|
||||||
|
'birth_date' => [2000,2,1,9,10,11],
|
||||||
|
'birth_time' => [2000,2,1,9,10,11]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should store datetime string for datetime column" do
|
||||||
|
obj.should_receive(:birth_date_and_time=).once.with("2000-02-01 09:10:11")
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should store time string for a Date class column" do
|
it "should store date string for a date column" do
|
||||||
self.stub!(:column_for_attribute).and_return( mock('Column', :klass => Date) )
|
obj.should_receive(:birth_date=).once.with("2000-02-01")
|
||||||
self.should_receive(:birth_date=).once.with("2000-02-01 09:10:11")
|
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
||||||
callstack = {'birth_date' => @date_array}
|
end
|
||||||
execute_callstack_for_multiparameter_attributes(callstack)
|
|
||||||
|
it "should store time string for a time column" do
|
||||||
|
obj.should_receive(:birth_time=).once.with("09:10:11")
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user