Skip to content
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

Make an ActiveStorage adapter #431

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ directory.

Standard adapter, writes out to a file.

##### `SitemapGenerator::ActiveStorageAdapter`

Uses `ActiveStorage::Blob` to store the sitemap.

##### `SitemapGenerator::FogAdapter`

Uses `Fog::Storage` to upload to any service supported by Fog.
Expand Down
1 change: 1 addition & 0 deletions lib/sitemap_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
module SitemapGenerator
autoload(:Interpreter, 'sitemap_generator/interpreter')
autoload(:FileAdapter, 'sitemap_generator/adapters/file_adapter')
autoload(:ActiveStorageAdapter, 'sitemap_generator/adapters/active_storage_adapter') if defined?(::ActiveStorage)
autoload(:S3Adapter, 'sitemap_generator/adapters/s3_adapter')
autoload(:AwsSdkAdapter, 'sitemap_generator/adapters/aws_sdk_adapter')
autoload(:WaveAdapter, 'sitemap_generator/adapters/wave_adapter')
Expand Down
26 changes: 26 additions & 0 deletions lib/sitemap_generator/adapters/active_storage_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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
end

def write(location, raw_data)
SitemapGenerator::FileAdapter.new.write(location, raw_data)

ActiveStorage::Blob.transaction do
ActiveStorage::Blob.where(key: key).destroy_all

ActiveStorage::Blob.create_and_upload!(
key: key,
io: open(location.path, 'rb'),
filename: filename,
content_type: 'application/gzip',
identify: false
)
end
end
end
end
39 changes: 39 additions & 0 deletions spec/sitemap_generator/adapters/active_storage_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'
require 'sitemap_generator/adapters/active_storage_adapter'

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

def self.where(*args)
FakeScope.new
end

def self.create_and_upload!(**kwargs)
'ActiveStorage::Blob'
end

class FakeScope
def destroy_all
true
end
end
end
}

before do
stub_const('ActiveStorage::Blob', fake_active_storage_blob)
end

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'
end
end
end