Skip to content

Commit ed5aad4

Browse files
authored
Variants support (#69)
* Add support for variants * Fix CI * Bump gem version
1 parent 5dee67a commit ed5aad4

File tree

8 files changed

+83
-42
lines changed

8 files changed

+83
-42
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
gemfile: [rails_6_1.gemfile, rails_head.gemfile]
12-
ruby_version: [2.5, 2.6, 2.7, 3.0]
13-
exclude:
14-
- gemfile: rails_head.gemfile
15-
ruby_version: 2.5
16-
- gemfile: rails_head.gemfile
17-
ruby_version: 2.6
11+
gemfile: [rails_7_0.gemfile, rails_head.gemfile]
12+
ruby_version: [2.7, 3.0]
1813
env:
1914
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}
2015
CC_TEST_REPORTER_ID: 7196b4aa257fde33f24463218af32db6a6efd23d9148204822f757fa614a093e

active_storage_base64.gemspec

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

77
s.files = Dir['LICENSE.txt', 'README.md', 'lib/**/*']
88

99
s.require_paths = ['lib']
10-
s.authors = ['Ricardo Cortio']
10+
s.authors = ['Ricardo Cortio', 'Santiago Bartesaghi']
1111
s.license = 'MIT'
1212
s.homepage = 'https://github.com/rootstrap/active-storage-base64'
13-
s.email = 'ricardo@rootstrap.com'
13+
s.email = ['ricardo@rootstrap.com', 'santiago.bartesaghi@rootstrap.com']
1414

15-
s.required_ruby_version = '>= 2.5.0'
15+
s.required_ruby_version = '>= 2.7.0'
1616

1717
# Dependencies
18-
s.add_dependency 'rails', '>= 6.1'
18+
s.add_dependency 'rails', '>= 7.0'
1919

2020
# Development dependencies
2121
s.add_development_dependency 'pry-rails', '~> 0.3.6'
@@ -24,4 +24,5 @@ Gem::Specification.new do |s|
2424
s.add_development_dependency 'rubocop', '~> 1.22.0'
2525
s.add_development_dependency 'simplecov', '~> 0.17.1'
2626
s.add_development_dependency 'sqlite3', '1.4.2'
27+
s.add_development_dependency 'image_processing', '~> 1.2'
2728
end

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', '~> 6.1'
11+
gem 'rails', '~> 7.0'
1212
gem 'sqlite3'
13-
gem 'active_storage_base64', '~> 1.2.0'
13+
gem 'active_storage_base64', '~> 2.0.0'
1414
end
1515

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

3-
gem 'rails', '~> 6.1.0'
3+
gem 'rails', '~> 7.0.0'
44

55
gemspec path: '..'

lib/active_storage_support/support_for_base64.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module ActiveStorageSupport
55
module SupportForBase64
66
extend ActiveSupport::Concern
77
class_methods do
8-
def has_one_base64_attached(name, dependent: :purge_later, service: nil, strict_loading: false)
9-
has_one_attached name, dependent: dependent, service: service, strict_loading: strict_loading
8+
def has_one_base64_attached(name, dependent: :purge_later, service: nil, strict_loading: false, &block)
9+
has_one_attached name, dependent: dependent, service: service, strict_loading: strict_loading, &block
1010

1111
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
1212
# frozen_string_literal: true
@@ -28,8 +28,8 @@ def #{name}=(attachable)
2828
CODE
2929
end
3030

31-
def has_many_base64_attached(name, dependent: :purge_later, service: nil, strict_loading: false)
32-
has_many_attached name, dependent: dependent, service: service, strict_loading: strict_loading
31+
def has_many_base64_attached(name, dependent: :purge_later, service: nil, strict_loading: false, &block)
32+
has_many_attached name, dependent: dependent, service: service, strict_loading: strict_loading, &block
3333

3434
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
3535
# frozen_string_literal: true

spec/dummy/app/models/user.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
class User < ApplicationRecord
2-
has_one_base64_attached :avatar
3-
has_many_base64_attached :pictures
2+
has_one_base64_attached :avatar do |attachable|
3+
attachable.variant :thumb, resize_to_fit: [1, 1]
4+
end
5+
6+
has_many_base64_attached :pictures do |attachable|
7+
attachable.variant :thumb, resize_to_fit: [1, 1]
8+
end
49
end

