Sha256: d0d16dd67ebe9fe81912a134ed5e81290a1de053fd2bd318e94b98820fc9acba
Contents?: true
Size: 1.41 KB
Versions: 2
Compression:
Stored size: 1.41 KB
Contents
# frozen_string_literal: true require_relative '../cop' module ActiveRecord module DataIntegrity module BelongsTo # Checks foreign key presence to the parent table of belongs_to association class ForeignKey < ActiveRecord::DataIntegrity::Cop def call results = associations.map do |association| valid?(association) end results.none?(&:!) end private def valid?(association) success = foreign_key?(association) unless success to_table = association.class_name.constantize.table_name log("belongs_to #{association.name} but has no foreign key to #{to_table}.id") end progress(success, 'F') success rescue NameError log("Error processing #{model.name}.#{association.name}") end def associations model ._reflections .values .select { |association| association.is_a?(ActiveRecord::Reflection::BelongsToReflection) } .reject(&:polymorphic?) end def foreign_key?(association) to_table = association.class_name.constantize.table_name connection.foreign_keys(model.table_name).any? do |foreign_key| foreign_key.to_table == to_table && foreign_key.options.fetch(:primary_key) == 'id' end end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems