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.



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

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

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/sitemap_generator/adapters/active_storage_adapter.rb', line 4

def filename
  @filename
end

#keyObject (readonly)

Returns the value of attribute key.



4
5
6
# File 'lib/sitemap_generator/adapters/active_storage_adapter.rb', line 4

def key
  @key
end

Instance Method Details

#write(location, raw_data) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sitemap_generator/adapters/active_storage_adapter.rb', line 10

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