Skip to content

Commit 8d0edf9

Browse files
committed
missing_presence_validation: properly report column and association names
1 parent 40eb725 commit 8d0edf9

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

lib/active_record_doctor/detectors/missing_presence_validation.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ class MissingPresenceValidation < Base # :nodoc:
2121

2222
private
2323

24-
def message(type:, column_or_association:, model:)
24+
def message(type:, column:, reflection:, model:)
2525
case type
2626
when :missing_validator
27-
"add a `presence` validator to #{model}.#{column_or_association} - it's NOT NULL but lacks a validator"
27+
"add a `presence` validator to #{model}.#{column} - it's NOT NULL but lacks a validator"
2828
when :optional_association
29-
"add `optional: false` to #{model}.#{column_or_association} - the foreign key #{column_or_association}_id is NOT NULL" # rubocop:disable Layout/LineLength
29+
"add `optional: false` to #{model}.#{reflection.name} - the foreign key #{reflection.foreign_key} is NOT NULL"
3030
when :optional_polymorphic_association
31-
"add `optional: false` to #{model}.#{column_or_association} - the foreign key #{column_or_association}_id or type #{column_or_association}_type are NOT NULL" # rubocop:disable Layout/LineLength
31+
"add `optional: false` to #{model}.#{reflection.name} - the foreign key #{reflection.foreign_key} or type #{reflection.foreign_type} are NOT NULL" # rubocop:disable Layout/LineLength
3232
end
3333
end
3434

@@ -125,7 +125,7 @@ def detect
125125

126126
# ... report an error about an incorrectly configured polymorphic
127127
# association.
128-
problematic_polymorphic_associations << reflection.name
128+
problematic_polymorphic_associations << reflection
129129
else
130130
# If the foreign key is not one of the columns that lack a
131131
# validator then it means the association added a validator and is
@@ -137,30 +137,30 @@ def detect
137137
problematic_columns.delete(foreign_key_column)
138138

139139
# ... report an error about an incorrectly configured association.
140-
problematic_associations << reflection.name
140+
problematic_associations << reflection
141141
end
142142
end
143143

144144
# Finally, regular and polymorphic associations that are explicitly
145145
# ignored should be removed from the output. It's NOT enough to skip
146146
# processing them in the loop above because their underlying foreign
147147
# key and type columns must be removed from output, too.
148-
problematic_associations.reject! do |name|
149-
config(:ignore_attributes).include?("#{model.name}.#{name}")
148+
problematic_associations.reject! do |reflection|
149+
config(:ignore_attributes).include?("#{model.name}.#{reflection.name}")
150150
end
151-
problematic_polymorphic_associations.reject! do |name|
152-
config(:ignore_attributes).include?("#{model.name}.#{name}")
151+
problematic_polymorphic_associations.reject! do |reflection|
152+
config(:ignore_attributes).include?("#{model.name}.#{reflection.name}")
153153
end
154154

155155
# Job is done and all accumulated errors can be reported.
156-
problematic_polymorphic_associations.each do |name|
157-
problem!(type: :optional_polymorphic_association, column_or_association: name, model: model.name)
156+
problematic_polymorphic_associations.each do |reflection|
157+
problem!(type: :optional_polymorphic_association, column: nil, reflection: reflection, model: model.name)
158158
end
159-
problematic_associations.each do |name|
160-
problem!(type: :optional_association, column_or_association: name, model: model.name)
159+
problematic_associations.each do |reflection|
160+
problem!(type: :optional_association, column: nil, reflection: reflection, model: model.name)
161161
end
162162
problematic_columns.each do |column|
163-
problem!(type: :missing_validator, column_or_association: column.name, model: model.name)
163+
problem!(type: :missing_validator, column: column.name, reflection: nil, model: model.name)
164164
end
165165
end
166166
end

0 commit comments

Comments
 (0)