Class: ForeignKeyChecker::Utils::BelongsTo::TableLevel

Inherits:
Object
  • Object
show all
Defined in:
lib/foreign_key_checker/utils/belongs_to.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, table, columns_of_table, polymorphic_suffixes, fks:, on_error: proc { }) ⇒ TableLevel

Returns a new instance of TableLevel.



188
189
190
191
192
193
194
195
196
# File 'lib/foreign_key_checker/utils/belongs_to.rb', line 188

def initialize(connection, table, columns_of_table, polymorphic_suffixes, fks:, on_error: proc { } )
  @connection = connection
  @table = table
  @columns = columns_of_table[table]
  @columns_of_table = columns_of_table
  @polymorphic_suffixes = polymorphic_suffixes
  @on_error = on_error
  @fks = fks
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



187
188
189
# File 'lib/foreign_key_checker/utils/belongs_to.rb', line 187

def columns
  @columns
end

#columns_of_tableObject (readonly)

Returns the value of attribute columns_of_table.



187
188
189
# File 'lib/foreign_key_checker/utils/belongs_to.rb', line 187

def columns_of_table
  @columns_of_table
end

#connectionObject (readonly)

Returns the value of attribute connection.



187
188
189
# File 'lib/foreign_key_checker/utils/belongs_to.rb', line 187

def connection
  @connection
end

#fksObject (readonly)

Returns the value of attribute fks.



187
188
189
# File 'lib/foreign_key_checker/utils/belongs_to.rb', line 187

def fks
  @fks
end

#polymorphic_suffixesObject (readonly)

Returns the value of attribute polymorphic_suffixes.



187
188
189
# File 'lib/foreign_key_checker/utils/belongs_to.rb', line 187

def polymorphic_suffixes
  @polymorphic_suffixes
end

#tableObject (readonly)

Returns the value of attribute table.



187
188
189
# File 'lib/foreign_key_checker/utils/belongs_to.rb', line 187

def table
  @table
end

Instance Method Details

#candidatesObject



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/foreign_key_checker/utils/belongs_to.rb', line 202

def candidates
  columns.select { |column| column.name.ends_with?(foreign_key_suffix) }.each_with_object([]) do |column, object|
    level = ColumnLevel.new(self, column)
    level.errors.each(&@on_error)
    [level.association].compact.each do |ass|
      next if fks.find { |fk| ass.conflict_with_fk?(fk) }

      object.push(ass)
    end
  end
end

#foreign_key_suffixObject



198
199
200
# File 'lib/foreign_key_checker/utils/belongs_to.rb', line 198

def foreign_key_suffix
  '_id'
end