Class: RescueUniqueConstraint::RescueHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rescue_unique_constraint/rescue_handler.rb

Overview

Handles storing and matching [index, field] pairs to exceptions

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ RescueHandler

Returns a new instance of RescueHandler.



4
5
6
7
# File 'lib/rescue_unique_constraint/rescue_handler.rb', line 4

def initialize(model)
  @model = model
  @indexes_to_rescue_on = []
end

Instance Method Details

#add_index(index, field, scope) ⇒ Object



9
10
11
# File 'lib/rescue_unique_constraint/rescue_handler.rb', line 9

def add_index(index, field, scope)
  indexes_to_rescue_on << Index.new(index, field, scope)
end

#matching_indexes(e) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rescue_unique_constraint/rescue_handler.rb', line 13

def matching_indexes(e)
  indexes = indexes_to_rescue_on.select do |index|
    database_adapter.index_error?(index, e.message)
  end
  raise e unless indexes.any?
  indexes
end