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.0.147 lib/numeric_inclusion_validator.rb
renalware-core-2.0.146 lib/numeric_inclusion_validator.rb
renalware-core-2.0.145 lib/numeric_inclusion_validator.rb
renalware-core-2.0.144 lib/numeric_inclusion_validator.rb
renalware-core-2.0.143 lib/numeric_inclusion_validator.rb
renalware-core-2.0.142 lib/numeric_inclusion_validator.rb
renalware-core-2.0.141 lib/numeric_inclusion_validator.rb
renalware-core-2.0.140 lib/numeric_inclusion_validator.rb
renalware-core-2.0.139 lib/numeric_inclusion_validator.rb
renalware-core-2.0.138 lib/numeric_inclusion_validator.rb
renalware-core-2.0.137 lib/numeric_inclusion_validator.rb
renalware-core-2.0.136 lib/numeric_inclusion_validator.rb
renalware-core-2.0.135 lib/numeric_inclusion_validator.rb
renalware-core-2.0.134 lib/numeric_inclusion_validator.rb
renalware-core-2.0.133 lib/numeric_inclusion_validator.rb
renalware-core-2.0.132 lib/numeric_inclusion_validator.rb
renalware-core-2.0.131 lib/numeric_inclusion_validator.rb
renalware-core-2.0.130 lib/numeric_inclusion_validator.rb
renalware-core-2.0.129 lib/numeric_inclusion_validator.rb
renalware-core-2.0.128 lib/numeric_inclusion_validator.rb