Skip to content

Commit

Permalink
Remove active_record_supports_active_storage? method
Browse files Browse the repository at this point in the history
  • Loading branch information
vsppedro committed Mar 17, 2021
1 parent 3a239b5 commit b1a694f
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 137 deletions.
4 changes: 0 additions & 4 deletions spec/support/unit/helpers/active_record_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ def active_record_version
Tests::Version.new(::ActiveRecord::VERSION::STRING)
end

def active_record_supports_active_storage?
active_record_version >= 5.2
end

def active_record_supports_validate_presence_on_active_storage?
active_record_version >= '6.0.0.beta1'
end
Expand Down
264 changes: 131 additions & 133 deletions spec/unit/shoulda/matchers/active_record/have_attached_matcher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,194 +1,192 @@
require 'unit_spec_helper'

describe Shoulda::Matchers::ActiveRecord::HaveAttachedMatcher, type: :model do
if active_record_supports_active_storage?
before do
create_table :active_storage_blobs do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.bigint :byte_size, null: false
t.string :checksum, null: false
t.datetime :created_at, null: false

t.index [:key], unique: true
end
before do
create_table :active_storage_blobs do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.bigint :byte_size, null: false
t.string :checksum, null: false
t.datetime :created_at, null: false

t.index [:key], unique: true
end

create_table :active_storage_attachments do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false
t.references :blob, null: false
create_table :active_storage_attachments do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false
t.references :blob, null: false

t.datetime :created_at, null: false
t.datetime :created_at, null: false

t.index [:record_type, :record_id, :name, :blob_id],
name: 'index_active_storage_attachments_uniqueness', unique: true
t.index [:record_type, :record_id, :name, :blob_id],
name: 'index_active_storage_attachments_uniqueness', unique: true

# The original rails migration has a foreign key.
# Since this messes up the clearing of the database, it's removed here.
# t.foreign_key :active_storage_blobs, column: :blob_id
# The original rails migration has a foreign key.
# Since this messes up the clearing of the database, it's removed here.
# t.foreign_key :active_storage_blobs, column: :blob_id
end
end

describe 'have_one_attached' do
describe '#description' do
it 'returns the message with the name of the association' do
matcher = have_one_attached(:avatar)
expect(matcher.description).
to eq('have a has_one_attached called avatar')
end
end

describe 'have_one_attached' do
describe '#description' do
it 'returns the message with the name of the association' do
matcher = have_one_attached(:avatar)
expect(matcher.description).
to eq('have a has_one_attached called avatar')
end
context 'when the attached exists on the model' do
it 'matches' do
record = record_having_one_attached(:avatar)
expect { have_one_attached(:avatar) }.
to match_against(record).
or_fail_with(<<-MESSAGE)
Did not expect User to have a has_one_attached called avatar, but it does.
MESSAGE
end

context 'when the attached exists on the model' do
context 'and the reader attribute does not exist' do
it 'matches' do
record = record_having_one_attached(:avatar)
record = record_having_one_attached(:avatar, remove_reader: true)
expect { have_one_attached(:avatar) }.
to match_against(record).
or_fail_with(<<-MESSAGE)
Did not expect User to have a has_one_attached called avatar, but it does.
MESSAGE
end

