Skip to content

Commit

Permalink
Fix validate_absence_of failing for array columns
Browse files Browse the repository at this point in the history
This commit fixes [#1240](#1240).

Changelog

- Made `ValidatingAbsenceOfMatcher#value` check whether column is array

- Added tests on array columns for `ValidateAbsenceOfMatcher`
  • Loading branch information
thealiilman committed Dec 2, 2020
1 parent 3adbbf1 commit 9140e2b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Expand Up @@ -107,6 +107,8 @@ def value
else
obj
end
elsif array_column?
['an arbitary value']
else
case column_type
when :integer, :float then 1
Expand Down Expand Up @@ -137,6 +139,12 @@ def reflection
@subject.class.respond_to?(:reflect_on_association) &&
@subject.class.reflect_on_association(@attribute)
end

def array_column?
@subject.class.respond_to?(:columns_hash) &&
@subject.class.columns_hash[@attribute.to_s].respond_to?(:array) &&
@subject.class.columns_hash[@attribute.to_s].array
end
end
end
end
Expand Down
Expand Up @@ -48,6 +48,49 @@ def self.available_column_types
end
end

if database_supports_array_columns? && active_record_supports_array_columns?
context 'when the column backing the attribute is an array' do
context 'of varchar' do
it 'still works' do
record = validating_absence_of(
:attr,
{},
type: :varchar,
options: { array: true, default: [], null: false },
)

expect(record).to validate_absence_of(:attr)
end
end

context 'of string' do
it 'still works' do
record = validating_absence_of(
:attr,
{},
type: :string,
options: { array: true, default: [], null: false },
)

expect(record).to validate_absence_of(:attr)
end
end

context 'of a type other than string' do
it 'still works' do
record = validating_absence_of(
:possible_meeting_dates,
{},
type: :date,
options: { array: true, default: [], null: false },
)

expect(record).to validate_absence_of(:possible_meeting_dates)
end
end
end
end

context 'when used in the negative' do
it 'fails' do
assertion = lambda do
Expand Down

0 comments on commit 9140e2b

Please sign in to comment.