spec/user_base64_spec.rb

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@
250250
end
251251
end
252252
end
253+
254+
context 'when using a variant' do
255+
let(:user) { User.create!(avatar: base64_data) }
256+
257+
it 'returns a link' do
258+
url = rails_url.rails_blob_url(user.avatar.variant(:thumb).processed)
259+
260+
expect(url).to be
261+
end
262+
end
253263
end
254264

255265
context 'when user uses pictures' do
@@ -600,13 +610,13 @@
600610
context 'when a single picture is passed' do
601611
context 'when only data is specified' do
602612
it 'attaches a picture' do
603-
user = User.create(pictures: base64_data)
613+
user = User.create!(pictures: base64_data)
604614

605615
expect(user.pictures.attached?).to be
606616
end
607617

608618
it 'attached file matches attachment file' do
609-
user = User.create(pictures: base64_data)
619+
user = User.create!(pictures: base64_data)
610620

611621
expect(
612622
File.open(ActiveStorage::Blob.service.send(:path_for,
@@ -617,7 +627,7 @@
617627

618628
context 'when a filename is specified' do
619629
it 'assigns the specified filename' do
620-
user = User.create(pictures: data_with_filename)
630+
user = User.create!(pictures: data_with_filename)
621631

622632
expect(user.pictures.first.filename.to_s).to eq(filename)
623633
end
@@ -626,7 +636,7 @@
626636

627637
context 'when an array of pictures is passed' do
628638
it 'attaches multiple pictures' do
629-
user = User.create(pictures: attachments_with_filename)
639+
user = User.create!(pictures: attachments_with_filename)
630640

631641
expect(user.pictures.first.filename.to_s).to eq(filename)
632642
expect(user.pictures.second.filename.to_s).to eq(second_filename)
@@ -652,13 +662,13 @@
652662
context 'when a single picture is passed' do
653663
context 'when only data is specified' do
654664
it 'attaches a picture' do
655-
user = User.create(pictures: base64_data)
665+
user = User.create!(pictures: base64_data)
656666

657667
expect(user.pictures.attached?).to be
658668
end
659669

660670
it 'attached file matches attachment file' do
661-
user = User.create(pictures: base64_data)
671+
user = User.create!(pictures: base64_data)
662672

663673
expect(
664674
File.open(ActiveStorage::Blob.service.send(:path_for,
@@ -669,7 +679,7 @@
669679

670680
context 'when a filename is specified' do
671681
it 'assigns the specified filename' do
672-
user = User.create(pictures: data_with_filename)
682+
user = User.create!(pictures: data_with_filename)
673683

674684
expect(user.pictures.first.filename.to_s).to eq(filename)
675685
end
@@ -678,7 +688,7 @@
678688

679689
context 'when an array of pictures is passed' do
680690
it 'attaches multiple pictures' do
681-
user = User.create(pictures: attachments_with_filename)
691+
user = User.create!(pictures: attachments_with_filename)
682692

683693
expect(user.pictures.first.filename.to_s).to eq(filename)
684694
expect(user.pictures.second.filename.to_s).to eq(second_filename)
@@ -694,7 +704,7 @@
694704
end
695705

696706
context 'when user has only one picture attached' do
697-
let(:user) { User.create(pictures: base64_data) }
707+
let(:user) { User.create!(pictures: base64_data) }
698708

699709
context 'when the user wants to remove the picture' do
700710
it 'removes the picture' do
@@ -706,7 +716,7 @@
706716
end
707717

708718
context 'when user has multiple pictures attached' do
709-
let(:user) { User.create(pictures: pictures_attachments) }
719+
let(:user) { User.create!(pictures: pictures_attachments) }
710720

711721
context 'when user wants to remove the pictures' do
712722
it 'removes the pictures' do
@@ -788,7 +798,7 @@
788798
params.permit(:data)
789799
end
790800
context 'when user has only one picture attached' do
791-
let(:user) { User.create(pictures: base64_data) }
801+
let(:user) { User.create!(pictures: base64_data) }
792802

793803
context 'when the user wants to remove the picture' do
794804
it 'removes the picture' do
@@ -800,7 +810,7 @@
800810
end
801811

802812
context 'when user has multiple pictures attached' do
803-
let(:user) { User.create(pictures: pictures_attachments) }
813+
let(:user) { User.create!(pictures: pictures_attachments) }
804814

805815
context 'when user wants to remove the pictures' do
806816
it 'removes the pictures' do
@@ -868,5 +878,15 @@
868878
end
869879
end
870880
end
881+
882+
context 'when using a variant' do
883+
let(:user) { User.create!(pictures: base64_data) }
884+
885+
it 'returns a link' do
886+
url = rails_url.rails_blob_url(user.pictures.first.variant(:thumb).processed)
887+
888+
expect(url).to be
889+
end
890+
end
871891
end
872892
end

spec/user_file_spec.rb

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@
5252

5353
context 'when the avatar is sent as a hash parameter to the user' do
5454
it 'attaches an avatar to the user' do
55-
user = User.create(avatar: file)
55+
user = User.create!(avatar: file)
5656

5757
expect(user.avatar.attached?).to be
5858
end
5959

6060
it 'assigns the specified filename' do
61-
user = User.create(avatar: file)
61+
user = User.create!(avatar: file)
6262

6363
expect(user.avatar.filename.to_s).to eq(filename)
6464
end
@@ -74,7 +74,7 @@
7474
end
7575

7676
context 'when user has an avatar attached' do
77-
let(:user) { User.create(avatar: file) }
77+
let(:user) { User.create!(avatar: file) }
7878

7979
context 'when the user wants to remove the avatar' do
8080
it 'removes the avatar' do
@@ -92,6 +92,16 @@
9292
end
9393
end
9494
end
95+
96+
context 'when using a variant' do
97+
let(:user) { User.create!(avatar: file) }
98+
99+
it 'returns a link' do
100+
url = rails_url.rails_blob_url(user.avatar.variant(:thumb).processed)
101+
102+
expect(url).to be
103+
end
104+
end
95105
end
96106

97107
context 'when user uses pictures' do
@@ -218,27 +228,27 @@
218228
context 'when pictures are passed as a hash parameter' do
219229
context 'when a single picture is passed' do
220230
it 'attaches a picture' do
221-
user = User.create(pictures: file)
231+
user = User.create!(pictures: file)
222232

223233
expect(user.pictures.attached?).to be
224234
end
225235

226236
it 'assigns the specified filename' do
227-
user = User.create(pictures: file)
237+
user = User.create!(pictures: file)
228238

229239
expect(user.pictures.first.filename).to eq(filename)
230240
end
231241
end
232242

233243
context 'when an array of pictures is passed' do
234244
it 'attaches multiple pictures' do
235-
user = User.create(pictures: pictures_attachments)
245+
user = User.create!(pictures: pictures_attachments)
236246

237247
expect(user.pictures.count).to eq(2)
238248
end
239249

240250
it 'assigns the specified filename' do
241-
user = User.create(pictures: pictures_attachments)
251+
user = User.create!(pictures: pictures_attachments)
242252

243253
expect(user.pictures.first.filename).to eq(filename)
244254
expect(user.pictures.second.filename).to eq(second_filename)
@@ -248,7 +258,7 @@
248258

249259
context 'when user already has pictures attached' do
250260
context 'when user has only one picture attached' do
251-
let(:user) { User.create(pictures: file) }
261+
let(:user) { User.create!(pictures: file) }
252262

253263
context 'when the user wants to remove the picture' do
254264
it 'removes the picture' do
@@ -260,7 +270,7 @@
260270
end
261271

262272
context 'when user has multiple pictures attached' do
263-
let(:user) { User.create(pictures: pictures_attachments) }
273+
let(:user) { User.create!(pictures: pictures_attachments) }
264274

265275
context 'when user wants to remove the pictures' do
266276
it 'removes the pictures' do
@@ -287,5 +297,15 @@
287297
end
288298
end
289299
end
300+
301+
context 'when using a variant' do
302+
let(:user) { User.create!(pictures: pictures_attachments) }
303+
304+
it 'returns a link' do
305+
url = rails_url.rails_blob_url(user.pictures.first.variant(:thumb).processed)
306+
307+
expect(url).to be
308+
end
309+
end
290310
end
291311
end

0 commit comments

Comments
 (0)