Module: ActiveShrine::Attachment::AttachmentMethods

Defined in:
lib/active_shrine/attachment.rb

Instance Method Summary collapse

Instance Method Details

#content_typeObject



55
56
57
# File 'lib/active_shrine/attachment.rb', line 55

def content_type
  file.mime_type
end

#extensionObject



63
64
65
# File 'lib/active_shrine/attachment.rb', line 63

def extension
  file.extension
end

#file=(value) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_shrine/attachment.rb', line 77

def file=(value)
  # It is the same file. we are good to go.
  return if value == signed_id

  if value.is_a?(String)
    # it is an already uploaded file. either
    # - via direct upload so the form is sending us a json hash to set
    # - or was set because a previous submission failed, so the form is sending us the signed_id
    begin
      # attempt to parse as a json hash
      value = JSON.parse value
    rescue JSON::ParserError
      # this is not a valid json hash, let's check if it is a valid signed_id
      unsigned = Rails.application.message_verifier(:active_shrine_attachment).verify value
      value = JSON.parse unsigned["file"]
    end
  end

  super
rescue ActiveSupport::MessageVerifier::InvalidSignature
  errors.add(:file, "is invalid")
end

#filenameObject



59
60
61
# File 'lib/active_shrine/attachment.rb', line 59

def filename
  file.original_filename
end

#purgeObject



100
101
102
103
# File 'lib/active_shrine/attachment.rb', line 100

def purge
  file_attacher.destroy_block { destroy } if file_attacher.respond_to?(:destroy_block)
  destroy
end

#purge_laterObject



105
106
107
108
109
110
111
112
# File 'lib/active_shrine/attachment.rb', line 105

def purge_later
  file_attacher.destroy_background
  file_attacher.instance_variable_set :@file, nil # prevent shrine from attempting to destroy the file again
  destroy
rescue NoMethodError
  raise NotImplementedError, ("You need to enable Shrine backgrounding to use purge_later: " \
                              "https://shrinerb.com/docs/plugins/backgrounding")
end

#representable?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/active_shrine/attachment.rb', line 67

def representable?
  %r{image/.*}.match? content_type
end

#signed_idObject



71
72
73
74
75
# File 'lib/active_shrine/attachment.rb', line 71

def signed_id
  # add the id to ensure uniqueness
  value = ({id:, file: file.to_json} if file.present?) || {}
  Rails.application.message_verifier(:active_shrine_attachment).generate value
end

#url(variant = nil, strict: false) ⇒ Object

Returns a URL for this attachment.

Parameters:

  • variant (Symbol, nil) (defaults to: nil)

    derivative name (e.g. :thumb). If the derivative does not exist, falls back to the original.

  • strict (Boolean) (defaults to: false)

    when true, returns nil unless the file has been promoted to permanent storage. Defaults to false, which returns the cache URL during the pending window so uploads are visible immediately.



48
49
50
51
52
53
# File 'lib/active_shrine/attachment.rb', line 48

def url(variant = nil, strict: false)
  attacher = file_attacher
  return nil if strict && !attacher.stored?

  (variant && attacher.url(variant)) || attacher.url
end