Skip to content

Commit 4b9d2e6

Browse files
authored
Merge pull request #36 from rootstrap/support-rails-6
Support Rails 6
2 parents 2794d49 + d9dcec1 commit 4b9d2e6

File tree

10 files changed

+110
-33
lines changed

10 files changed

+110
-33
lines changed

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Metrics/BlockLength:
1919
Exclude:
2020
- config/**/*
2121
- spec/**/*
22+
ExcludedMethods:
23+
- class_methods
2224

2325
Metrics/BlockNesting:
2426
Max: 4

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ before_install:
44
- gem install bundler -v "~> 1.17"
55

66
rvm:
7-
- 2.2.8
8-
- 2.3.5
9-
- 2.4.2
107
- 2.5.0
8+
- 2.6.0
119
- ruby-head
1210

1311
matrix:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class UsersController < ApplicationController
9696
def create
9797
user = User.create(user_params)
9898
user.avatar = { data: params[:avatar] }
99+
user.save
99100
end
100101

101102
private

active_storage_base64.gemspec

Lines changed: 3 additions & 3 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 = '0.1.4'
3+
s.version = '1.0.0'
44
s.summary = 'Base64 support for ActiveStorage'
55
s.description = s.summary
66

@@ -17,13 +17,13 @@ Gem::Specification.new do |s|
1717
s.required_ruby_version = '>= 2.2.2'
1818

1919
# Dependencies
20-
s.add_dependency 'rails', '~> 5.2'
20+
s.add_dependency 'rails', '~> 6.0'
2121

2222
# Development dependencies
2323
s.add_development_dependency 'pry-rails', '~> 0.3.6'
2424
s.add_development_dependency 'reek', '~> 4.8.1'
2525
s.add_development_dependency 'rspec-rails', '~> 3.8.0'
2626
s.add_development_dependency 'rubocop', '~> 0.56.0'
2727
s.add_development_dependency 'simplecov'
28-
s.add_development_dependency 'sqlite3', '1.3.13'
28+
s.add_development_dependency 'sqlite3', '1.4.1'
2929
end

config.reek

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ ClassVariable:
99
exclude: []
1010
ControlParameter:
1111
enabled: true
12-
exclude: ['ActiveStorageSupport::SupportForBase64#add_helper_method']
1312
DataClump:
1413
enabled: true
15-
exclude: ['ActiveStorageSupport::SupportForBase64']
1614
max_copies: 2
1715
min_clump_size: 2
1816
DuplicateMethodCall:

lib/active_storage_support/base64_many.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
module ActiveStorageSupport
22
class Base64Many < ActiveStorage::Attached::Many
33
def attach(*attachables)
4-
super base64_attachments(attachables)
4+
super self.class.from_base64(attachables)
55
end
66

7-
def base64_attachments(attachments)
8-
attachments.flatten.map do |attachment|
9-
ActiveStorageSupport::Base64Attach.attachment_from_data(attachment)
7+
def self.from_base64(attachables)
8+
attachables = [attachables] unless attachables.is_a?(Array)
9+
10+
attachables.flatten.map do |attachable|
11+
ActiveStorageSupport::Base64Attach.attachment_from_data(attachable)
1012
end
1113
end
1214
end
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
module ActiveStorageSupport
22
class Base64One < ActiveStorage::Attached::One
33
def attach(attachable)
4-
attachment = ActiveStorageSupport::Base64Attach.attachment_from_data(attachable)
5-
super attachment
4+
super self.class.from_base64(attachable)
5+
end
6+
7+
def self.from_base64(attachable)
8+
ActiveStorageSupport::Base64Attach.attachment_from_data(attachable)
69
end
710
end
811
end

lib/active_storage_support/support_for_base64.rb

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,48 @@ module SupportForBase64
88
def has_one_base64_attached(name, dependent: :purge_later)
99
has_one_attached name, dependent: dependent
1010

11-
add_helper_method(ActiveStorageSupport::Base64One, name, dependent: dependent)
11+
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
12+
def #{name}
13+
@active_storage_attached_#{name} ||= ActiveStorageSupport::Base64One.new("#{name}", self)
14+
end
15+
def #{name}=(attachable)
16+
attachment_changes["#{name}"] =
17+
if attachable.nil?
18+
ActiveStorage::Attached::Changes::DeleteOne.new("#{name}", self)
19+
else
20+
ActiveStorage::Attached::Changes::CreateOne.new(
21+
"#{name}", self, ActiveStorageSupport::Base64One.from_base64(attachable)
22+
)
23+
end
24+
end
25+
CODE
1226
end
1327

1428
def has_many_base64_attached(name, dependent: :purge_later)
1529
has_many_attached name, dependent: dependent
1630

17-
add_helper_method(ActiveStorageSupport::Base64Many, name, dependent: dependent)
18-
end
19-
20-
def add_helper_method(type, name, dependent:)
21-
class_eval <<-CODE, __FILE__, __LINE__ + 1
31+
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
2232
def #{name}
23-
@active_storage_attached_#{name} ||=
24-
#{type}.new("#{name}", self, dependent: #{dependent == :purge_later ? ':purge_later' : 'false'})
33+
@active_storage_attached_#{name} ||= ActiveStorageSupport::Base64Many.new("#{name}", self)
2534
end
26-
27-
def #{name}=(data)
28-
#{name}.attach(data)
35+
def #{name}=(attachables)
36+
if ActiveStorage.replace_on_assign_to_many
37+
attachment_changes["#{name}"] =
38+
if Array(attachables).none?
39+
ActiveStorage::Attached::Changes::DeleteMany.new("#{name}", self)
40+
else
41+
ActiveStorage::Attached::Changes::CreateMany.new(
42+
"#{name}", self, ActiveStorageSupport::Base64Many.from_base64(attachables)
43+
)
44+
end
45+
else
46+
if Array(attachables).any?
47+
attachment_changes["#{name}"] =
48+
ActiveStorage::Attached::Changes::CreateMany.new(
49+
"#{name}", self, #{name}.blobs + ActiveStorageSupport::Base64Many.from_base64(attachables)
50+
)
51+
end
52+
end
2953
end
3054
CODE
3155
end

spec/user_base64_spec.rb

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@
5656
context 'when only data is specified' do
5757
it 'attaches an avatar to the user' do
5858
user.avatar = base64_data
59+
user.save
5960

6061
expect(user.avatar.attached?).to be
6162
end
6263

6364
it 'attached file matches attachment file' do
6465
user.avatar = base64_data
66+
user.save
6567

