class UniqueValidationInspector::Inspector
Constants
- VERSION
Public Class Methods
new(app)
click to toggle source
# File lib/unique_validation_inspector.rb, line 13 def initialize(app) @app = app end
Public Instance Methods
defined_unique_indexes(table_name, field, scope)
click to toggle source
# File lib/unique_validation_inspector.rb, line 35 def defined_unique_indexes(table_name, field, scope) #https://dev.mysql.com/doc/refman/5.7/en/multiple-column-indexes.html columns = [] columns += field columns = columns + Array(scope) if scope unique_indexes(table_name).any? do |index_def| columns.map(&:to_s) == index_def.columns end end
defined_unique_validations()
click to toggle source
# File lib/unique_validation_inspector.rb, line 21 def defined_unique_validations ActiveRecord::Base.descendants.reject do |model| if model.abstract_class? true else validators = model.validators.select {|v| v.is_a?(ActiveRecord::Validations::UniquenessValidator) } validators.empty? end end.collect do |model| validators = model.validators.select {|v| v.is_a?(ActiveRecord::Validations::UniquenessValidator) } {:model => model, :validators => validators} end end
load_everything!()
click to toggle source
# File lib/unique_validation_inspector.rb, line 17 def load_everything! @app.eager_load! end
Private Instance Methods
unique_indexes(table_name)
click to toggle source
# File lib/unique_validation_inspector.rb, line 48 def unique_indexes(table_name) ActiveRecord::Base.connection.indexes(table_name.to_sym).select{|i| i.unique } end