Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validate_absence_of failing for array columns #1383

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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