Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an option to preprocessed AS variants
ActiveStorage variants are processed on the fly when they are needed but sometimes we're sure that they are accessed and want to processed them upfront. `preprocessed` option is added when declaring variants. ``` class User < ApplicationRecord has_one_attached :avatar do |attachable| attachable.variant :thumb, resize_to_limit: [100, 100], preprocessed: true end end ```
- Loading branch information
Showing
11 changed files
with
188 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActiveStorage::TransformJob < ActiveStorage::BaseJob | ||
queue_as { ActiveStorage.queues[:transform] } | ||
|
||
discard_on ActiveRecord::RecordNotFound | ||
retry_on ActiveStorage::IntegrityError, attempts: 10, wait: :exponentially_longer | ||
|
||
def perform(blob, transformations) | ||
blob.variant(transformations).process | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActiveStorage::NamedVariant # :nodoc: | ||
attr_reader :transformations, :preprocessed | ||
|
||
def initialize(transformations) | ||
@preprocessed = transformations[:preprocessed] | ||
@transformations = transformations.except(:preprocessed) | ||
end | ||
|
||
def preprocessed?(record) | ||
case preprocessed | ||
when Symbol | ||
record.send(preprocessed) | ||
when Proc | ||
preprocessed.call(record) | ||
else | ||
preprocessed | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
require "database/setup" | ||
|
||
class ActiveStorage::TransformJobTest < ActiveJob::TestCase | ||
setup { @blob = create_file_blob } | ||
|
||
test "creates variant" do | ||
transformations = { resize_to_limit: [100, 100] } | ||
|
||
assert_changes -> { @blob.variant(transformations).processed? }, from: false, to: true do | ||
perform_enqueued_jobs do | ||
ActiveStorage::TransformJob.perform_later @blob, transformations | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters