Module: ValidatesTimeliness::ActionView::InstanceTag

Defined in:
lib/validates_timeliness/action_view/instance_tag.rb

Overview

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.

Defined Under Namespace

Classes: TimelinessDateTime

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/validates_timeliness/action_view/instance_tag.rb', line 15

def self.included(base)
  selector_method = Rails::VERSION::STRING < '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

Instance Method Details

#datetime_selector_with_timeliness(*args) ⇒ Object



26
27
28
29
# File 'lib/validates_timeliness/action_view/instance_tag.rb', line 26

def datetime_selector_with_timeliness(*args)
  @timeliness_date_or_time_tag = true
  datetime_selector_without_timeliness(*args)
end

#value_with_timeliness(object) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/validates_timeliness/action_view/instance_tag.rb', line 31

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