Sha256: d18b53dd08582ecba95feac977d5794d394c4f2a712906c826891b1fb516d13a

Contents?: true

Size: 749 Bytes

Versions: 1

Compression:

Stored size: 749 Bytes

Contents

class MeasuredValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, measurable)
    measured_config = record.class.measured_fields[attribute]

    measured_class = measured_config[:class]
    measurable_unit = record.send("#{ attribute }_unit")
    measurable_value = record.send("#{ attribute }_value")

    message = options[:message] || "is not a valid unit"

    if options[:units]
      valid_units = [options[:units]].flatten.map{|u| measured_class.conversion.to_unit_name(u) }
      record.errors.add(attribute, message) unless valid_units.include?(measured_class.conversion.to_unit_name(measurable_unit))
    end

    record.errors.add(attribute, message) unless measured_class.valid_unit?(measurable_unit)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
measured-rails-0.0.1 lib/measured/rails/validations.rb