Skip to content

Commit 8a3fa9b

Browse files
committed
REW-2045 - update set_current_host method to include test environment
Allow name of attachment to be dynamically set using config file
1 parent dcb2076 commit 8a3fa9b

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

app/models/concerns/csv2db/active_storage_adapter.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ module Csv2db::ActiveStorageAdapter
66
MAX_EXPIRY = 7.days.to_s.freeze
77

88
included do
9-
has_one_attached :csv_upload
9+
has_one_attached Csv2db.config.file_attachment_name
1010

1111
validate :check_file_extension
12+
13+
alias_method :file_attachment, Csv2db.config.file_attachment_name
1214
end
1315

1416
def file=(file)
@@ -17,7 +19,7 @@ def file=(file)
1719

1820
filename = file.original_filename
1921

20-
csv_upload.attach(
22+
file_attachment.attach(
2123
io: File.open(file),
2224
filename: filename,
2325
content_type: file.content_type
@@ -27,30 +29,30 @@ def file=(file)
2729
end
2830

2931
def expiring_link(expires_in: MAX_EXPIRY)
30-
return unless csv_upload.present?
32+
return unless file_attachment.present?
3133

32-
set_current_host_if_local
34+
set_current_host
3335

34-
csv_upload.service_url(expires_in: expires_in.to_i, disposition: 'attachment')
36+
file_attachment.service_url(expires_in: expires_in.to_i, disposition: 'attachment')
3537
end
3638

3739
private
3840

39-
def set_current_host_if_local
40-
return unless Rails.application.config.active_storage.service == :local
41+
def set_current_host
42+
return unless %i[test local].include?(Rails.application.config.active_storage.service)
4143

4244
ActiveStorage::Current.host = ReportGenerator.config.local_storage_host
4345
end
4446

4547
def check_file_extension
4648
# very basic check of file extension
47-
errors.add(:file, I18n.t('shared.file_processor.incorrect_file_type')) unless csv_upload.blob.content_type == FILE_TYPE
49+
errors.add(:file, I18n.t('shared.file_processor.incorrect_file_type')) unless file_attachment.blob.content_type == FILE_TYPE
4850
end
4951

5052
def file_data
5153
return @file_data if @file_data.present?
5254

53-
csv_upload.blob.open do |blob|
55+
file_attachment.blob.open do |blob|
5456
@file_data = str_to_utf8(blob.read)
5557
end
5658

lib/csv2db/config.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Csv2db
44
class Config
55
include Singleton
66

7-
attr_writer :storage_adapter, :local_storage_host
7+
attr_writer :storage_adapter, :local_storage_host, :file_attachment_name
88

99
def storage_adapter
1010
@storage_adapter ||= :dragonfly
@@ -14,5 +14,9 @@ def storage_adapter
1414
def local_storage_host
1515
@local_storage_host ||= ''
1616
end
17+
18+
def file_attachment_name
19+
@file_attachment_name ||= :csv_attachment
20+
end
1721
end
1822
end

spec/models/concerns/csv2db/import_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class TestModel < ActiveRecord::Base
111111
end
112112

113113
let(:attachment_spy) do
114-
spy('csv_upload')
114+
spy('file_attachment')
115115
end
116116

117117
subject do
@@ -121,7 +121,7 @@ class TestModel < ActiveRecord::Base
121121
before do
122122
allow(TestModel).to receive(:has_one_attached)
123123
TestModel.include(Csv2db::ActiveStorageAdapter)
124-
allow(subject).to receive(:csv_upload).and_return(attachment_spy)
124+
allow(subject).to receive(:file_attachment).and_return(attachment_spy)
125125
end
126126

127127
it 'calls correct attach methods' do

0 commit comments

Comments
 (0)