Sha256: 254b8e4dcfdd4b8ba605d8ef94cec91c8cbb99c37ef4bbd1196d36001f6ed86e

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# Attribute ordering
#   Ensures one value is greater or lesser than another (set of) value(s).
#   Always skips over nil values; use :presence to validate those.
# eg: validates :start_at, :before=>:finish_at
#     validates :start_at, :before=>{:value_of=>:finish_at, :if=>... }
#     validates :finish_at, :after => [:start_at, :alt_start_at]
#     validates :finish_at, :after=>{:values_of => [:start_at, :alt_start_at], :if=>... }

module ActiveModel::Validations
  class BeforeValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, value)
      compare_to = Array(options[:value_of] || options[:values_of] || options[:in] || options[:with])
      compare_to.each do |attr_name|
        greater = record.send attr_name
        next unless value && greater
        unless value < greater
          attr2 = record.class.human_attribute_name attr_name
          record.errors.add(attribute, :before, options.except(:before).merge!(:attribute2=>attr2))
        end
      end
    end
  end
  class AfterValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, value)
      compare_to = Array(options[:value_of] || options[:values_of] || options[:in] || options[:with])
      compare_to.each do |attr_name|
        lesser = record.send attr_name
        next unless value && lesser
        unless value > lesser
          attr2 = record.class.human_attribute_name attr_name
          record.errors.add(attribute, :after, options.except(:after).merge!(:attribute2=>attr2))
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
can_has_validations-0.2.0 lib/can_has_validations/validators/ordering_validator.rb