Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NoMethodError in ActiveStorage::TransformJob for untracked variants. #48999

Conversation

bryantraywick
Copy link
Contributor

Motivation / Background

This Pull Request has been created because a NoMethodError: private method `process' called for #<ActiveStorage::Variant error is raised in ActiveStorage::TransformJob when variant tracking is disabled. This prevents the ability to use preprocessed variants when variant tracking is disabled.

Detail

This Pull Request changes the call to process in ActiveStorage::TransformJob to the processed method which is publicly available in both the ActiveStorage::VariantWithRecord and ActiveStorage::Variant classes.

Additional information

The following code snipped contains a working recreation of the issue with the current main branch.

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails", branch: "main"
  gem "rack", "~> 2.0"
  gem "sqlite3"
  gem 'image_processing', '~> 1.2'
end

require "active_record/railtie"
require "active_storage/engine"
require "tmpdir"

class TestApp < Rails::Application
  config.root = __dir__
  config.hosts << "example.org"
  config.eager_load = false
  config.session_store :cookie_store, key: "cookie_store_key"
  config.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  Rails.logger  = config.logger

  config.active_storage.service = :local
  config.active_storage.service_configurations = {
    local: {
      root: Dir.tmpdir,
      service: "Disk"
    }
  }

  config.active_storage.track_variants = false
end

ENV["DATABASE_URL"] = "sqlite3::memory:"

Rails.application.initialize!

require ActiveStorage::Engine.root.join("db/migrate/20170806125915_create_active_storage_tables.rb").to_s

ActiveRecord::Schema.define do
  CreateActiveStorageTables.new.change

  create_table :users, force: true
end

class User < ActiveRecord::Base
  has_one_attached :profile do |attachable|
    attachable.variant :test, resize_to_limit: [100, 100], preprocessed: true
  end
end

require "minitest/autorun"

class BugTest < Minitest::Test
  def test_upload_and_download
    user = User.create!(
      profile: {
        content_type: "image/jpeg",
        filename: "racecar.jpg",
        io: Pathname.new("activestorage/test/fixtures/files/racecar.jpg").open,
      }
    )

    assert_equal false, user.profile.variant(:test).send(:processed?)

    user.profile.attachment.send(:named_variants).each do |_name, named_variant|
      assert_raises(NoMethodError) do
        ActiveStorage::TransformJob.perform_now(user.profile, named_variant.transformations)
      end
    end
  end
end

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rafaelfranca rafaelfranca merged commit 144dd24 into rails:main Aug 23, 2023
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants