class ActiveStorage::VariantWithRecord
ActiveStorage::VariantRecord. This is only used if ActiveStorage.track_variants
is enabled.
Like an ActiveStorage::Variant, but keeps detail about the variant in the database as an
def create_or_find_record(image:)
def create_or_find_record(image:) @record = ActiveRecord::Base.connected_to(role: ActiveRecord.writing_role) do blob.variant_records.create_or_find_by!(variation_digest: variation.digest) do |record| record.image.attach(image) end end end
def image
def image record&.image end
def initialize(blob, variation)
def initialize(blob, variation) @blob, @variation = blob, ActiveStorage::Variation.wrap(variation) end
def process
def process transform_blob { |image| create_or_find_record(image: image) } unless processed? end
def processed
def processed process self end
def processed?
def processed? record.present? end
def record
def record @record ||= if blob.variant_records.loaded? blob.variant_records.find { |v| v.variation_digest == variation.digest } else blob.variant_records.find_by(variation_digest: variation.digest) end end
def transform_blob
def transform_blob blob.open do |input| variation.transform(input) do |output| yield io: output, filename: "#{blob.filename.base}.#{variation.format.downcase}", content_type: variation.content_type, service_name: blob.service.name end end end