Class: SitemapGenerator::ActiveStorageAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/sitemap_generator/adapters/active_storage_adapter.rb

Overview

Class for uploading sitemaps to ActiveStorage.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key: :sitemap, filename: 'sitemap.xml.gz') ⇒ ActiveStorageAdapter

Returns a new instance of ActiveStorageAdapter.



8
9
10
11
# File 'lib/sitemap_generator/adapters/active_storage_adapter.rb', line 8

def initialize(key: :sitemap, filename: 'sitemap.xml.gz')
  @key = key
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



6
7
8
# File 'lib/sitemap_generator/adapters/active_storage_adapter.rb', line 6

def filename
  @filename
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/sitemap_generator/adapters/active_storage_adapter.rb', line 6

def key
  @key
end

Instance Method Details

#write(location, raw_data) ⇒ Object

rubocop:disable Metrics/MethodLength



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sitemap_generator/adapters/active_storage_adapter.rb', line 13

def write(location, raw_data) # rubocop:disable Metrics/MethodLength
  SitemapGenerator::FileAdapter.new.write(location, raw_data)

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

    File.open(location.path, 'rb') do |io|
      ActiveStorage::Blob.create_and_upload!(
        key: key,
        io: io,
        filename: filename,
        content_type: 'application/gzip',
        identify: false
      )
    end
  end
end