Support enum attributes on validate_absence_of (#1464)
This commit is contained in:
parent
629a0f0af9
commit
ed60e91741
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue