Module: DatabaseConsistency::Helper

Defined in:
lib/database_consistency/helper.rb

Overview

The module contains helper methods

Class Method Summary collapse

Class Method Details

.check_inclusion?(array, element) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/database_consistency/helper.rb', line 36

def check_inclusion?(array, element)
  array.include?(element.to_s) || array.include?(element.to_sym)
end

.find_field(model, attribute) ⇒ Object

Find a database field with name equals to attribute



24
25
26
# File 'lib/database_consistency/helper.rb', line 24

def find_field(model, attribute)
  model.columns.select.find { |field| field.name == attribute }
end

.load_environment! ⇒ Object

Loads all models



19
20
21
# File 'lib/database_consistency/helper.rb', line 19

def load_environment!
  Rails.application.eager_load! if defined?(Rails)
end

.message(model, column, template = nil) ⇒ String

Returns:

  • (String)


29
30
31
32
33
# File 'lib/database_consistency/helper.rb', line 29

def message(model, column, template = nil)
  str = "column #{column.name} of table #{model.table_name} of model #{model.name}"
  str += " #{template}" if template
  str
end

.models ⇒ Object

Returns list of models to check



7
8
9
# File 'lib/database_consistency/helper.rb', line 7

def models
  ActiveRecord::Base.descendants.delete_if(&:abstract_class?)
end

.parent_models ⇒ Object

Return list of not inherited models



12
13
14
15
16
# File 'lib/database_consistency/helper.rb', line 12

def parent_models
  models.group_by(&:table_name).each_value.map do |models|
    models.min_by { |model| models.include?(model.superclass) ? 1 : 0 }
  end
end