Skip to content

Commit

Permalink
Keep VariantWithRecord API consistent with Variant
Browse files Browse the repository at this point in the history
The `process` and `processed?` methods in Variant are private.
  • Loading branch information
rafaelfranca committed Aug 23, 2023
1 parent 144dd24 commit c18bcd5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
18 changes: 9 additions & 9 deletions activestorage/app/models/active_storage/variant_with_record.rb
Expand Up @@ -13,18 +13,10 @@ def initialize(blob, variation)
end

def processed
process
process unless processed?
self
end

def process
transform_blob { |image| create_or_find_record(image: image) } unless processed?
end

def processed?
record.present?
end

def image
record&.image
end
Expand All @@ -37,6 +29,14 @@ def destroy
delegate :key, :url, :download, to: :image, allow_nil: true

private
def processed?
record.present?
end

def process
transform_blob { |image| create_or_find_record(image: image) }
end

def transform_blob
blob.open do |input|
variation.transform(input) do |output|
Expand Down
2 changes: 1 addition & 1 deletion activestorage/test/jobs/transform_job_test.rb
Expand Up @@ -9,7 +9,7 @@ class ActiveStorage::TransformJobTest < ActiveJob::TestCase
test "creates variant" do
transformations = { resize_to_limit: [100, 100] }

assert_changes -> { @blob.variant(transformations).processed? }, from: false, to: true do
assert_changes -> { @blob.variant(transformations).send(:processed?) }, from: false, to: true do
perform_enqueued_jobs do
ActiveStorage::TransformJob.perform_later @blob, transformations
end
Expand Down
16 changes: 8 additions & 8 deletions activestorage/test/models/variant_with_record_test.rb
Expand Up @@ -19,7 +19,7 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase
variant = blob.variant(resize_to_limit: [100, 100])

assert_difference -> { blob.variant_records.count }, +1 do
variant.process
variant.processed
end

assert_match(/racecar\.jpg/, variant.url)
Expand All @@ -36,13 +36,13 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase
blob = create_file_blob(filename: "racecar.jpg")

assert_difference -> { blob.variant_records.count } do
blob.variant(resize_to_limit: [100, 100]).process
blob.variant(resize_to_limit: [100, 100]).processed
end

variant = blob.variant(resize_to_limit: [100, 100])

assert_no_difference -> { blob.variant_records.count } do
variant.process
variant.processed
end

assert_match(/racecar\.jpg/, variant.url)
Expand All @@ -54,7 +54,7 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase

test "variant of a blob is on the same service" do
blob = create_file_blob(filename: "racecar.jpg", service_name: "local_public")
variant = blob.variant(resize_to_limit: [100, 100]).process
variant = blob.variant(resize_to_limit: [100, 100]).processed

assert_equal "local_public", variant.image.blob.service_name
end
Expand All @@ -65,12 +65,12 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase

blob1 = directly_upload_file_blob(filename: "racecar.jpg")
assert_difference -> { ActiveStorage::VariantRecord.count }, +1 do
blob1.representation(resize_to_limit: [100, 100]).process
blob1.representation(resize_to_limit: [100, 100]).processed
end

blob2 = directly_upload_file_blob(filename: "racecar_rotated.jpg")
assert_difference -> { ActiveStorage::VariantRecord.count }, +1 do
blob2.representation(resize_to_limit: [100, 100]).process
blob2.representation(resize_to_limit: [100, 100]).processed
end

assert_no_difference -> { ActiveStorage::VariantRecord.count } do
Expand Down Expand Up @@ -126,12 +126,12 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase

blob1 = directly_upload_file_blob(filename: "racecar.jpg")
assert_difference -> { ActiveStorage::VariantRecord.count }, +1 do
blob1.representation(resize_to_limit: [100, 100]).process
blob1.representation(resize_to_limit: [100, 100]).processed
end

blob2 = directly_upload_file_blob(filename: "racecar_rotated.jpg")
assert_difference -> { ActiveStorage::VariantRecord.count }, +1 do
blob2.representation(resize_to_limit: [100, 100]).process
blob2.representation(resize_to_limit: [100, 100]).processed
end

assert_no_difference -> { ActiveStorage::VariantRecord.count } do
Expand Down

0 comments on commit c18bcd5

Please sign in to comment.