Sha256: c6a684c16111c97fd443959f7991187e8c30220c90b76639620874783f71dd49

Contents?: true

Size: 825 Bytes

Versions: 1

Compression:

Stored size: 825 Bytes

Contents

# frozen_string_literal: true

class ReferenceValidator < BaseValidator

  REGEXP = /\A[+-]?\d+\z/

  private

  def valid?
    (valid_id? && valid_type?) || valid_object?
  end

  def valid_id?
    if value.blank?
      false
    elsif !valid_regexp?
      record.errors.add(attribute, *error_message)
    else
      !value.to_i.negative?
    end
  end

  def valid_object?
    association_type = options[:association] || attribute.to_s.chomp('_id')
    association_object = record.send(association_type)
    return true if association_object.present? && association_object.valid?

    record.errors.add(association_type, *error_message)
  end

  def valid_type?
    return true unless options[:polymorphic]

    association_type = "#{attribute.to_s.chomp('_id')}_type"
    record.send(association_type).present?
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lite-validators-1.7.2 lib/lite/validators/reference_validator.rb