Sha256: fc4e84d5a197f4b9961b2b29bfd2a62b42348faae07501e3c8d502755a7896e6

Contents?: true

Size: 1.02 KB

Versions: 9

Compression:

Stored size: 1.02 KB

Contents

module Tuning
  module Validations
    extend ActiveSupport::Concern

    class TimeValidator < ActiveModel::EachValidator
      CHECKS = { before: :<, before_or_equal_to: :<=, after: :>, after_or_equal_to: :>= }

      def validate_each(record, attribute, value)
        if [Date, Time].any? { |klass| value.kind_of?(klass) }
          CHECKS.each do |name, operator|
            if options.has_key?(name)
              other = options[name]
              case other
              when Symbol
                other = record.send(other)
              when Proc
                other = other.call(record)
              end
              unless value.send(operator, other)
                record.errors.add attribute, name, time: I18n.l(other)
              end
            end
          end
        else
          record.errors.add attribute, :not_a_time
        end
      end

    end
    module ClassMethods

      def validates_time_of(*attr_names)
        validates_with TimeValidator, _merge_attributes(attr_names)
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
tuning-5.1.3 lib/tuning/validations/time.rb
tuning-5.1.2 lib/tuning/validations/time.rb
tuning-5.1.0 lib/tuning/validations/time.rb
tuning-4.0.1.2 lib/tuning/validations/time.rb
tuning-4.0.1.1 lib/tuning/validations/time.rb
tuning-4.0.1.0 lib/tuning/validations/time.rb
tuning-4.0.0.1 lib/tuning/validations/time.rb
tuning-4.0.0.0 lib/tuning/validations/time.rb
tuning-1.0.0 lib/tuning/validations/time.rb