Skip to content

Commit

Permalink
Add support to Rails 7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
santib committed Sep 19, 2023
1 parent 9358958 commit 49609bf
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 115 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions active_storage_base64.gemspec
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -15,7 +15,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = '>= 2.7.0'

# Dependencies
s.add_dependency 'rails', '>= 7.0'
s.add_dependency 'rails', '>= 7.1'

# Development dependencies
s.add_development_dependency 'pry-rails', '~> 0.3.6'
Expand Down
4 changes: 2 additions & 2 deletions bug_report_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_7_0.gemfile → gemfiles/rails_7_1.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source 'https://rubygems.org'

gem 'rails', '~> 7.0.0'
gem 'rails', '~> 7.1.0'

gemspec path: '..'
31 changes: 9 additions & 22 deletions lib/active_storage_support/support_for_base64.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
102 changes: 23 additions & 79 deletions spec/user_base64_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -841,46 +813,18 @@
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
end
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)
Expand Down
14 changes: 7 additions & 7 deletions spec/user_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 49609bf

Please sign in to comment.