Sha256: e68d93917347c31281b80597e90ac641932b88fabab5bd515bd69e1993a33acc

Contents?: true

Size: 1.22 KB

Versions: 17

Compression:

Stored size: 1.22 KB

Contents

module ValidatesTimeliness
  module AttributeMethods
    extend ActiveSupport::Concern

    included do
      class_attribute :timeliness_validated_attributes
      self.timeliness_validated_attributes = []
    end
  end
end

ActiveModel::Type::Date.prepend Module.new {
  def cast_value(value)
    return super unless ValidatesTimeliness.use_plugin_parser

    if value.is_a?(::String)
      return if value.empty?
      value = Timeliness::Parser.parse(value, :date)
      value.to_date if value
    elsif value.respond_to?(:to_date)
      value.to_date
    else
      value
    end
  end
}

ActiveModel::Type::Time.prepend Module.new {
  def user_input_in_time_zone(value)
    return super unless ValidatesTimeliness.use_plugin_parser

    if value.is_a?(String)
      dummy_time_value = value.sub(/\A(\d\d\d\d-\d\d-\d\d |)/, Date.current.to_s + ' ')
      Timeliness::Parser.parse(dummy_time_value, :datetime, zone: :current)
    else
      value.in_time_zone
    end
  end
}

ActiveModel::Type::DateTime.prepend Module.new {
  def user_input_in_time_zone(value)
    if value.is_a?(String) && ValidatesTimeliness.use_plugin_parser
      Timeliness::Parser.parse(value, :datetime, zone: :current)
    else
      value.in_time_zone
    end
  end
}

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
validates_timeliness-8.0.0.beta1 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-7.0.0 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-7.0.0.beta2 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-5.0.1 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-6.0.1 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-7.0.0.beta1 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-6.0.0 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-6.0.0.beta2 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-6.0.0.beta1 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-6.0.0.alpha1 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-5.0.0 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-5.0.0.beta2 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-5.0.0.beta1 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-5.0.0.alpha5 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-5.0.0.alpha4 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-5.0.0.alpha3 lib/validates_timeliness/attribute_methods.rb
validates_timeliness-5.0.0.alpha2 lib/validates_timeliness/attribute_methods.rb