Module: DatabaseConsistency::Helper
- Defined in:
- lib/database_consistency/helper.rb
Overview
The module contains helper methods
Class Method Summary collapse
- .check_inclusion?(array, element) ⇒ Boolean
- .extract_index_columns(index_columns) ⇒ Array<String>
- .first_level_associations(model) ⇒ Object
-
.models ⇒ Object
Returns list of models to check.
-
.parent_models ⇒ Object
Return list of not inherited models.
- .scope_columns(validator, model) ⇒ Object
- .sorted_uniqueness_validator_columns(attribute, validator, model) ⇒ Object
- .uniqueness_validator_columns(attribute, validator, model) ⇒ Object
- .wrapped_attribute_name(attribute, validator) ⇒ String
Class Method Details
.check_inclusion?(array, element) ⇒ Boolean
23 24 25 |
# File 'lib/database_consistency/helper.rb', line 23 def check_inclusion?(array, element) array.include?(element.to_s) || array.include?(element.to_sym) end |
.extract_index_columns(index_columns) ⇒ Array<String>
39 40 41 42 43 44 45 46 47 |
# File 'lib/database_consistency/helper.rb', line 39 def extract_index_columns(index_columns) return index_columns unless index_columns.is_a?(String) index_columns.split(',') .map(&:strip) .map { |str| str.gsub(/lower\(/i, 'lower(') } .map { |str| str.gsub(/\(([^)]+)\)::\w+/, '\1') } .map { |str| str.gsub(/'([^)]+)'::\w+/, '\1') } end |
.first_level_associations(model) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/database_consistency/helper.rb', line 27 def first_level_associations(model) associations = model.reflect_on_all_associations while model != ActiveRecord::Base && model.respond_to?(:reflect_on_all_associations) model = model.superclass associations -= model.reflect_on_all_associations end associations end |
.models ⇒ Object
Returns list of models to check
9 10 11 12 13 |
# File 'lib/database_consistency/helper.rb', line 9 def models ActiveRecord::Base.descendants.delete_if(&:abstract_class?).delete_if do |klass| !klass.connection.table_exists?(klass.table_name) || klass.name.include?('HABTM_') end end |
.parent_models ⇒ Object
Return list of not inherited models
16 17 18 19 20 |
# File 'lib/database_consistency/helper.rb', line 16 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 |
.scope_columns(validator, model) ⇒ Object
57 58 59 60 61 |
# File 'lib/database_consistency/helper.rb', line 57 def scope_columns(validator, model) Array.wrap(validator.[:scope]).map do |scope_item| model._reflect_on_association(scope_item)&.foreign_key || scope_item end end |
.sorted_uniqueness_validator_columns(attribute, validator, model) ⇒ Object
49 50 51 |
# File 'lib/database_consistency/helper.rb', line 49 def sorted_uniqueness_validator_columns(attribute, validator, model) uniqueness_validator_columns(attribute, validator, model).sort end |
.uniqueness_validator_columns(attribute, validator, model) ⇒ Object
53 54 55 |
# File 'lib/database_consistency/helper.rb', line 53 def uniqueness_validator_columns(attribute, validator, model) ([wrapped_attribute_name(attribute, validator)] + scope_columns(validator, model)).map(&:to_s) end |
.wrapped_attribute_name(attribute, validator) ⇒ String
64 65 66 67 68 69 70 |
# File 'lib/database_consistency/helper.rb', line 64 def wrapped_attribute_name(attribute, validator) if validator.[:case_sensitive].nil? || validator.[:case_sensitive] attribute else "lower(#{attribute})" end end |