Support enum attributes on validate_absence_of (#1464)

This commit is contained in:
Gabriel Sobrinho 2021-12-20 12:33:23 -03:00 committed by GitHub
parent 629a0f0af9
commit ed60e91741
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -109,6 +109,8 @@ module Shoulda
end
elsif array_column?
['an arbitary value']
elsif enum_column?
enum_values.first
else
case column_type
when :integer, :float then 1
@ -145,6 +147,15 @@ module Shoulda
@subject.class.columns_hash[@attribute.to_s].respond_to?(:array) &&
@subject.class.columns_hash[@attribute.to_s].array
end
def enum_column?
@subject.class.respond_to?(:defined_enums) &&
@subject.class.defined_enums.key?(@attribute.to_s)
end
def enum_values
@subject.class.defined_enums[@attribute.to_s].values
end
end
end
end

View File

@ -91,6 +91,15 @@ describe Shoulda::Matchers::ActiveModel::ValidateAbsenceOfMatcher, type: :model
end
end
context 'when the column backing the attribute is an enum' do
it 'still works' do
model = define_model_validating_absence_of(:attr)
model.enum attr: %w[one two three]
expect(model.new).to validate_absence_of(:attr)
end
end
context 'when used in the negative' do
it 'fails' do
assertion = lambda do