context 'and the reader attribute does not exist' do
it 'matches' do
record = record_having_one_attached(:avatar, remove_reader: true)
expect { have_one_attached(:avatar) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected User to have a has_one_attached called avatar, but this could not be proved.
User does not have a :avatar method.
MESSAGE
end
MESSAGE
end
end

context 'and the writer attribute does not exist' do
it 'matches' do
record = record_having_one_attached(:avatar, remove_writer: true)
expect { have_one_attached(:avatar) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
context 'and the writer attribute does not exist' do
it 'matches' do
record = record_having_one_attached(:avatar, remove_writer: true)
expect { have_one_attached(:avatar) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected User to have a has_one_attached called avatar, but this could not be proved.
User does not have a :avatar= method.
MESSAGE
end
MESSAGE
end
end

context 'and the attachments association does not exist' do
it 'matches' do
record = record_having_one_attached(:avatar, remove_attachments: true)
expect { have_one_attached(:avatar) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
context 'and the attachments association does not exist' do
it 'matches' do
record = record_having_one_attached(:avatar, remove_attachments: true)
expect { have_one_attached(:avatar) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected User to have a has_one_attached called avatar, but this could not be proved.
Expected User to have a has_one association called avatar_attachment (no association called avatar_attachment)
MESSAGE
end
MESSAGE
end
end

context 'and the blobs association is invalid' do
it 'matches' do
record = record_having_one_attached(:avatar, invalidate_blobs: true)
expect { have_one_attached(:avatar) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
context 'and the blobs association is invalid' do
it 'matches' do
record = record_having_one_attached(:avatar, invalidate_blobs: true)
expect { have_one_attached(:avatar) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected User to have a has_one_attached called avatar, but this could not be proved.
Expected User to have a has_one association called avatar_blob through avatar_attachment (avatar_blob should resolve to ActiveStorage::Blob for class_name)
MESSAGE
end
MESSAGE
end
end

context 'and the eager loading scope does not exist' do
it 'matches' do
record = record_having_one_attached(:avatar, remove_eager_loading_scope: true)
expect { have_one_attached(:avatar) }.
not_to match_against(record).
and_fail_with <<-MESSAGE
context 'and the eager loading scope does not exist' do
it 'matches' do
record = record_having_one_attached(:avatar, remove_eager_loading_scope: true)
expect { have_one_attached(:avatar) }.
not_to match_against(record).
and_fail_with <<-MESSAGE
Expected User to have a has_one_attached called avatar, but this could not be proved.
User does not have a :with_attached_avatar scope.
MESSAGE
end
MESSAGE
end
end
end
end

describe 'have_many_attached' do
describe '#description' do
it 'returns the message with the name of the association' do
matcher = have_many_attached(:avatars)
expect(matcher.description).
to eq('have a has_many_attached called avatars')
end
describe 'have_many_attached' do
describe '#description' do
it 'returns the message with the name of the association' do
matcher = have_many_attached(:avatars)
expect(matcher.description).
to eq('have a has_many_attached called avatars')
end
end

context 'when the attached exists on the model' do
it 'matches' do
record = record_having_many_attached(:avatars)
expect { have_many_attached(:avatars) }.
to match_against(record).
or_fail_with(<<-MESSAGE)
context 'when the attached exists on the model' do
it 'matches' do
record = record_having_many_attached(:avatars)
expect { have_many_attached(:avatars) }.
to match_against(record).
or_fail_with(<<-MESSAGE)
Did not expect User to have a has_many_attached called avatars, but it does.
MESSAGE
end
MESSAGE
end

context 'and the reader attribute does not exist' do
it 'matches' do
record = record_having_many_attached(:avatars, remove_reader: true)
expect { have_many_attached(:avatars) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
context 'and the reader attribute does not exist' do
it 'matches' do
record = record_having_many_attached(:avatars, remove_reader: true)
expect { have_many_attached(:avatars) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected User to have a has_many_attached called avatars, but this could not be proved.
User does not have a :avatars method.
MESSAGE
end
MESSAGE
end
end

context 'and the writer attribute does not exist' do
it 'matches' do
record = record_having_many_attached(:avatars, remove_writer: true)
expect { have_many_attached(:avatars) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
context 'and the writer attribute does not exist' do
it 'matches' do
record = record_having_many_attached(:avatars, remove_writer: true)
expect { have_many_attached(:avatars) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected User to have a has_many_attached called avatars, but this could not be proved.
User does not have a :avatars= method.
MESSAGE
end
MESSAGE
end
end

context 'and the attachments association does not exist' do
it 'matches' do
record = record_having_many_attached(:avatars, remove_attachments: true)
expect { have_many_attached(:avatars) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
context 'and the attachments association does not exist' do
it 'matches' do
record = record_having_many_attached(:avatars, remove_attachments: true)
expect { have_many_attached(:avatars) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected User to have a has_many_attached called avatars, but this could not be proved.
Expected User to have a has_many association called avatars_attachments (no association called avatars_attachments)
MESSAGE
end
MESSAGE
end
end

context 'and the blobs association is invalid' do
it 'matches' do
record = record_having_many_attached(:avatars, invalidate_blobs: true)
expect { have_many_attached(:avatars) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
context 'and the blobs association is invalid' do
it 'matches' do
record = record_having_many_attached(:avatars, invalidate_blobs: true)
expect { have_many_attached(:avatars) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected User to have a has_many_attached called avatars, but this could not be proved.
Expected User to have a has_many association called avatars_blobs through avatars_attachments (avatars_blobs should resolve to ActiveStorage::Blob for class_name)
MESSAGE
end
MESSAGE
end
end

context 'and the eager loading scope does not exist' do
it 'matches' do
record = record_having_many_attached(:avatars, remove_eager_loading_scope: true)
expect { have_many_attached(:avatars) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
context 'and the eager loading scope does not exist' do
it 'matches' do
record = record_having_many_attached(:avatars, remove_eager_loading_scope: true)
expect { have_many_attached(:avatars) }.
not_to match_against(record).
and_fail_with(<<-MESSAGE)
Expected User to have a has_many_attached called avatars, but this could not be proved.
User does not have a :with_attached_avatars scope.
MESSAGE
end
MESSAGE
end
end
end
Expand Down

0 comments on commit b1a694f

Please sign in to comment.