Sha256: a65633f067b3f9f08040f101cac00c7e76057fbae91dc6cf71854f8cc1c2fa80

Contents?: true

Size: 1.21 KB

Versions: 187

Compression:

Stored size: 1.21 KB

Contents

class Lalala::Pages::ChildrenLengthValidator < ActiveModel::Validations::LengthValidator

  def check_validity!
    keys = CHECKS.keys & options.keys

    if keys.empty?
      raise ArgumentError, 'Range unspecified. Specify the :in, :within, :maximum, :minimum, or :is option.'
    end

    keys.each do |key|
      value = options[key]

      if value.is_a?(Proc)
        next
      end

      if value.is_a?(Integer) && value >= 0
        next
      end

      raise ArgumentError, ":#{key} must be a nonnegative Integer"
    end
  end

  def validate_each(record, attribute, value)
    value = tokenize(value)
    value_length = value.respond_to?(:length) ? value.length : value.to_s.length

    CHECKS.each do |key, validity_check|
      check_value = options[key]
      check_value = check_value.call(record) if Proc === check_value
      next unless check_value
      next if value_length.send(validity_check, check_value)

      errors_options = options.except(*RESERVED_OPTIONS)
      errors_options[:count] = check_value

      default_message = options[MESSAGES[key]]
      errors_options[:message] ||= default_message if default_message

      record.errors.add(attribute, MESSAGES[key], errors_options)
    end
  end

end

Version data entries

187 entries across 187 versions & 1 rubygems

Version Path
lalala-4.1.0.dev.425 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.420 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.419 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.418 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.417 lib/lalala/pages/children_length_validator.rb
lalala-4.0.0.dev.416 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.415 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.414 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.413 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.412 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.411 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.410 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.409 lib/lalala/pages/children_length_validator.rb
lalala-4.0.0.dev.407 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.408 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.398 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.397 lib/lalala/pages/children_length_validator.rb
lalala-4.0.0.dev.396 lib/lalala/pages/children_length_validator.rb
lalala-4.0.0.dev.395 lib/lalala/pages/children_length_validator.rb
lalala-4.1.0.dev.394 lib/lalala/pages/children_length_validator.rb