From fd5c4e7422581f5e7797e62d689bd07cb15ab4f0 Mon Sep 17 00:00:00 2001 From: Santiago Bartesaghi Date: Tue, 19 Sep 2023 15:03:48 -0300 Subject: [PATCH] Add support to Rails 7.1 (#74) * Add support to Rails 7.1 * Use beta version for now --- .github/workflows/ci.yml | 4 +- README.md | 1 + active_storage_base64.gemspec | 8 +- bug_report_template.rb | 4 +- .../{rails_7_0.gemfile => rails_7_1.gemfile} | 2 +- .../support_for_base64.rb | 31 ++---- spec/user_base64_spec.rb | 102 ++++-------------- spec/user_file_spec.rb | 14 +-- 8 files changed, 49 insertions(+), 117 deletions(-) rename gemfiles/{rails_7_0.gemfile => rails_7_1.gemfile} (62%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 31583e6..26fad73 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,8 +8,8 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - gemfile: [rails_7_0.gemfile, rails_head.gemfile] - ruby_version: [2.7, 3.0] + gemfile: [rails_7_1.gemfile, rails_head.gemfile] + ruby_version: [2.7, 3.0, 3.1, 3.2] env: BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }} CC_TEST_REPORTER_ID: 7196b4aa257fde33f24463218af32db6a6efd23d9148204822f757fa614a093e diff --git a/README.md b/README.md index aa65f24..f3feb2b 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ gem 'active_storage_base64' ## Compatibility Rails Version | ActiveStorageBase64 Version --------------|----------------------------- +7.1.x | 3.0.x 7.0.x | 2.0.x 6.1.x | 1.2.x 6.0.x | 1.1.x diff --git a/active_storage_base64.gemspec b/active_storage_base64.gemspec index 14df786..ab5602c 100644 --- a/active_storage_base64.gemspec +++ b/active_storage_base64.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'active_storage_base64' - s.version = '2.0.0' + s.version = '3.0.0' s.summary = 'Base64 support for ActiveStorage' s.description = s.summary @@ -15,11 +15,11 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 2.7.0' # Dependencies - s.add_dependency 'activestorage', '>= 7.0' - s.add_dependency 'activesupport', '>= 7.0' + s.add_dependency 'activestorage', '> 7.0' + s.add_dependency 'activesupport', '> 7.0' # Development dependencies - s.add_development_dependency 'rails', '>= 7.0' + s.add_development_dependency 'rails', '> 7.0' s.add_development_dependency 'pry-rails', '~> 0.3.6' s.add_development_dependency 'reek', '~> 6.0.6' s.add_development_dependency 'rspec-rails', '~> 3.8.0' diff --git a/bug_report_template.rb b/bug_report_template.rb index a992247..d9b3ee5 100644 --- a/bug_report_template.rb +++ b/bug_report_template.rb @@ -8,9 +8,9 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } # Activate the gem you are reporting the issue against. - gem 'rails', '~> 7.0' + gem 'rails', '~> 7.1' gem 'sqlite3' - gem 'active_storage_base64', '~> 2.0.0' + gem 'active_storage_base64', '~> 3.0.0' end require 'active_record/railtie' diff --git a/gemfiles/rails_7_0.gemfile b/gemfiles/rails_7_1.gemfile similarity index 62% rename from gemfiles/rails_7_0.gemfile rename to gemfiles/rails_7_1.gemfile index 998d87a..34ee410 100644 --- a/gemfiles/rails_7_0.gemfile +++ b/gemfiles/rails_7_1.gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -gem 'rails', '~> 7.0.0' +gem 'rails', '~> 7.1.0.beta1' gemspec path: '..' diff --git a/lib/active_storage_support/support_for_base64.rb b/lib/active_storage_support/support_for_base64.rb index 41264d1..47a6984 100644 --- a/lib/active_storage_support/support_for_base64.rb +++ b/lib/active_storage_support/support_for_base64.rb @@ -17,7 +17,7 @@ def #{name} def #{name}=(attachable) attachment_changes["#{name}"] = - if attachable.nil? + if attachable.nil? || attachable == "" ActiveStorage::Attached::Changes::DeleteOne.new("#{name}", self) else ActiveStorage::Attached::Changes::CreateOne.new( @@ -39,28 +39,15 @@ def #{name} end def #{name}=(attachables) - if ActiveStorage.replace_on_assign_to_many - attachment_changes["#{name}"] = - if Array(attachables).none? - ActiveStorage::Attached::Changes::DeleteMany.new("#{name}", self) - else - ActiveStorage::Attached::Changes::CreateMany.new( - "#{name}", self, ActiveStorageSupport::Base64Many.from_base64(attachables) - ) - end - else - ActiveSupport::Deprecation.warn \ - "config.active_storage.replace_on_assign_to_many is deprecated and will be removed in Rails 7.1. " \ - "Make sure that your code works well with config.active_storage.replace_on_assign_to_many set to true before upgrading. " \ - "To append new attachables to the Active Storage association, prefer using `attach`. " \ - "Using association setter would result in purging the existing attached attachments and replacing them with new ones." + attachables = Array(attachables).compact_blank + pending_uploads = attachment_changes["#{name}"].try(:pending_uploads) - if Array(attachables).any? - attachment_changes["#{name}"] = - ActiveStorage::Attached::Changes::CreateMany.new( - "#{name}", self, #{name}.blobs + ActiveStorageSupport::Base64Many.from_base64(attachables) - ) - end + attachment_changes["#{name}"] = if attachables.none? + ActiveStorage::Attached::Changes::DeleteMany.new("#{name}", self) + else + ActiveStorage::Attached::Changes::CreateMany.new( + "#{name}", self, ActiveStorageSupport::Base64Many.from_base64(attachables), pending_uploads: pending_uploads + ) end end CODE diff --git a/spec/user_base64_spec.rb b/spec/user_base64_spec.rb index 2d6f5e2..374d3ed 100644 --- a/spec/user_base64_spec.rb +++ b/spec/user_base64_spec.rb @@ -446,14 +446,14 @@ context 'when it is called with only one picture' do context 'when only data is specified' do it 'attaches a picture to the user' do - user.pictures = base64_data + user.pictures = [base64_data] user.save expect(user.pictures.attached?).to be end it 'attached file matches attachment file' do - user.pictures = base64_data + user.pictures = [base64_data] user.save expect( @@ -465,7 +465,7 @@ context 'when filename is specified' do it 'assigns the specified filename' do - user.pictures = data_with_filename + user.pictures = [data_with_filename] user.save expect(user.pictures.first.filename.to_s).to eq(filename) @@ -531,14 +531,14 @@ context 'when it is called with only one picture' do context 'when only data is specified' do it 'attaches a picture to the user' do - user.pictures = base64_data + user.pictures = [base64_data] user.save expect(user.pictures.attached?).to be end it 'attached file matches attachment file' do - user.pictures = base64_data + user.pictures = [base64_data] user.save expect( @@ -550,7 +550,7 @@ context 'when filename is specified' do it 'assigns the specified filename' do - user.pictures = data_with_filename + user.pictures = [data_with_filename] user.save expect(user.pictures.first.filename.to_s).to eq(filename) @@ -610,13 +610,13 @@ context 'when a single picture is passed' do context 'when only data is specified' do it 'attaches a picture' do - user = User.create!(pictures: base64_data) + user = User.create!(pictures: [base64_data]) expect(user.pictures.attached?).to be end it 'attached file matches attachment file' do - user = User.create!(pictures: base64_data) + user = User.create!(pictures: [base64_data]) expect( File.open(ActiveStorage::Blob.service.send(:path_for, @@ -627,7 +627,7 @@ context 'when a filename is specified' do it 'assigns the specified filename' do - user = User.create!(pictures: data_with_filename) + user = User.create!(pictures: [data_with_filename]) expect(user.pictures.first.filename.to_s).to eq(filename) end @@ -662,13 +662,13 @@ context 'when a single picture is passed' do context 'when only data is specified' do it 'attaches a picture' do - user = User.create!(pictures: base64_data) + user = User.create!(pictures: [base64_data]) expect(user.pictures.attached?).to be end it 'attached file matches attachment file' do - user = User.create!(pictures: base64_data) + user = User.create!(pictures: [base64_data]) expect( File.open(ActiveStorage::Blob.service.send(:path_for, @@ -679,7 +679,7 @@ context 'when a filename is specified' do it 'assigns the specified filename' do - user = User.create!(pictures: data_with_filename) + user = User.create!(pictures: [data_with_filename]) expect(user.pictures.first.filename.to_s).to eq(filename) end @@ -704,7 +704,7 @@ end context 'when user has only one picture attached' do - let(:user) { User.create!(pictures: base64_data) } + let(:user) { User.create!(pictures: [base64_data]) } context 'when the user wants to remove the picture' do it 'removes the picture' do @@ -747,38 +747,10 @@ end end - context 'when replacing on assign' do - before do - @previous = ActiveStorage.replace_on_assign_to_many - ActiveStorage.replace_on_assign_to_many = true - end - - after do - ActiveStorage.replace_on_assign_to_many = @previous - end - - it 'updates the existing record replacing attachments' do - user.pictures = pictures_attachments - user.save - expect(user.pictures.count).to eq(2) - end - end - - context 'when appending on assign' do - before do - @previous = ActiveStorage.replace_on_assign_to_many - ActiveStorage.replace_on_assign_to_many = false - end - - after do - ActiveStorage.replace_on_assign_to_many = @previous - end - - it 'updates the existing record appending the new attachments' do - user.pictures = pictures_attachments - user.save - expect(user.pictures.count).to eq(4) - end + it 'updates the existing record replacing attachments' do + user.pictures = pictures_attachments + user.save + expect(user.pictures.count).to eq(2) end end end @@ -798,7 +770,7 @@ params.permit(:data) end context 'when user has only one picture attached' do - let(:user) { User.create!(pictures: base64_data) } + let(:user) { User.create!(pictures: [base64_data]) } context 'when the user wants to remove the picture' do it 'removes the picture' do @@ -841,38 +813,10 @@ end end - context 'when replacing on assign' do - before do - @previous = ActiveStorage.replace_on_assign_to_many - ActiveStorage.replace_on_assign_to_many = true - end - - after do - ActiveStorage.replace_on_assign_to_many = @previous - end - - it 'updates the existing record replacing attachments' do - user.pictures = pictures_attachments - user.save - expect(user.pictures.count).to eq(2) - end - end - - context 'when appending on assign' do - before do - @previous = ActiveStorage.replace_on_assign_to_many - ActiveStorage.replace_on_assign_to_many = false - end - - after do - ActiveStorage.replace_on_assign_to_many = @previous - end - - it 'updates the existing record appending the new attachments' do - user.pictures = pictures_attachments - user.save - expect(user.pictures.count).to eq(4) - end + it 'updates the existing record replacing attachments' do + user.pictures = pictures_attachments + user.save + expect(user.pictures.count).to eq(2) end end end @@ -880,7 +824,7 @@ end context 'when using a variant' do - let(:user) { User.create!(pictures: base64_data) } + let(:user) { User.create!(pictures: [base64_data]) } it 'returns a link' do url = rails_url.rails_blob_url(user.pictures.first.variant(:thumb).processed) diff --git a/spec/user_file_spec.rb b/spec/user_file_spec.rb index ffdc384..ecfc184 100644 --- a/spec/user_file_spec.rb +++ b/spec/user_file_spec.rb @@ -174,14 +174,14 @@ context 'when "user.pictures=" is called' do context 'when it is called with only one picture' do it 'attaches a picture to the user' do - user.pictures = file + user.pictures = [file] user.save expect(user.pictures.attached?).to be end it 'assigns the specified filename' do - user.pictures = file + user.pictures = [file] user.save expect(user.pictures.first.filename.to_s).to eq(filename) @@ -191,14 +191,14 @@ context 'when it is called with more than one picture' do context 'when called with an array' do it 'attaches an array of pictures to the user' do - user.pictures = pictures_attachments + user.pictures = [pictures_attachments] user.save expect(user.pictures.count).to eq(2) end it 'assigns the specified filename' do - user.pictures = pictures_attachments + user.pictures = [pictures_attachments] user.save expect(user.pictures.first.filename).to eq(filename) @@ -228,13 +228,13 @@ context 'when pictures are passed as a hash parameter' do context 'when a single picture is passed' do it 'attaches a picture' do - user = User.create!(pictures: file) + user = User.create!(pictures: [file]) expect(user.pictures.attached?).to be end it 'assigns the specified filename' do - user = User.create!(pictures: file) + user = User.create!(pictures: [file]) expect(user.pictures.first.filename).to eq(filename) end @@ -258,7 +258,7 @@ context 'when user already has pictures attached' do context 'when user has only one picture attached' do - let(:user) { User.create!(pictures: file) } + let(:user) { User.create!(pictures: [file]) } context 'when the user wants to remove the picture' do it 'removes the picture' do