Skip to content

Commit fd5c4e7

Browse files
authored
Add support to Rails 7.1 (#74)
* Add support to Rails 7.1 * Use beta version for now
1 parent 4a5adef commit fd5c4e7

File tree

8 files changed

+49
-117
lines changed

8 files changed

+49
-117
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
gemfile: [rails_7_0.gemfile, rails_head.gemfile]
12-
ruby_version: [2.7, 3.0]
11+
gemfile: [rails_7_1.gemfile, rails_head.gemfile]
12+
ruby_version: [2.7, 3.0, 3.1, 3.2]
1313
env:
1414
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}
1515
CC_TEST_REPORTER_ID: 7196b4aa257fde33f24463218af32db6a6efd23d9148204822f757fa614a093e

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ gem 'active_storage_base64'
1616
## Compatibility
1717
Rails Version | ActiveStorageBase64 Version
1818
--------------|-----------------------------
19+
7.1.x | 3.0.x
1920
7.0.x | 2.0.x
2021
6.1.x | 1.2.x
2122
6.0.x | 1.1.x

active_storage_base64.gemspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'active_storage_base64'
3-
s.version = '2.0.0'
3+
s.version = '3.0.0'
44
s.summary = 'Base64 support for ActiveStorage'
55
s.description = s.summary
66

@@ -15,11 +15,11 @@ Gem::Specification.new do |s|
1515
s.required_ruby_version = '>= 2.7.0'
1616

1717
# Dependencies
18-
s.add_dependency 'activestorage', '>= 7.0'
19-
s.add_dependency 'activesupport', '>= 7.0'
18+
s.add_dependency 'activestorage', '> 7.0'
19+
s.add_dependency 'activesupport', '> 7.0'
2020

2121
# Development dependencies
22-
s.add_development_dependency 'rails', '>= 7.0'
22+
s.add_development_dependency 'rails', '> 7.0'
2323
s.add_development_dependency 'pry-rails', '~> 0.3.6'
2424
s.add_development_dependency 'reek', '~> 6.0.6'
2525
s.add_development_dependency 'rspec-rails', '~> 3.8.0'

bug_report_template.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
99

1010
# Activate the gem you are reporting the issue against.
11-
gem 'rails', '~> 7.0'
11+
gem 'rails', '~> 7.1'
1212
gem 'sqlite3'
13-
gem 'active_storage_base64', '~> 2.0.0'
13+
gem 'active_storage_base64', '~> 3.0.0'
1414
end
1515

1616
require 'active_record/railtie'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
source 'https://rubygems.org'
22

3-
gem 'rails', '~> 7.0.0'
3+
gem 'rails', '~> 7.1.0.beta1'
44

55
gemspec path: '..'

