Sha256: 38d6019db573c98326c8772dbddb6a7ef3a41092c7da58681ed2d1c17e42790c
Contents?: true
Size: 1.44 KB
Versions: 3
Compression:
Stored size: 1.44 KB
Contents
# frozen_string_literal: true require "active_record_doctor/detectors/base" module ActiveRecordDoctor module Detectors class IncorrectBooleanPresenceValidation < Base # :nodoc: @description = "detect presence (instead of inclusion) validators on boolean columns" @config = { ignore_models: { description: "models whose validators should not be checked", global: true }, ignore_attributes: { description: "attributes, written as Model.attribute, whose validators should not be checked" } } private def message(model:, attribute:) # rubocop:disable Layout/LineLength "replace the `presence` validator on #{model}.#{attribute} with `inclusion` - `presence` can't be used on booleans" # rubocop:enable Layout/LineLength end def detect each_model(except: config(:ignore_models), existing_tables_only: true) do |model| each_attribute(model, except: config(:ignore_attributes)) do |column| next unless column.type == :boolean next unless has_presence_validator?(model, column) problem!(model: model.name, attribute: column.name) end end end def has_presence_validator?(model, column) model.validators.any? do |validator| validator.kind == :presence && validator.attributes.include?(column.name.to_sym) end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems