Module: DatabaseConsistency::Helper
- Defined in:
- lib/database_consistency/helper.rb
Overview
The module contains helper methods
Class Method Summary collapse
- .adapter ⇒ Object
- .check_inclusion?(array, element) ⇒ Boolean
- .connection_config(klass) ⇒ Object
- .extract_index_columns(index_columns) ⇒ Array<String>
- .first_level_associations(model) ⇒ Object
- .foreign_key_or_attribute(model, attribute) ⇒ Object
-
.models ⇒ Object
Returns list of models to check.
-
.parent_models ⇒ Object
Return list of not inherited models.
- .project_klass?(klass) ⇒ Boolean
- .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, model) ⇒ String
Class Method Details
.adapter ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/database_consistency/helper.rb', line 8 def adapter if ActiveRecord::Base.respond_to?(:connection_config) ActiveRecord::Base.connection_config[:adapter] else ActiveRecord::Base.connection_db_config.configuration_hash[:adapter] end end |
.check_inclusion?(array, element) ⇒ Boolean
50 51 52 |
# File 'lib/database_consistency/helper.rb', line 50 def check_inclusion?(array, element) array.include?(element.to_s) || array.include?(element.to_sym) end |
.connection_config(klass) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/database_consistency/helper.rb', line 16 def connection_config(klass) if klass.respond_to?(:connection_config) klass.connection_config else klass.connection_db_config.configuration_hash end end |
.extract_index_columns(index_columns) ⇒ Array<String>
66 67 68 69 70 71 72 73 74 |
# File 'lib/database_consistency/helper.rb', line 66 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
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/database_consistency/helper.rb', line 54 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 |
.foreign_key_or_attribute(model, attribute) ⇒ Object
90 91 92 |
# File 'lib/database_consistency/helper.rb', line 90 def foreign_key_or_attribute(model, attribute) model._reflect_on_association(attribute)&.foreign_key || attribute end |
.models ⇒ Object
Returns list of models to check
25 26 27 28 29 30 31 |
# File 'lib/database_consistency/helper.rb', line 25 def models ActiveRecord::Base.descendants.delete_if(&:abstract_class?).select do |klass| klass.connection.table_exists?(klass.table_name) && !klass.name.include?('HABTM_') && project_klass?(klass) end end |
.parent_models ⇒ Object
Return list of not inherited models
34 35 36 37 38 |
# File 'lib/database_consistency/helper.rb', line 34 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 |
.project_klass?(klass) ⇒ Boolean
43 44 45 46 47 |
# File 'lib/database_consistency/helper.rb', line 43 def project_klass?(klass) return true unless Module.respond_to?(:const_source_location) && defined?(Bundler) !Module.const_source_location(klass.to_s).first.to_s.include?(Bundler.bundle_path.to_s) end |
.scope_columns(validator, model) ⇒ Object
84 85 86 87 88 |
# File 'lib/database_consistency/helper.rb', line 84 def scope_columns(validator, model) Array.wrap(validator.[:scope]).map do |scope_item| foreign_key_or_attribute(model, scope_item) end end |
.sorted_uniqueness_validator_columns(attribute, validator, model) ⇒ Object
76 77 78 |
# File 'lib/database_consistency/helper.rb', line 76 def sorted_uniqueness_validator_columns(attribute, validator, model) uniqueness_validator_columns(attribute, validator, model).sort end |
.uniqueness_validator_columns(attribute, validator, model) ⇒ Object
80 81 82 |
# File 'lib/database_consistency/helper.rb', line 80 def uniqueness_validator_columns(attribute, validator, model) ([wrapped_attribute_name(attribute, validator, model)] + scope_columns(validator, model)).map(&:to_s) end |
.wrapped_attribute_name(attribute, validator, model) ⇒ String
95 96 97 98 99 100 101 102 103 |
# File 'lib/database_consistency/helper.rb', line 95 def wrapped_attribute_name(attribute, validator, model) attribute = foreign_key_or_attribute(model, attribute) if validator.[:case_sensitive].nil? || validator.[:case_sensitive] attribute else "lower(#{attribute})" end end |