Sha256: 2718caa97d58db2592189d73cda65c9ec2672c6e87efd267700ed17e21feac6a

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

module ValidatesTimeliness

  def self.enable_datetime_select_invalid_value_extension!
    ::ActionView::Helpers::InstanceTag.send(:include, ValidatesTimeliness::ActionView::InstanceTag)
  end

  module ActionView

    # 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    
    
      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
      
      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 = ParseDate.parsedate(raw_value)
        
        TimelinessDateTime.new(*time_array[0..5])
      end      
       
    end

  end   
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
adzap-validates_timeliness-1.1.6 lib/validates_timeliness/action_view/instance_tag.rb
adzap-validates_timeliness-1.1.7 lib/validates_timeliness/action_view/instance_tag.rb
validates_timeliness-1.1.7 lib/validates_timeliness/action_view/instance_tag.rb
validates_timeliness-1.1.6 lib/validates_timeliness/action_view/instance_tag.rb