Sha256: 4a1a41c3de42a895fdf1435aefc3b55d2dd16fa95ee4d8df8e8a074e0c632e61
Contents?: true
Size: 1.43 KB
Versions: 3
Compression:
Stored size: 1.43 KB
Contents
require 'nullalign/nonnull_constraint' module Nullalign module Introspectors class ValidatesPresenceOf def instances(model) model.validators.select do |v| v.class == ActiveRecord::Validations::PresenceValidator && (v.options.keys & %i(on if unless)).empty? end end def desired_nonnull_constraints(model) instances(model).map do |v| v.attributes.map do |attribute| # This next bit is to avoid a false positive in the case where a validator uses # an association name rather than the field name (i.e. user vs user_id). association = model.reflect_on_all_associations.detect {|r| r.name == attribute } attribute_value = if association != nil association.foreign_key else attribute end Nullalign::NonnullConstraint.new(model, model.table_name, attribute_value) end end.flatten end private :desired_nonnull_constraints def missing_nonnull_constraints(model) return [] unless model.connection.tables.include? model.table_name existing_nonnull_constraints = TableData.new.nonnull_constraints(model) desired_nonnull_constraints(model).reject do |index| existing_nonnull_constraints.include?(index) end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems