diff --git a/lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb index bd834c33d..084b0b28c 100644 --- a/lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb @@ -107,6 +107,8 @@ def value else obj end + elsif array_column? + ['an arbitary value'] else case column_type when :integer, :float then 1 @@ -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 diff --git a/spec/unit/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb b/spec/unit/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb index a8a99cda6..7d404297c 100644 --- a/spec/unit/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb @@ -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