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

Conditional versions created with ':from_version' are always processed #2516

Closed
iBublik opened this issue Nov 9, 2020 · 0 comments
Closed

Comments

@iBublik
Copy link

iBublik commented Nov 9, 2020

Assuming we have the following setup

class AvatarUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  storage :file

  version :small, from_version: :cropped, if: :process_small? do
    process resize_to_fit: [150, 150]
  end

  version :cropped do
    process resize_to_fit: [200, 200]
  end

  private

  def process_small?
    !model.skip_processing_small_avatar
  end
end

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader

  attr_writer :skip_processing_small_avatar
end

When we try to disable processing of version, it doesn't work:

user = User.new
user.skip_processing_small_avatar = true
user.avatar = File.open(my_file)
user.avatar.small.exists? # true

If we define the nested version, everything works as expected:

class AvatarUploader < CarrierWave::Uploader::Base
  # other code is the same

  version :cropped do
    process resize_to_fit: [200, 200]
   
    version :small, if: :process_small? do
      process resize_to_fit: [150, 150]
    end
  end
end

user = User.new
user.skip_processing_small_avatar = true
user.avatar = File.open(my_file)
user.avatar.cropped.small.exists? # false

Related code

Callback:

after :cache, :cache_versions!

def versions_to_cache
@versions_to_cache || dependent_versions
end

Sibling versions are always included here, whether they're active or not:
end.to_a + sibling_versions.select do |name, v|
v.class.version_options[:from_version] == self.class.version_names.last
end.to_a

I guess sibling versions should be also filtered this way:

sibling_versions.select do |version, _uploader|
  version.class.version_options[:from_version] == self.class.version_names.last && 
    parent_version.version_exists?(version)
end.to_a

I can create PR if you also think that it's a bug and not expected behavior.

@mshibuya mshibuya added this to the Release v3.0.0 milestone Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants