Sha256: 9ceea893cbaa9d311315eade0f6b26bccb28cb40da26da0e9684a447b2d06c34

Contents?: true

Size: 1.35 KB

Versions: 142

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

# Note I did want to put this validator in lib but I could not get it to load
# if put in app/validators. Probably something dumb I was doing, so it could be good
# to move this file later if you can get to the bottom of the issue.
# (I wondered if it was to do with load order - the model loading before the validator -
# although other validators are not exhibiting this issue)
# --------------------------------------------------------------------------------------
# This is a sub-class of the rails InclusionValidator that will use a different
# validation I18n message with `to` and `from` arguments.
#
# Rather than "is not included in the list", you can use the I18n args to build a
# more helpful more helpful message, e.g.:
# activerecord:
#   errors:
#    messages:
#      numeric_inclusion: "must be between %{from} and %{to}"

class NumericInclusionValidator < ActiveModel::Validations::InclusionValidator
  def validate_each(record, attribute, value)
    delimiter = options[:in]
    exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter
    unless exclusions.send(inclusion_method(exclusions), value)
      i18n_args = options.except(:in)
                         .merge!(value: value, from: delimiter.first, to: delimiter.last)
      record.errors.add(attribute, :numeric_inclusion, i18n_args)
    end
  end
end

Version data entries

142 entries across 142 versions & 1 rubygems

Version Path
renalware-core-2.1.1 lib/numeric_inclusion_validator.rb
renalware-core-2.1.0 lib/numeric_inclusion_validator.rb
renalware-core-2.0.167 lib/numeric_inclusion_validator.rb
renalware-core-2.0.166 lib/numeric_inclusion_validator.rb
renalware-core-2.0.165 lib/numeric_inclusion_validator.rb
renalware-core-2.0.164 lib/numeric_inclusion_validator.rb
renalware-core-2.0.163 lib/numeric_inclusion_validator.rb
renalware-core-2.0.162 lib/numeric_inclusion_validator.rb
renalware-core-2.0.161 lib/numeric_inclusion_validator.rb
renalware-core-2.0.160 lib/numeric_inclusion_validator.rb
renalware-core-2.0.159 lib/numeric_inclusion_validator.rb
renalware-core-2.0.158 lib/numeric_inclusion_validator.rb
renalware-core-2.0.157 lib/numeric_inclusion_validator.rb
renalware-core-2.0.156 lib/numeric_inclusion_validator.rb
renalware-core-2.0.155 lib/numeric_inclusion_validator.rb
renalware-core-2.0.153 lib/numeric_inclusion_validator.rb
renalware-core-2.0.152 lib/numeric_inclusion_validator.rb
renalware-core-2.0.151 lib/numeric_inclusion_validator.rb
renalware-core-2.0.149 lib/numeric_inclusion_validator.rb
renalware-core-2.0.148 lib/numeric_inclusion_validator.rb