Skip to content

Commit 78af5b4

Browse files
author
Mattia Roccoberton
committed
Move Rails version specific code in separated modules
1 parent fc459a2 commit 78af5b4

5 files changed

Lines changed: 129 additions & 40 deletions

File tree

lib/active_storage/service/db_service.rb

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
# frozen_string_literal: true
22

3+
require 'active_storage/service/db_service_rails60'
4+
require 'active_storage/service/db_service_rails61'
5+
require 'active_storage/service/db_service_rails70'
6+
37
module ActiveStorage
48
# Wraps a DB table as an Active Storage service. See ActiveStorage::Service
59
# for the generic API documentation that applies to all services.
610
class Service::DBService < Service
7-
def initialize(**)
11+
if Rails::VERSION::MAJOR >= 7
12+
include ActiveStorage::DBServiceRails70
13+
elsif Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR == 1
14+
include ActiveStorage::DBServiceRails61
15+
else
16+
include ActiveStorage::DBServiceRails60
17+
end
18+
19+
def initialize(public: false, **)
820
@chunk_size = ENV.fetch('ASDB_CHUNK_SIZE') { 1.megabytes }
21+
@public = public
922
end
1023

1124
def upload(key, io, checksum: nil, **)
@@ -62,17 +75,6 @@ def exist?(key)
6275
end
6376
end
6477

65-
if Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR == 0
66-
def url(key, expires_in:, filename:, disposition:, content_type:)
67-
instrument :url, key: key do |payload|
68-
opts = { expires_in: expires_in, filename: filename, content_type: content_type, disposition: disposition }
69-
generate_url(key, opts).tap do |generated_url|
70-
payload[:url] = generated_url
71-
end
72-
end
73-
end
74-
end
75-
7678
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {})
7779
instrument :url, key: key do |payload|
7880
verified_token_with_expiration = ActiveStorage.verifier.generate(
@@ -99,25 +101,6 @@ def headers_for_direct_upload(_key, content_type:, **)
99101

100102
private
101103

102-
def current_host
103-
if ActiveStorage::Current.respond_to? :url_options
104-
opts = ActiveStorage::Current.url_options || {}
105-
url = "#{opts[:protocol]}#{opts[:host]}"
106-
url += ":#{opts[:port]}" if opts[:port]
107-
url || ''
108-
else
109-
ActiveStorage::Current.host
110-
end
111-
end
112-
113-
def private_url(key, expires_in:, filename:, content_type:, disposition:, **)
114-
generate_url(key, expires_in: expires_in, filename: filename, content_type: content_type, disposition: disposition)
115-
end
116-
117-
def public_url(key, filename:, content_type: nil, disposition: :attachment, **)
118-
generate_url(key, expires_in: nil, filename: filename, content_type: content_type, disposition: disposition)
119-
end
120-
121104
def generate_url(key, expires_in:, filename:, content_type:, disposition:)
122105
content_disposition = content_disposition_with(type: disposition, filename: filename)
123106
verified_key_with_expiration = ActiveStorage.verifier.generate(
@@ -164,13 +147,5 @@ def stream(key)
164147
def url_helpers
165148
@url_helpers ||= ::ActiveStorageDB::Engine.routes.url_helpers
166149
end
167-
168-
def url_options
169-
if ActiveStorage::Current.respond_to? :url_options
170-
ActiveStorage::Current.url_options
171-
else
172-
{ host: ActiveStorage::Current.host }
173-
end
174-
end
175150
end
176151
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveStorage
4+
module DBServiceRails60
5+
def url(key, expires_in:, filename:, disposition:, content_type:)
6+
instrument :url, key: key do |payload|
7+
generate_url(key, expires_in: expires_in, filename: filename, content_type: content_type, disposition: disposition).tap do |generated_url|
8+
payload[:url] = generated_url
9+
end
10+
end
11+
end
12+
13+
private
14+
15+
def current_host
16+
url_options[:host]
17+
end
18+
19+
def url_options
20+
{
21+
host: ActiveStorage::Current.host
22+
}
23+
end
24+
end
25+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveStorage
4+
module DBServiceRails61
5+
private
6+
7+
def current_host
8+
url_options[:host]
9+
end
10+
11+
def private_url(key, expires_in:, filename:, content_type:, disposition:, **)
12+
generate_url(
13+
key,
14+
expires_in: expires_in,
15+
filename: filename,
16+
content_type: content_type,
17+
disposition: disposition
18+
)
19+
end
20+
21+
def public_url(key, filename:, content_type: nil, disposition: :attachment, **)
22+
generate_url(
23+
key,
24+
expires_in: nil,
25+
filename: filename,
26+
content_type: content_type,
27+
disposition: disposition
28+
)
29+
end
30+
31+
def url_options
32+
{
33+
host: ActiveStorage::Current.host
34+
}
35+
end
36+
end
37+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveStorage
4+
module DBServiceRails70
5+
private
6+
7+
def current_host
8+
opts = url_options || {}
9+
url = "#{opts[:protocol]}#{opts[:host]}"
10+
url + ":#{opts[:port]}" if opts[:port]
11+
end
12+
13+
def private_url(key, expires_in:, filename:, content_type:, disposition:, **)
14+
generate_url(
15+
key,
16+
expires_in: expires_in,
17+
filename: filename,
18+
content_type: content_type,
19+
disposition: disposition
20+
)
21+
end
22+
23+
def public_url(key, filename:, content_type: nil, disposition: :attachment, **)
24+
generate_url(
25+
key,
26+
expires_in: nil,
27+
filename: filename,
28+
content_type: content_type,
29+
disposition: disposition
30+
)
31+
end
32+
33+
def url_options
34+
ActiveStorage::Current.url_options
35+
end
36+
end
37+
end

spec/rails_helper.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
add_filter %r{^/lib/active_storage_db/version.rb}
77
add_filter %r{^/spec/}
88
add_filter %r{^/vendor/}
9+
10+
case ENV['RAILS']
11+
when '6.0' then add_filter /_rails61|_rails70/
12+
when '6.1' then add_filter /_rails60|_rails70/
13+
when '7.0' then add_filter /_rails60|_rails61/
14+
end
915
end
1016

1117
require 'spec_helper'
@@ -35,13 +41,22 @@
3541
config.use_transactional_fixtures = true
3642

3743
config.before(:suite) do
44+
db_config =
45+
if ActiveRecord::Base.respond_to? :connection_db_config
46+
ActiveRecord::Base.connection_db_config.configuration_hash
47+
else
48+
ActiveRecord::Base.connection_config
49+
end
50+
3851
intro = ('-' * 80)
3952
intro << "\n"
4053
intro << "- Ruby: #{RUBY_VERSION}\n"
4154
intro << "- Rails: #{Rails.version}\n"
4255
intro << "- ActiveStorage: #{ActiveStorage.version}\n"
43-
intro << "- DB_TEST: #{ENV['DB_TEST']}\n"
56+
intro << "- DB adapter: #{db_config[:adapter]}\n"
57+
intro << "- DB name: #{db_config[:database]}\n"
4458
intro << ('-' * 80)
59+
4560
RSpec.configuration.reporter.message(intro)
4661
end
4762
end

0 commit comments

Comments
 (0)