Skip to content

Commit ace206c

Browse files
authored
Make incorrect_length_validation use column name (#111)
This commit fixes a bug that caused incorrect_length_validation to use the first length validator on _any_ column when identifying the model-level maximum. This is naturally incorrect, and the code was modified to take the column name into account.
1 parent 4096828 commit ace206c

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/active_record_doctor/detectors/incorrect_length_validation.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def detect
3838
next if config(:ignore_attributes).include?("#{model.name}.#{column.name}")
3939
next if ![:string, :text].include?(column.type)
4040

41-
model_maximum = maximum_allowed_by_validations(model)
41+
model_maximum = maximum_allowed_by_validations(model, column.name.to_sym)
4242
next if model_maximum == column.limit
4343

4444
problem!(
@@ -52,9 +52,11 @@ def detect
5252
end
5353
end
5454

55-
def maximum_allowed_by_validations(model)
55+
def maximum_allowed_by_validations(model, column)
5656
length_validator = model.validators.find do |validator|
57-
validator.kind == :length && validator.options.include?(:maximum)
57+
validator.kind == :length &&
58+
validator.options.include?(:maximum) &&
59+
validator.attributes.include?(column)
5860
end
5961
length_validator ? length_validator.options[:maximum] : nil
6062
end

test/active_record_doctor/detectors/incorrect_length_validation_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ class ActiveRecordDoctor::Detectors::IncorrectLengthValidationTest < Minitest::T
44
def test_validation_and_limit_equal_is_ok
55
create_table(:users) do |t|
66
t.string :email, limit: 64
7+
t.string :name, limit: 32
78
end.create_model do
89
validates :email, length: { maximum: 64 }
10+
validates :name, length: { maximum: 32 }
911
end
1012

1113
refute_problems

0 commit comments

Comments
 (0)