date/time select extension

This commit is contained in:
Adam Meehan
2010-08-02 16:58:03 +10:00
parent 924ba64b00
commit fabb2be7af
7 changed files with 259 additions and 2 deletions

View File

@@ -36,6 +36,7 @@ end
require 'validates_timeliness/conversion'
require 'validates_timeliness/validator'
require 'validates_timeliness/helper_methods'
require 'validates_timeliness/extensions'
require 'validates_timeliness/version'
I18n.load_path << File.expand_path(File.dirname(__FILE__) + '/validates_timeliness/locale/en.yml')

View File

@@ -0,0 +1,9 @@
module ValidatesTimeliness
module Extensions
autoload :DateTimeSelect, 'validates_timeliness/extensions/date_time_select'
end
def self.enable_date_time_select_extension!
::ActionView::Helpers::InstanceTag.send(:include, ValidatesTimeliness::Extensions::DateTimeSelect)
end
end

View File

@@ -0,0 +1,45 @@
module ValidatesTimeliness
module Extensions
module DateTimeSelect
extend ActiveSupport::Concern
# Intercepts the date and time select helpers to reuse the values from the
# the params rather than the parsed value. This allows invalid date/time
# values to be redisplayed instead of blanks to aid correction by the user.
# Its a minor usability improvement which is rarely an issue for the user.
included do
alias_method_chain :datetime_selector, :timeliness
alias_method_chain :value, :timeliness
end
module InstanceMethods
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)
unless @timeliness_date_or_time_tag && @template_object.params[@object_name]
return value_without_timeliness(object)
end
pairs = @template_object.params[@object_name].select {|k,v| k =~ /^#{@method_name}\(/ }
return value_without_timeliness(object) if pairs.empty?
values = [nil] * 6
pairs.map do |(param, value)|
position = param.scan(/\(([0-9]*).*\)/).first.first
values[position.to_i-1] = value
end
TimelinessDateTime.new(*values)
end
end
end
end
end