Class: Alchemy::StorageAdapter::ActiveStorage::AttachmentUrl

Inherits:
Object
  • Object
show all
Defined in:
app/models/alchemy/storage_adapter/active_storage/attachment_url.rb

Overview

The class representing an URL to an ActiveStorage attachment

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attachment) ⇒ AttachmentUrl

Returns a new instance of AttachmentUrl.



10
11
12
# File 'app/models/alchemy/storage_adapter/active_storage/attachment_url.rb', line 10

def initialize(attachment)
  @attachment = attachment
end

Instance Attribute Details

#attachmentObject (readonly)

Returns the value of attribute attachment.



8
9
10
# File 'app/models/alchemy/storage_adapter/active_storage/attachment_url.rb', line 8

def attachment
  @attachment
end

Instance Method Details

#call(options = {}) ⇒ String

The attachment url

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :download (Symbol)

    return a URL for downloading the attachment

  • :name (Symbol)

    The filename

  • :format (Symbol)

    The file extension

Returns:

  • (String)


23
24
25
26
27
28
29
30
31
32
# File 'app/models/alchemy/storage_adapter/active_storage/attachment_url.rb', line 23

def call(options = {})
  if attachment.file.attached?
    options[:format] ||= attachment.suffix
    if options.delete(:download)
      routes.download_attachment_path(attachment, options)
    else
      routes.show_attachment_path(attachment, options)
    end
  end
end