6668
expect(
6769
File.open(ActiveStorage::Blob.service.send(:path_for, user.avatar.key)).read
@@ -72,6 +74,7 @@
7274
context 'when filename is specified along with data' do
7375
it 'assigns the specified filename' do
7476
user.avatar = data_with_filename
77+
user.save
7578

7679
expect(user.avatar.filename.to_s).to eq(filename)
7780
end
@@ -218,12 +221,14 @@
218221
context 'when only data is specified' do
219222
it 'attaches a picture to the user' do
220223
user.pictures = base64_data
224+
user.save
221225

222226
expect(user.pictures.attached?).to be
223227
end
224228

225229
it 'attached file matches attachment file' do
226230
user.pictures = base64_data
231+
user.save
227232

228233
expect(
229234
File.open(ActiveStorage::Blob.service.send(:path_for, user.pictures.last.key)).read
@@ -234,6 +239,7 @@
234239
context 'when filename is specified' do
235240
it 'assigns the specified filename' do
236241
user.pictures = data_with_filename
242+
user.save
237243

238244
expect(user.pictures.first.filename.to_s).to eq(filename)
239245
end
@@ -244,21 +250,23 @@
244250
context 'when only data is specified' do
245251
it 'attaches an array of pictures to the user' do
246252
user.pictures = pictures_attachments
253+
user.save
247254

248255
expect(user.pictures.count).to eq(2)
249256
end
250257

251258
it 'attached file matches attachment file' do
252259
user.pictures = pictures_attachments
260+
user.save
253261

254262
expect(
255263
File.open(ActiveStorage::Blob.service.send(:path_for, user.pictures.last.key)).read
256264
).to match(second_file)
257265
end
258266

259267
it 'attaches multiple individual pictures to the user' do
260-
user.pictures = base64_data
261-
user.pictures = second_base64_data
268+
user.pictures = [base64_data, second_base64_data]
269+
user.save
262270

263271
expect(user.pictures.count).to eq(2)
264272
end
@@ -267,14 +275,15 @@
267275
context 'when pictures have a specified filename' do
268276
it 'assigns the specified filename for the array of pictures' do
269277
user.pictures = attachments_with_filename
278+
user.save
270279

271280
expect(user.pictures.first.filename.to_s).to eq(filename)
272281
expect(user.pictures.second.filename.to_s).to eq(second_filename)
273282
end
274283

275284
it 'assigns the specified filename for each individual picture' do
276-
user.pictures = data_with_filename
277-
user.pictures = second_data_with_filename
285+
user.pictures = [data_with_filename, second_data_with_filename]
286+
user.save
278287

279288
expect(user.pictures.first.filename.to_s).to eq(filename)
280289
expect(user.pictures.second.filename.to_s).to eq(second_filename)
@@ -358,6 +367,40 @@
358367
expect(first_url).not_to eq(second_url)
359368
end
360369
end
370+
371+
context 'when replacing on assign' do
372+
before do
373+
@previous = ActiveStorage.replace_on_assign_to_many
374+
ActiveStorage.replace_on_assign_to_many = true
375+
end
376+
377+
after do
378+
ActiveStorage.replace_on_assign_to_many = @previous
379+
end
380+
381+
it 'updates the existing record replacing attachments' do
382+
user.pictures = pictures_attachments
383+
user.save
384+
expect(user.pictures.count).to eq(2)
385+
end
386+
end
387+
388+
context 'when appending on assign' do
389+
before do
390+
@previous = ActiveStorage.replace_on_assign_to_many
391+
ActiveStorage.replace_on_assign_to_many = false
392+
end
393+
394+
after do
395+
ActiveStorage.replace_on_assign_to_many = @previous
396+
end
397+
398+
it 'updates the existing record appending the new attachments' do
399+
user.pictures = pictures_attachments
400+
user.save
401+
expect(user.pictures.count).to eq(4)
402+
end
403+
end
361404
end
362405
end
363406
end

spec/user_file_spec.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@
3737
context 'when "user.avatar=" is called' do
3838
it 'attaches an avatar to the user' do
3939
user.avatar = file
40+
user.save
4041

4142
expect(user.avatar.attached?).to be
4243
end
4344

4445
it 'assigns the specified filename' do
4546
user.avatar = file
47+
user.save
4648

4749
expect(user.avatar.filename.to_s).to eq(filename)
4850
end
@@ -163,12 +165,14 @@
163165
context 'when it is called with only one picture' do
164166
it 'attaches a picture to the user' do
165167
user.pictures = file
168+
user.save
166169

167170
expect(user.pictures.attached?).to be
168171
end
169172

170173
it 'assigns the specified filename' do
171174
user.pictures = file
175+
user.save
172176

173177
expect(user.pictures.first.filename.to_s).to eq(filename)
174178
end
@@ -178,12 +182,14 @@
178182
context 'when called with an array' do
179183
it 'attaches an array of pictures to the user' do
180184
user.pictures = pictures_attachments
185+
user.save
181186

182187
expect(user.pictures.count).to eq(2)
183188
end
184189

185190
it 'assigns the specified filename' do
186191
user.pictures = pictures_attachments
192+
user.save
187193

188194
expect(user.pictures.first.filename).to eq(filename)
189195
expect(user.pictures.second.filename).to eq(second_filename)
@@ -192,15 +198,15 @@
192198

193199
context 'when called with muiltiple individual pictures' do
194200
it 'attaches multiple individual pictures to the user' do
195-
user.pictures = file
196-
user.pictures = second_file
201+
user.pictures = [file, second_file]
202+
user.save
197203

198204
expect(user.pictures.count).to eq(2)
199205
end
200206

201207
it 'assigns the specified filename' do
202-
user.pictures = file
203-
user.pictures = second_file
208+
user.pictures = [file, second_file]
209+
user.save
204210

205211
expect(user.pictures.first.filename).to eq(filename)
206212
expect(user.pictures.second.filename).to eq(second_filename)

0 commit comments

Comments
 (0)