Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def detect

def has_presence_validator?(model, column)
model.validators.any? do |validator|
validator.kind == :presence && validator.attributes.include?(column.name.to_sym)
attributes = validator.attributes.map(&:to_s)
validator.kind == :presence && attributes.include?(column.name)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def message(model:, attribute:, table:, database_maximum:, model_maximum:)
def detect
each_model(except: config(:ignore_models), existing_tables_only: true) do |model|
each_attribute(model, except: config(:ignore_attributes), type: [:string, :text]) do |column|
model_maximum = maximum_allowed_by_validations(model, column.name.to_sym)
model_maximum = maximum_allowed_by_validations(model, column.name)
next if model_maximum == column.limit

problem!(
Expand All @@ -49,9 +49,11 @@ def detect

def maximum_allowed_by_validations(model, column)
length_validator = model.validators.find do |validator|
attributes = validator.attributes.map(&:to_s)

validator.kind == :length &&
validator.options.include?(:maximum) &&
validator.attributes.include?(column)
attributes.include?(column)
end
length_validator ? length_validator.options[:maximum] : nil
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ def non_null_needed?(model, column)
end

required_presence_validators(model).any? do |validator|
attributes = validator.attributes
attributes = validator.attributes.map(&:to_s)

attributes.include?(column.name.to_sym) ||
(belongs_to && attributes.include?(belongs_to.name.to_sym))
attributes.include?(column.name) ||
(belongs_to && attributes.include?(belongs_to.name.to_s))
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ def inclusion_validator_present?(model, column)
validator_items = inclusion_validator_items(validator)
return true if validator_items.is_a?(Proc)

attributes = validator.attributes.map(&:to_s)
validator.is_a?(ActiveModel::Validations::InclusionValidator) &&
validator.attributes.map(&:to_s).include?(column.name) &&
attributes.include?(column.name) &&
!validator_items.include?(nil)
end
end
Expand All @@ -69,23 +70,26 @@ def exclusion_validator_present?(model, column)
validator_items = inclusion_validator_items(validator)
return true if validator_items.is_a?(Proc)

attributes = validator.attributes.map(&:to_s)
validator.is_a?(ActiveModel::Validations::ExclusionValidator) &&
validator.attributes.include?(column.name.to_sym) &&
attributes.include?(column.name) &&
validator_items.include?(nil)
end
end

def presence_validator_present?(model, column)
allowed_attributes = [column.name.to_sym]
allowed_attributes = [column.name]

belongs_to = model.reflect_on_all_associations(:belongs_to).find do |reflection|
reflection.foreign_key == column.name
end
allowed_attributes << belongs_to.name.to_sym if belongs_to
allowed_attributes << belongs_to.name.to_s if belongs_to

model.validators.any? do |validator|
attributes = validator.attributes.map(&:to_s)

validator.is_a?(ActiveRecord::Validations::PresenceValidator) &&
(validator.attributes & allowed_attributes).present?
(attributes & allowed_attributes).present?
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def test_validation_and_limit_equal_is_ok
t.string :email, limit: 64
t.string :name, limit: 32
end.define_model do
validates :email, length: { maximum: 64 }
validates "email", length: { maximum: 64 } # use a string on purpose
validates :name, length: { maximum: 32 }
end

Expand Down
Loading