changed format of string used in multi param for invalid or partial values

This commit is contained in:
Adam Meehan
2010-01-12 22:05:14 +11:00
parent f1a0016bf7
commit 82c0e1bcd3
5 changed files with 150 additions and 56 deletions

View File

@@ -6,43 +6,49 @@ module ValidatesTimeliness
module ActionView
# Intercepts the date and time select helpers to allow the
# Intercepts the date and time select helpers to allow the
# attribute value before type cast to be used as in the select helpers.
# This means that an invalid date or time will be redisplayed rather than the
# type cast value which would be nil if invalid.
module InstanceTag
#
# Its a minor user experience improvement to be able to see original value
# entered to aid correction.
#
module InstanceTag
def self.included(base)
selector_method = Rails::VERSION::STRING < '2.2' ? :date_or_time_select : :datetime_selector
selector_method = Rails::VERSION::STRING.to_f < 2.2 ? :date_or_time_select : :datetime_selector
base.class_eval do
alias_method :datetime_selector_without_timeliness, selector_method
alias_method selector_method, :datetime_selector_with_timeliness
end
base.alias_method_chain :value, :timeliness
end
TimelinessDateTime = Struct.new(:year, :month, :day, :hour, :min, :sec)
TimelinessDateTime = Struct.new(:year, :month, :day, :hour, :min, :sec)
def datetime_selector_with_timeliness(*args)
@timeliness_date_or_time_tag = true
datetime_selector_without_timeliness(*args)
end
def value_with_timeliness(object)
return value_without_timeliness(object) unless @timeliness_date_or_time_tag
raw_value = value_before_type_cast(object)
if raw_value.nil? || raw_value.acts_like?(:time) || raw_value.is_a?(Date)
return value_without_timeliness(object)
end
time_array = ValidatesTimeliness::Formats.parse(raw_value, :datetime)
TimelinessDateTime.new(*time_array[0..5])
end
date, time = raw_value.split(' ')
date_array = date.split('-')
time_array = time.split(':')
TimelinessDateTime.new(*(date_array + time_array).map {|v| v.blank? ? nil : v.to_i})
end
end
end
end
end

View File

@@ -22,24 +22,23 @@ module ValidatesTimeliness
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("-")
year = values[0].blank? ? nil : ValidatesTimeliness::Formats.unambiguous_year(values[0].rjust(2, "0"))
[year, *values.slice(1, 2).map { |s| s.blank? ? nil : s.rjust(2, "0") }].join("-")
end
def extract_time_from_multiparameter_attributes(values)
values[3..5].map { |s| s.rjust(2, "0") }.join(":")
values[3..5].map { |s| s.blank? ? nil : s.rjust(2, "0") }.join(":")
end
end
module MultiparameterAttributes
def self.included(base)
base.alias_method_chain :execute_callstack_for_multiparameter_attributes, :timeliness
end
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|
@@ -47,7 +46,7 @@ module ValidatesTimeliness
if column && [:date, :time, :datetime].include?(column.type)
begin
callstack.delete(name)
if values.empty?
if values.empty? || values.all?(&:nil?)
send("#{name}=", nil)
else
value = ValidatesTimeliness::ActiveRecord.time_array_to_string(values, column.type)
@@ -63,7 +62,7 @@ module ValidatesTimeliness
end
execute_callstack_for_multiparameter_attributes_without_timeliness(callstack)
end
end
end