Repository: rails/rails
File: activestorage/app/models/active_storage/attachment.rb (Ruby)

Code before:
  #
  #   avatar.representation(resize_to_limit: [100, 100]).processed.url
  #
  # or if you are using pre-defined variants:
  #
  #   avatar.representation(:thumb).processed.url
  #
  # See ActiveStorage::Blob::Representable#representation for more information.
  #
  # Raises an +ArgumentError+ if +transformations+ is a +Symbol+ which is an
  # unknown pre-defined variant of the attachment.
  def representation(transformations)
    transformations = transformations_by_name(transformations)
    blob.representation(transformations)
  end

  private
    def analyze_blob_later
      blob.analyze_later unless blob.analyzed?
    end

    def mirror_blob_later
      blob.mirror_later
    end

    def create_variants
      return unless representable?

      immediate_transformations = []
      later_transformations     = []

      named_variants.each do |_name, named_variant|
        case named_variant.process(record)
        when :immediately
          immediate_transformations << named_variant.transformations
        when :later
          later_transformations << named_variant.transformations
        end
      end

      ActiveStorage::CreateVariantsJob.perform_now(blob, transformations: immediate_transformations, process: :immediately) if immediate_transformations.any?
      ActiveStorage::CreateVariantsJob.perform_later(blob, transformations: later_transformations, process: :later) if later_transformations.any?
    end

    def purge_dependent_blob_later
      blob&.purge_later if dependent == :purge_later
    end

    def dependent
      record.attachment_reflections[name]&.options&.fetch(:dependent, nil)
    end

Review comment (suggestion):
Maybe rename this to make clear variants might not get created immediately?
```suggestion
    def schedule_variant_transformations
```

Code after:
  #
  #   avatar.representation(resize_to_limit: [100, 100]).processed.url
  #
  # or if you are using pre-defined variants:
  #
  #   avatar.representation(:thumb).processed.url
  #
  # See ActiveStorage::Blob::Representable#representation for more information.
  #
  # Raises an +ArgumentError+ if +transformations+ is a +Symbol+ which is an
  # unknown pre-defined variant of the attachment.
  def representation(transformations)
    transformations = transformations_by_name(transformations)
    blob.representation(transformations)
  end

  private
    def analyze_blob_later
      blob.analyze_later unless blob.analyzed?
    end

    def mirror_blob_later
      blob.mirror_later
    end

    def create_variants
      return unless representable?

      immediate_variants = []
      later_variants     = []

      named_variants.each do |_name, named_variant|
        case named_variant.process(record)
        when :immediately
          immediate_variants << named_variant.transformations
        when :later
          later_variants << named_variant.transformations
        end
      end

      ActiveStorage::CreateVariantsJob.perform_now(blob, variants: immediate_variants, process: :immediately) if immediate_variants.any?
      ActiveStorage::CreateVariantsJob.perform_later(blob, variants: later_variants, process: :later) if later_variants.any?
    end

    def purge_dependent_blob_later
      blob&.purge_later if dependent == :purge_later
    end

    def dependent
      record.attachment_reflections[name]&.options&.fetch(:dependent, nil)
    end