Module: ActiveShrine::Attachment::AttachmentMethods
- Defined in:
- lib/active_shrine/attachment.rb
Instance Method Summary collapse
- #content_type ⇒ Object
- #extension ⇒ Object
- #file=(value) ⇒ Object
- #filename ⇒ Object
- #purge ⇒ Object
- #purge_later ⇒ Object
- #representable? ⇒ Boolean
- #signed_id ⇒ Object
-
#url(variant = nil, strict: false) ⇒ Object
Returns a URL for this attachment.
Instance Method Details
#content_type ⇒ Object
55 56 57 |
# File 'lib/active_shrine/attachment.rb', line 55 def content_type file.mime_type end |
#extension ⇒ Object
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.(:active_shrine_attachment).verify value value = JSON.parse unsigned["file"] end end super rescue ActiveSupport::MessageVerifier::InvalidSignature errors.add(:file, "is invalid") end |
#filename ⇒ Object
59 60 61 |
# File 'lib/active_shrine/attachment.rb', line 59 def filename file.original_filename end |
#purge ⇒ Object
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_later ⇒ Object
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
67 68 69 |
# File 'lib/active_shrine/attachment.rb', line 67 def representable? %r{image/.*}.match? content_type end |
#signed_id ⇒ Object
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.(:active_shrine_attachment).generate value end |
#url(variant = nil, strict: false) ⇒ Object
Returns a URL for this attachment.
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 |