Class: ActiveStorage::VariantWithRecord
- Includes:
- Blob::Servable
- Defined in:
- activestorage/app/models/active_storage/variant_with_record.rb
Overview
Active Storage Variant With Record
Like an ActiveStorage::Variant, but keeps detail about the variant in the database as an ActiveStorage::VariantRecord. This is used if ActiveStorage.track_variants is enabled.
Instance Attribute Summary collapse
-
#blob ⇒ Object
readonly
Returns the value of attribute blob.
-
#variation ⇒ Object
readonly
Returns the value of attribute variation.
Instance Method Summary collapse
-
#destroy ⇒ Object
Destroys record and deletes file from service.
- #filename ⇒ Object
- #image ⇒ Object
-
#initialize(blob, variation) ⇒ VariantWithRecord
constructor
A new instance of VariantWithRecord.
-
#process_from_io(io) ⇒ Object
Process the variant from a local io, avoiding a download from the service.
- #processed ⇒ Object
-
#processed? ⇒ Boolean
Returns true if the variant has already been processed and stored.
Methods included from Blob::Servable
#content_type_for_serving, #forced_disposition_for_serving
Constructor Details
#initialize(blob, variation) ⇒ VariantWithRecord
Returns a new instance of VariantWithRecord.
14 15 16 |
# File 'activestorage/app/models/active_storage/variant_with_record.rb', line 14 def initialize(blob, variation) @blob, @variation = blob, ActiveStorage::Variation.wrap(variation) end |
Instance Attribute Details
#blob ⇒ Object (readonly)
Returns the value of attribute blob.
10 11 12 |
# File 'activestorage/app/models/active_storage/variant_with_record.rb', line 10 def blob @blob end |
#variation ⇒ Object (readonly)
Returns the value of attribute variation.
10 11 12 |
# File 'activestorage/app/models/active_storage/variant_with_record.rb', line 10 def variation @variation end |
Instance Method Details
#destroy ⇒ Object
Destroys record and deletes file from service.
32 33 34 |
# File 'activestorage/app/models/active_storage/variant_with_record.rb', line 32 def destroy record&.destroy end |
#filename ⇒ Object
27 28 29 |
# File 'activestorage/app/models/active_storage/variant_with_record.rb', line 27 def filename ActiveStorage::Filename.new "#{blob.filename.base}.#{variation.format.downcase}" end |
#image ⇒ Object
23 24 25 |
# File 'activestorage/app/models/active_storage/variant_with_record.rb', line 23 def image record&.image end |
#process_from_io(io) ⇒ Object
Process the variant from a local io, avoiding a download from the service. This is an optimization for when the original file is still available locally (e.g., during the initial upload flow).
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'activestorage/app/models/active_storage/variant_with_record.rb', line 46 def process_from_io(io) # :nodoc: return if processed? variation.transform(io) do |output| create_or_find_record(image: { io: output, filename: "#{blob.filename.base}.#{variation.format.downcase}", content_type: variation.content_type, service_name: blob.service.name }) end end |
#processed ⇒ Object
18 19 20 21 |
# File 'activestorage/app/models/active_storage/variant_with_record.rb', line 18 def processed process unless processed? self end |
#processed? ⇒ Boolean
Returns true if the variant has already been processed and stored.
39 40 41 |
# File 'activestorage/app/models/active_storage/variant_with_record.rb', line 39 def processed? record.present? end |