mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 14:27:52 +00:00
moved mutliparam helper methods our of AR to reduce method pollution
This commit is contained in:
parent
7aa1a87731
commit
e399c6b510
@ -5,6 +5,33 @@ module ValidatesTimeliness
|
||||
end
|
||||
|
||||
module ActiveRecord
|
||||
|
||||
class << self
|
||||
|
||||
def time_array_to_string(values, type)
|
||||
values.collect! {|v| v.to_s }
|
||||
|
||||
case type
|
||||
when :date
|
||||
extract_date_from_multiparameter_attributes(values)
|
||||
when :time
|
||||
extract_time_from_multiparameter_attributes(values)
|
||||
when :datetime
|
||||
extract_date_from_multiparameter_attributes(values) + " " + extract_time_from_multiparameter_attributes(values)
|
||||
end
|
||||
end
|
||||
|
||||
def extract_date_from_multiparameter_attributes(values)
|
||||
year = ValidatesTimeliness::Formats.unambiguous_year(values[0].rjust(2, "0"))
|
||||
[year, *values.slice(1, 2).map { |s| s.rjust(2, "0") }].join("-")
|
||||
end
|
||||
|
||||
def extract_time_from_multiparameter_attributes(values)
|
||||
values[3..5].map { |s| s.rjust(2, "0") }.join(":")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
module MultiparameterAttributes
|
||||
|
||||
def self.included(base)
|
||||
@ -23,7 +50,7 @@ module ValidatesTimeliness
|
||||
if values.empty?
|
||||
send("#{name}=", nil)
|
||||
else
|
||||
value = time_array_to_string(values, column.type)
|
||||
value = ValidatesTimeliness::ActiveRecord.time_array_to_string(values, column.type)
|
||||
send("#{name}=", value)
|
||||
end
|
||||
rescue => ex
|
||||
@ -37,28 +64,6 @@ module ValidatesTimeliness
|
||||
execute_callstack_for_multiparameter_attributes_without_timeliness(callstack)
|
||||
end
|
||||
|
||||
def time_array_to_string(values, type)
|
||||
values.collect! {|v| v.to_s }
|
||||
|
||||
case type
|
||||
when :date
|
||||
extract_date_from_multiparameter_attributes(values)
|
||||
when :time
|
||||
extract_time_from_multiparameter_attributes(values)
|
||||
when :datetime
|
||||
extract_date_from_multiparameter_attributes(values) + " " + extract_time_from_multiparameter_attributes(values)
|
||||
end
|
||||
end
|
||||
|
||||
def extract_date_from_multiparameter_attributes(values)
|
||||
year = ValidatesTimeliness::Formats.unambiguous_year(values[0].rjust(2, "0"))
|
||||
[year, *values.slice(1, 2).map { |s| s.rjust(2, "0") }].join("-")
|
||||
end
|
||||
|
||||
def extract_time_from_multiparameter_attributes(values)
|
||||
values[3..5].map { |s| s.rjust(2, "0") }.join(":")
|
||||
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
|
||||
|
||||
@ -44,5 +44,9 @@ describe ValidatesTimeliness::ActiveRecord::MultiparameterAttributes do
|
||||
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
||||
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