diff --git a/.rubocop.yml b/.rubocop.yml index e04cbea..f6766b4 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -13,7 +13,7 @@ AllCops: - spec/dummy??/**/* - vendor/**/* SuggestExtensions: false - TargetRubyVersion: 2.7 + TargetRubyVersion: 3.0 Lint/MissingSuper: Exclude: diff --git a/active_storage_db.gemspec b/active_storage_db.gemspec index 7d6fd97..9db559a 100644 --- a/active_storage_db.gemspec +++ b/active_storage_db.gemspec @@ -16,7 +16,7 @@ Gem::Specification.new do |spec| spec.description = "An ActiveStorage service plugin to store files in database." spec.license = "MIT" - spec.required_ruby_version = ">= 2.7.0" + spec.required_ruby_version = ">= 3.0.0" spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = spec.homepage @@ -24,6 +24,6 @@ Gem::Specification.new do |spec| spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"] - spec.add_dependency "activestorage", ">= 6.0" - spec.add_dependency "rails", ">= 6.0" + spec.add_dependency "activestorage", ">= 6.1" + spec.add_dependency "rails", ">= 6.1" end diff --git a/lib/active_storage/service/db_service.rb b/lib/active_storage/service/db_service.rb index 831265a..03c366a 100644 --- a/lib/active_storage/service/db_service.rb +++ b/lib/active_storage/service/db_service.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require "active_storage/service/db_service_rails60" require "active_storage/service/db_service_rails61" require "active_storage/service/db_service_rails70" @@ -11,10 +10,8 @@ class Service::DBService < Service # :nocov: if Rails::VERSION::MAJOR >= 7 include ActiveStorage::DBServiceRails70 - elsif Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR == 1 - include ActiveStorage::DBServiceRails61 else - include ActiveStorage::DBServiceRails60 + include ActiveStorage::DBServiceRails61 end # :nocov: @@ -101,7 +98,7 @@ def url_for_direct_upload(key, expires_in:, content_type:, content_length:, chec purpose: :blob_token ) - url_helpers.update_service_url(verified_token_with_expiration, url_options).tap do |generated_url| + url_helpers.update_service_url(verified_token_with_expiration, url_params).tap do |generated_url| payload[:url] = generated_url end end @@ -150,12 +147,9 @@ def generate_url(key, expires_in:, filename:, content_type:, disposition:) purpose: :blob_key ) - current_uri = URI.parse(current_host) url_helpers.service_url( verified_key_with_expiration, - protocol: current_uri.scheme, - host: current_uri.host, - port: current_uri.port, + **url_params, disposition: content_disposition, content_type: content_type, filename: filename diff --git a/lib/active_storage/service/db_service_rails60.rb b/lib/active_storage/service/db_service_rails60.rb deleted file mode 100644 index 07b7b26..0000000 --- a/lib/active_storage/service/db_service_rails60.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -module ActiveStorage - module DBServiceRails60 - def url(key, expires_in:, filename:, disposition:, content_type:) - instrument :url, key: key do |payload| - generate_url(key, expires_in: expires_in, filename: filename, content_type: content_type, disposition: disposition).tap do |generated_url| - payload[:url] = generated_url - end - end - end - - private - - def current_host - url_options[:host] - end - - def url_options - { - host: ActiveStorage::Current.host - } - end - end -end diff --git a/lib/active_storage/service/db_service_rails61.rb b/lib/active_storage/service/db_service_rails61.rb index 6ca65cc..c2ec5c8 100644 --- a/lib/active_storage/service/db_service_rails61.rb +++ b/lib/active_storage/service/db_service_rails61.rb @@ -4,10 +4,6 @@ module ActiveStorage module DBServiceRails61 private - def current_host - url_options[:host] - end - def private_url(key, expires_in:, filename:, content_type:, disposition:, **) generate_url( key, @@ -28,10 +24,9 @@ def public_url(key, filename:, content_type: nil, disposition: :attachment, **) ) end - def url_options - { - host: ActiveStorage::Current.host - } + def url_params + uri = URI.parse(ActiveStorage::Current.host) + { protocol: uri.scheme, host: uri.host, port: uri.port } end end end diff --git a/lib/active_storage/service/db_service_rails70.rb b/lib/active_storage/service/db_service_rails70.rb index f4943cc..063e76c 100644 --- a/lib/active_storage/service/db_service_rails70.rb +++ b/lib/active_storage/service/db_service_rails70.rb @@ -3,28 +3,25 @@ module ActiveStorage module DBServiceRails70 def compose(source_keys, destination_key, **) - buffer = nil - comment = "DBService#compose" - source_keys.each do |source_key| - record = ::ActiveStorageDB::File.annotate(comment).find_by(ref: source_key) - raise ActiveStorage::FileNotFoundError unless record + instrument :compose, key: destination_key do + buffer = nil + comment = "DBService#compose" + source_keys.each do |source_key| + record = ::ActiveStorageDB::File.annotate(comment).find_by(ref: source_key) + raise ActiveStorage::FileNotFoundError unless record - if buffer - buffer << record.data - else - buffer = +record.data + if buffer + buffer << record.data + else + buffer = +record.data + end end + ::ActiveStorageDB::File.create!(ref: destination_key, data: buffer) if buffer end - ::ActiveStorageDB::File.create!(ref: destination_key, data: buffer) if buffer end private - def current_host - opts = url_options || {} - opts[:port] ? "#{opts[:protocol]}#{opts[:host]}:#{opts[:port]}" : "#{opts[:protocol]}#{opts[:host]}" - end - def private_url(key, expires_in:, filename:, content_type:, disposition:, **) generate_url( key, @@ -45,8 +42,9 @@ def public_url(key, filename:, content_type: nil, disposition: :attachment, **) ) end - def url_options - ActiveStorage::Current.url_options + def url_params + opts = ActiveStorage::Current.url_options || {} + { protocol: opts[:protocol], host: opts[:host], port: opts[:port] } end end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 39c80df..674d9ec 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -35,7 +35,7 @@ require 'factory_bot_rails' support_files = File.expand_path('support/**/*.rb', __dir__) -Dir[support_files].sort.each { |f| require f } +Dir[support_files].each { |f| require f } RSpec.configure do |config| config.filter_rails_from_backtrace!