-
Notifications
You must be signed in to change notification settings - Fork 280
ActiveStorageAdapter can upload multiple files #480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| raise LoadError, <<~MSG unless defined?(ActiveStorage) | ||
| Error: 'ActiveStorage' is not defined. | ||
|
|
||
| Please `require 'active_storage'` - or another library that defines this class. | ||
| MSG | ||
|
|
||
| module SitemapGenerator | ||
| # Class for uploading sitemaps to ActiveStorage. | ||
| class ActiveStorageAdapter | ||
| attr_reader :key, :filename | ||
|
|
||
| def initialize key: :sitemap, filename: 'sitemap.xml.gz' | ||
| @key, @filename = key, filename | ||
| def initialize(prefix: nil) | ||
| @prefix = Pathname.new prefix.to_s | ||
| end | ||
|
|
||
| def write(location, raw_data) | ||
| SitemapGenerator::FileAdapter.new.write(location, raw_data) | ||
| FileAdapter.new.write(location, raw_data) | ||
|
|
||
| ActiveStorage::Blob.transaction do | ||
| ActiveStorage::Blob.where(key: key).destroy_all | ||
| ActiveStorage::Blob.destroy_by(key: key(location)) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
However, because |
||
|
|
||
| ActiveStorage::Blob.create_and_upload!( | ||
| key: key, | ||
| io: open(location.path, 'rb'), | ||
| filename: filename, | ||
| content_type: 'application/gzip', | ||
| key: key(location), | ||
| io: File.open(location.path), | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switched to However, a future refactor could/should make |
||
| filename: location.filename, | ||
| content_type: content_type(location), | ||
| identify: false | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def key(location) | ||
| (@prefix / location.path_in_public).to_s | ||
| end | ||
|
|
||
| def content_type(location) | ||
| # Using .gz matching to be consistent with FileAdapter | ||
| # but this logic is brittle and needs refactored | ||
| "application/#{location.path.match?(/.gz$/) ? 'gzip' : 'xml'}" | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,71 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
| require 'sitemap_generator/adapters/active_storage_adapter' | ||
|
|
||
| RSpec.describe 'SitemapGenerator::ActiveStorageAdapter' do | ||
| let(:location) { SitemapGenerator::SitemapLocation.new } | ||
| let(:adapter) { SitemapGenerator::ActiveStorageAdapter.new } | ||
| let(:fake_active_storage_blob) { | ||
| Class.new do | ||
| def self.transaction | ||
| yield | ||
| end | ||
| subject(:adapter) { SitemapGenerator::ActiveStorageAdapter.new } | ||
|
|
||
| def self.where(*args) | ||
| FakeScope.new | ||
| end | ||
| let!(:active_storage) do | ||
| class_double('ActiveStorage::Blob', destroy_by: true, create_and_upload!: nil) | ||
| .tap { |blob| allow(blob).to receive(:transaction).and_yield } | ||
| .as_stubbed_const | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. class_double and as_stubbed_const are purpose built for creating safe double of classes. The Additionally, the |
||
| end | ||
|
|
||
| describe 'write' do | ||
| let(:location) do | ||
| SitemapGenerator::SitemapLocation.new( | ||
| filename: 'custom.xml', | ||
| sitemaps_path: 'some_path' | ||
| ) | ||
| end | ||
|
|
||
| it 'creates an ActiveStorage::Blob record' do | ||
| adapter.write(location, 'data') | ||
|
|
||
| expect(active_storage).to have_received(:create_and_upload!) | ||
| end | ||
|
|
||
| it 'gets key and filename from the sitemap_location' do | ||
| adapter.write(location, 'data') | ||
|
|
||
| def self.create_and_upload!(**kwargs) | ||
| 'ActiveStorage::Blob' | ||
| expect(active_storage).to have_received(:create_and_upload!) | ||
| .with(include(key: 'some_path/custom.xml', filename: 'custom.xml')) | ||
| end | ||
|
|
||
| # Ideally, this would be driven by the location or namer collaborators, | ||
| # but it's all rather murky at the moment. filename extension is what | ||
| # drives compression in FileAdapter, so consistency wins | ||
| context 'with a gzipped file' do | ||
| let(:location) { SitemapGenerator::SitemapLocation.new(filename: 'custom.xml.gz') } | ||
|
|
||
| specify do | ||
| adapter.write(location, 'data') | ||
|
|
||
| expect(active_storage).to have_received(:create_and_upload!) | ||
| .with(include(content_type: 'application/gzip')) | ||
| end | ||
| end | ||
|
|
||
| context 'with a non-gzipped file' do | ||
| let(:location) { SitemapGenerator::SitemapLocation.new(filename: 'custom.xml') } | ||
|
|
||
| class FakeScope | ||
| def destroy_all | ||
| true | ||
| end | ||
| specify do | ||
| adapter.write(location, 'data') | ||
|
|
||
| expect(active_storage).to have_received(:create_and_upload!) | ||
| .with(include(content_type: 'application/xml')) | ||
| end | ||
| end | ||
| } | ||
|
|
||
| before do | ||
| stub_const('ActiveStorage::Blob', fake_active_storage_blob) | ||
| end | ||
| context 'with a custom prefix for segmenting from other blobs' do | ||
| subject(:adapter) { SitemapGenerator::ActiveStorageAdapter.new(prefix: 'sitemaps') } | ||
|
|
||
| describe 'write' do | ||
| it 'should create an ActiveStorage::Blob record' do | ||
| expect(location).to receive(:filename).and_return('sitemap.xml.gz').at_least(2).times | ||
| expect(adapter.write(location, 'data')).to eq 'ActiveStorage::Blob' | ||
| it 'prefixes only the key' do | ||
| adapter.write(location, 'data') | ||
|
|
||
| expect(active_storage).to have_received(:create_and_upload!) | ||
| .with(include(key: 'sitemaps/some_path/custom.xml', filename: 'custom.xml')) | ||
| end | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not exactly sure why all these adapters write to file first, but they all do, so this is preserved. (Though it seems awkward? If I'm using active-storage adapter, for instance, I wouldn't expect files to also be written to disk.)