lib/active_storage_support/support_for_base64.rb

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def #{name}
1717
1818
def #{name}=(attachable)
1919
attachment_changes["#{name}"] =
20-
if attachable.nil?
20+
if attachable.nil? || attachable == ""
2121
ActiveStorage::Attached::Changes::DeleteOne.new("#{name}", self)
2222
else
2323
ActiveStorage::Attached::Changes::CreateOne.new(
@@ -39,28 +39,15 @@ def #{name}
3939
end
4040
4141
def #{name}=(attachables)
42-
if ActiveStorage.replace_on_assign_to_many
43-
attachment_changes["#{name}"] =
44-
if Array(attachables).none?
45-
ActiveStorage::Attached::Changes::DeleteMany.new("#{name}", self)
46-
else
47-
ActiveStorage::Attached::Changes::CreateMany.new(
48-
"#{name}", self, ActiveStorageSupport::Base64Many.from_base64(attachables)
49-
)
50-
end
51-
else
52-
ActiveSupport::Deprecation.warn \
53-
"config.active_storage.replace_on_assign_to_many is deprecated and will be removed in Rails 7.1. " \
54-
"Make sure that your code works well with config.active_storage.replace_on_assign_to_many set to true before upgrading. " \
55-
"To append new attachables to the Active Storage association, prefer using `attach`. " \
56-
"Using association setter would result in purging the existing attached attachments and replacing them with new ones."
42+
attachables = Array(attachables).compact_blank
43+
pending_uploads = attachment_changes["#{name}"].try(:pending_uploads)
5744
58-
if Array(attachables).any?
59-
attachment_changes["#{name}"] =
60-
ActiveStorage::Attached::Changes::CreateMany.new(
61-
"#{name}", self, #{name}.blobs + ActiveStorageSupport::Base64Many.from_base64(attachables)
62-
)
63-
end
45+
attachment_changes["#{name}"] = if attachables.none?
46+
ActiveStorage::Attached::Changes::DeleteMany.new("#{name}", self)
47+
else
48+
ActiveStorage::Attached::Changes::CreateMany.new(
49+
"#{name}", self, ActiveStorageSupport::Base64Many.from_base64(attachables), pending_uploads: pending_uploads
50+
)
6451
end
6552
end
6653
CODE

spec/user_base64_spec.rb

Lines changed: 23 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,14 @@
446446
context 'when it is called with only one picture' do
447447
context 'when only data is specified' do
448448
it 'attaches a picture to the user' do
449-
user.pictures = base64_data
449+
user.pictures = [base64_data]
450450
user.save
451451

452452
expect(user.pictures.attached?).to be
453453
end
454454

455455
it 'attached file matches attachment file' do
456-
user.pictures = base64_data
456+
user.pictures = [base64_data]
457457
user.save
458458

459459
expect(
@@ -465,7 +465,7 @@
465465

466466
context 'when filename is specified' do
467467
it 'assigns the specified filename' do
468-
user.pictures = data_with_filename
468+
user.pictures = [data_with_filename]
469469
user.save
470470

471471
expect(user.pictures.first.filename.to_s).to eq(filename)
@@ -531,14 +531,14 @@
531531
context 'when it is called with only one picture' do
532532
context 'when only data is specified' do
533533
it 'attaches a picture to the user' do
534-
user.pictures = base64_data
534+
user.pictures = [base64_data]
535535
user.save
536536

537537
expect(user.pictures.attached?).to be
538538
end
539539

540540
it 'attached file matches attachment file' do
541-
user.pictures = base64_data
541+
user.pictures = [base64_data]
542542
user.save
543543

544544
expect(
@@ -550,7 +550,7 @@
550550

551551
context 'when filename is specified' do
552552
it 'assigns the specified filename' do
553-
user.pictures = data_with_filename
553+
user.pictures = [data_with_filename]
554554
user.save
555555

556556
expect(user.pictures.first.filename.to_s).to eq(filename)
@@ -610,13 +610,13 @@
610610
context 'when a single picture is passed' do
611611
context 'when only data is specified' do
612612
it 'attaches a picture' do
613-
user = User.create!(pictures: base64_data)
613+
user = User.create!(pictures: [base64_data])
614614

615615
expect(user.pictures.attached?).to be
616616
end
617617

618618
it 'attached file matches attachment file' do
619-
user = User.create!(pictures: base64_data)
619+
user = User.create!(pictures: [base64_data])
620620

621621
expect(
622622
File.open(ActiveStorage::Blob.service.send(:path_for,
@@ -627,7 +627,7 @@
627627

628628
context 'when a filename is specified' do
629629
it 'assigns the specified filename' do
630-
user = User.create!(pictures: data_with_filename)
630+
user = User.create!(pictures: [data_with_filename])
631631

632632
expect(user.pictures.first.filename.to_s).to eq(filename)
633633
end
@@ -662,13 +662,13 @@
662662
context 'when a single picture is passed' do
663663
context 'when only data is specified' do
664664
it 'attaches a picture' do
665-
user = User.create!(pictures: base64_data)
665+
user = User.create!(pictures: [base64_data])
666666

667667
expect(user.pictures.attached?).to be
668668
end
669669

670670
it 'attached file matches attachment file' do
671-
user = User.create!(pictures: base64_data)
671+
user = User.create!(pictures: [base64_data])
672672

673673
expect(
674674
File.open(ActiveStorage::Blob.service.send(:path_for,
@@ -679,7 +679,7 @@
679679

680680
context 'when a filename is specified' do
681681
it 'assigns the specified filename' do
682-
user = User.create!(pictures: data_with_filename)
682+
user = User.create!(pictures: [data_with_filename])
683683

684684
expect(user.pictures.first.filename.to_s).to eq(filename)
685685
end
@@ -704,7 +704,7 @@
704704
end
705705

706706
context 'when user has only one picture attached' do
707-
let(:user) { User.create!(pictures: base64_data) }
707+
let(:user) { User.create!(pictures: [base64_data]) }
708708

709709
context 'when the user wants to remove the picture' do
710710
it 'removes the picture' do
@@ -747,38 +747,10 @@
747747
end
748748
end
749749

750-
context 'when replacing on assign' do
751-
before do
752-
@previous = ActiveStorage.replace_on_assign_to_many
753-
ActiveStorage.replace_on_assign_to_many = true
754-
end
755-
756-
after do
757-
ActiveStorage.replace_on_assign_to_many = @previous
758-
end
759-
760-
it 'updates the existing record replacing attachments' do
761-
user.pictures = pictures_attachments
762-
user.save
763-
expect(user.pictures.count).to eq(2)
764-
end
765-
end
766-
767-
context 'when appending on assign' do
768-
before do
769-
@previous = ActiveStorage.replace_on_assign_to_many
770-
ActiveStorage.replace_on_assign_to_many = false
771-
end
772-
773-
after do
774-
ActiveStorage.replace_on_assign_to_many = @previous
775-
end
776-
777-
it 'updates the existing record appending the new attachments' do
778-
user.pictures = pictures_attachments
779-
user.save
780-
expect(user.pictures.count).to eq(4)
781-
end
750+
it 'updates the existing record replacing attachments' do
751+
user.pictures = pictures_attachments
752+
user.save
753+
expect(user.pictures.count).to eq(2)
782754
end
783755
end
784756
end
@@ -798,7 +770,7 @@
798770
params.permit(:data)
799771
end
800772
context 'when user has only one picture attached' do
801-
let(:user) { User.create!(pictures: base64_data) }
773+
let(:user) { User.create!(pictures: [base64_data]) }
802774

803775
context 'when the user wants to remove the picture' do
804776
it 'removes the picture' do
@@ -841,46 +813,18 @@
841813
end
842814
end
843815

844-
context 'when replacing on assign' do
845-
before do
846-
@previous = ActiveStorage.replace_on_assign_to_many
847-
ActiveStorage.replace_on_assign_to_many = true
848-
end
849-
850-
after do
851-
ActiveStorage.replace_on_assign_to_many = @previous
852-
end
853-
854-
it 'updates the existing record replacing attachments' do
855-
user.pictures = pictures_attachments
856-
user.save
857-
expect(user.pictures.count).to eq(2)
858-
end
859-
end
860-
861-
context 'when appending on assign' do
862-
before do
863-
@previous = ActiveStorage.replace_on_assign_to_many
864-
ActiveStorage.replace_on_assign_to_many = false
865-
end
866-
867-
after do
868-
ActiveStorage.replace_on_assign_to_many = @previous
869-
end
870-
871-
it 'updates the existing record appending the new attachments' do
872-
user.pictures = pictures_attachments
873-
user.save
874-
expect(user.pictures.count).to eq(4)
875-
end
816+
it 'updates the existing record replacing attachments' do
817+
user.pictures = pictures_attachments
818+
user.save
819+
expect(user.pictures.count).to eq(2)
876820
end
877821
end
878822
end
879823
end
880824
end
881825

882826
context 'when using a variant' do
883-
let(:user) { User.create!(pictures: base64_data) }
827+
let(:user) { User.create!(pictures: [base64_data]) }
884828

885829
it 'returns a link' do
886830
url = rails_url.rails_blob_url(user.pictures.first.variant(:thumb).processed)

spec/user_file_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@
174174
context 'when "user.pictures=" is called' do
175175
context 'when it is called with only one picture' do
176176
it 'attaches a picture to the user' do
177-
user.pictures = file
177+
user.pictures = [file]
178178
user.save
179179

180180
expect(user.pictures.attached?).to be
181181
end
182182

183183
it 'assigns the specified filename' do
184-
user.pictures = file
184+
user.pictures = [file]
185185
user.save
186186

187187
expect(user.pictures.first.filename.to_s).to eq(filename)
@@ -191,14 +191,14 @@
191191
context 'when it is called with more than one picture' do
192192
context 'when called with an array' do
193193
it 'attaches an array of pictures to the user' do
194-
user.pictures = pictures_attachments
194+
user.pictures = [pictures_attachments]
195195
user.save
196196

197197
expect(user.pictures.count).to eq(2)
198198
end
199199

200200
it 'assigns the specified filename' do
201-
user.pictures = pictures_attachments
201+
user.pictures = [pictures_attachments]
202202
user.save
203203

204204
expect(user.pictures.first.filename).to eq(filename)
@@ -228,13 +228,13 @@
228228
context 'when pictures are passed as a hash parameter' do
229229
context 'when a single picture is passed' do
230230
it 'attaches a picture' do
231-
user = User.create!(pictures: file)
231+
user = User.create!(pictures: [file])
232232

233233
expect(user.pictures.attached?).to be
234234
end
235235

236236
it 'assigns the specified filename' do
237-
user = User.create!(pictures: file)
237+
user = User.create!(pictures: [file])
238238

239239
expect(user.pictures.first.filename).to eq(filename)
240240
end
@@ -258,7 +258,7 @@
258258

259259
context 'when user already has pictures attached' do
260260
context 'when user has only one picture attached' do
261-
let(:user) { User.create!(pictures: file) }
261+
let(:user) { User.create!(pictures: [file]) }
262262

263263
context 'when the user wants to remove the picture' do
264264
it 'removes the picture' do

0 commit comments

Comments
 (0)