mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
moved mutliparam helper methods our of AR to reduce method pollution
This commit is contained in:
parent
7aa1a87731
commit
e399c6b510
@ -5,37 +5,8 @@ module ValidatesTimeliness
|
||||
end
|
||||
|
||||
module ActiveRecord
|
||||
module MultiparameterAttributes
|
||||
|
||||
def self.included(base)
|
||||
base.alias_method_chain :execute_callstack_for_multiparameter_attributes, :timeliness
|
||||
end
|
||||
|
||||
# Assign dates and times as formatted strings to force the use of the plugin parser
|
||||
# and store a before_type_cast value for attribute
|
||||
def execute_callstack_for_multiparameter_attributes_with_timeliness(callstack)
|
||||
errors = []
|
||||
callstack.each do |name, values|
|
||||
column = column_for_attribute(name)
|
||||
if column && [:date, :time, :datetime].include?(column.type)
|
||||
begin
|
||||
callstack.delete(name)
|
||||
if values.empty?
|
||||
send("#{name}=", nil)
|
||||
else
|
||||
value = time_array_to_string(values, column.type)
|
||||
send("#{name}=", value)
|
||||
end
|
||||
rescue => ex
|
||||
errors << ::ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
unless errors.empty?
|
||||
raise ::ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
|
||||
end
|
||||
execute_callstack_for_multiparameter_attributes_without_timeliness(callstack)
|
||||
end
|
||||
class << self
|
||||
|
||||
def time_array_to_string(values, type)
|
||||
values.collect! {|v| v.to_s }
|
||||
@ -61,5 +32,39 @@ module ValidatesTimeliness
|
||||
|
||||
end
|
||||
|
||||
module MultiparameterAttributes
|
||||
|
||||
def self.included(base)
|
||||
base.alias_method_chain :execute_callstack_for_multiparameter_attributes, :timeliness
|
||||
end
|
||||
|
||||
# Assign dates and times as formatted strings to force the use of the plugin parser
|
||||
# and store a before_type_cast value for attribute
|
||||
def execute_callstack_for_multiparameter_attributes_with_timeliness(callstack)
|
||||
errors = []
|
||||
callstack.each do |name, values|
|
||||
column = column_for_attribute(name)
|
||||
if column && [:date, :time, :datetime].include?(column.type)
|
||||
begin
|
||||
callstack.delete(name)
|
||||
if values.empty?
|
||||
send("#{name}=", nil)
|
||||
else
|
||||
value = ValidatesTimeliness::ActiveRecord.time_array_to_string(values, column.type)
|
||||
send("#{name}=", value)
|
||||
end
|
||||
rescue => ex
|
||||
errors << ::ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
unless errors.empty?
|
||||
raise ::ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
|
||||
end
|
||||
execute_callstack_for_multiparameter_attributes_without_timeliness(callstack)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@ -6,17 +6,17 @@ describe ValidatesTimeliness::ActiveRecord::MultiparameterAttributes do
|
||||
end
|
||||
|
||||
it "should convert array for datetime type into datetime string" do
|
||||
time_string = obj.time_array_to_string([2000,2,1,9,10,11], :datetime)
|
||||
time_string = time_array_to_string([2000,2,1,9,10,11], :datetime)
|
||||
time_string.should == "2000-02-01 09:10:11"
|
||||
end
|
||||
|
||||
it "should convert array for date type into date string" do
|
||||
time_string = obj.time_array_to_string([2000,2,1], :date)
|
||||
time_string = time_array_to_string([2000,2,1], :date)
|
||||
time_string.should == "2000-02-01"
|
||||
end
|
||||
|
||||
it "should convert array for time type into time string" do
|
||||
time_string = obj.time_array_to_string([2000,1,1,9,10,11], :time)
|
||||
time_string = time_array_to_string([2000,1,1,9,10,11], :time)
|
||||
time_string.should == "09:10:11"
|
||||
end
|
||||
|
||||
@ -45,4 +45,8 @@ describe ValidatesTimeliness::ActiveRecord::MultiparameterAttributes do
|
||||
end
|
||||
end
|
||||
|
||||
def time_array_to_string(*args)
|
||||
ValidatesTimeliness::ActiveRecord.time_array_to_string(*args)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user