Sha256: c681f0cb64939709d67f89f437ace21bde60ca9cf6d01e74a427857bb1cb3c78

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

require 'active_support'
require 'active_support/core_ext/object/try'
require "rescue_from_duplicate/active_record/version"

module RescueFromDuplicate
  module ActiveRecord
  end

  def self.missing_unique_indexes
    ar_subclasses = ObjectSpace.each_object(Class).select{ |klass| klass < ::ActiveRecord::Base }
    klasses = ar_subclasses.select do |klass|
      klass.validators.any? { |v| v.is_a?(::ActiveRecord::Validations::UniquenessValidator) || klass._rescue_from_duplicates.any? }
    end

    missing_unique_indexes = []

    klasses.each do |klass|
      klass._rescue_from_duplicate_handlers.each do |handler|
        unique_indexes = klass.connection.indexes(klass.table_name).select(&:unique)

        unless unique_indexes.any? { |index| index.columns.map(&:to_s).sort == handler.columns }
          missing_unique_indexes << MissingUniqueIndex.new(klass, handler.attributes, handler.columns)
        end
      end
    end
    missing_unique_indexes
  end
end

require 'rescue_from_duplicate/active_record/extension'
require 'rescue_from_duplicate/uniqueness_rescuer'
require 'rescue_from_duplicate/rescuer'
require 'rescue_from_duplicate/missing_unique_index'

ActiveSupport.on_load(:active_record) do
  ::ActiveRecord::Base.send :include, RescueFromDuplicate::ActiveRecord::Extension
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord-rescue_from_duplicate-0.1.0 lib/rescue_from_duplicate/active_record.rb
activerecord-rescue_from_duplicate-0.0.7 lib/rescue_from_duplicate/active_record.rb