mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
date/time select extension
This commit is contained in:
@@ -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')
|
||||
|
||||
9
lib/validates_timeliness/extensions.rb
Normal file
9
lib/validates_timeliness/extensions.rb
Normal 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
|
||||
45
lib/validates_timeliness/extensions/date_time_select.rb
Normal file
45
lib/validates_timeliness/extensions/date_time_select.rb
Normal 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
|
||||
Reference in New Issue
Block a user