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
- .connected?(klass) ⇒ Boolean
- .connection_config(klass) ⇒ Object
- .database_name(model) ⇒ Object
- .extract_index_columns(index_columns) ⇒ Array<String>
- .first_level_associations(model) ⇒ Object
- .foreign_key_or_attribute(model, attribute) ⇒ Object
-
.models(configuration) ⇒ Object
Returns list of models to check.
-
.parent_models(configuration) ⇒ Object
Return list of not inherited models.
- .postgresql? ⇒ Boolean
- .project_klass?(klass) ⇒ Boolean
- .project_models(configuration) ⇒ Object
- .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_db_config) ActiveRecord::Base.connection_db_config.configuration_hash[:adapter] else ActiveRecord::Base.connection_config[:adapter] end end |
.check_inclusion?(array, element) ⇒ Boolean
75 76 77 |
# File 'lib/database_consistency/helper.rb', line 75 def check_inclusion?(array, element) array.include?(element.to_s) || array.include?(element.to_sym) end |
.connected?(klass) ⇒ Boolean
49 50 51 52 53 54 |
# File 'lib/database_consistency/helper.rb', line 49 def connected?(klass) klass.connection rescue ActiveRecord::ConnectionNotEstablished puts "#{klass} doesn't have active connection: ignoring" false end |
.connection_config(klass) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/database_consistency/helper.rb', line 24 def connection_config(klass) if klass.respond_to?(:connection_db_config) klass.connection_db_config.configuration_hash else klass.connection_config end end |
.database_name(model) ⇒ Object
16 17 18 |
# File 'lib/database_consistency/helper.rb', line 16 def database_name(model) model.connection_db_config.name.to_s if model.respond_to?(:connection_db_config) end |
.extract_index_columns(index_columns) ⇒ Array<String>
91 92 93 94 95 96 97 98 99 |
# File 'lib/database_consistency/helper.rb', line 91 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
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/database_consistency/helper.rb', line 79 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
115 116 117 |
# File 'lib/database_consistency/helper.rb', line 115 def foreign_key_or_attribute(model, attribute) model._reflect_on_association(attribute)&.foreign_key || attribute end |
.models(configuration) ⇒ Object
Returns list of models to check
41 42 43 44 45 46 47 |
# File 'lib/database_consistency/helper.rb', line 41 def models(configuration) project_models(configuration).select do |klass| !klass.abstract_class? && klass.table_exists? && !klass.name.include?('HABTM_') end end |
.parent_models(configuration) ⇒ Object
Return list of not inherited models
57 58 59 60 61 |
# File 'lib/database_consistency/helper.rb', line 57 def parent_models(configuration) models(configuration).group_by(&:table_name).each_value.flat_map do |models| models.reject { |model| models.include?(model.superclass) } end end |
.postgresql? ⇒ Boolean
20 21 22 |
# File 'lib/database_consistency/helper.rb', line 20 def postgresql? adapter == 'postgresql' end |
.project_klass?(klass) ⇒ Boolean
66 67 68 69 70 71 72 |
# File 'lib/database_consistency/helper.rb', line 66 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) rescue NameError false end |
.project_models(configuration) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/database_consistency/helper.rb', line 32 def project_models(configuration) ActiveRecord::Base.descendants.select do |klass| next unless configuration.model_enabled?(klass) project_klass?(klass) && connected?(klass) end end |
.scope_columns(validator, model) ⇒ Object
109 110 111 112 113 |
# File 'lib/database_consistency/helper.rb', line 109 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
101 102 103 |
# File 'lib/database_consistency/helper.rb', line 101 def sorted_uniqueness_validator_columns(attribute, validator, model) uniqueness_validator_columns(attribute, validator, model).sort end |
.uniqueness_validator_columns(attribute, validator, model) ⇒ Object
105 106 107 |
# File 'lib/database_consistency/helper.rb', line 105 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
120 121 122 123 124 125 126 127 128 |
# File 'lib/database_consistency/helper.rb', line 120 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 |