Skip to content

Commit

Permalink
Give a stable name to version classes
Browse files Browse the repository at this point in the history
Fixes #2407, Fixes #2471
  • Loading branch information
mshibuya committed Jan 15, 2023
1 parent 8dace34 commit a9de756
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/carrierwave/uploader/versions.rb
Expand Up @@ -74,9 +74,11 @@ def recursively_apply_block_to_versions(&block)
private

def build_version(name, options)
class_name = "#{name.to_s.camelize}VersionUploader"

if !versions.has_key?(name)
uploader = Class.new(self)
const_set("Uploader#{uploader.object_id}".tr('-', '_'), uploader)
const_set(class_name, uploader)
uploader.version_names += [name]
uploader.versions = {}
uploader.processors = []
Expand Down Expand Up @@ -118,8 +120,9 @@ def #{name}
end
RUBY
else
uploader = Class.new(versions[name])
const_set("Uploader#{uploader.object_id}".tr('-', '_'), uploader)
parent = versions[name]
uploader = Class.new(parent)
parent.const_set(class_name, uploader)
uploader.processors = []
uploader.version_options = uploader.version_options.merge(options)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/uploader/versions_spec.rb
Expand Up @@ -8,6 +8,9 @@
end

after do
@uploader_class.constants
.select { |const| const.to_s =~ /Uploader$/ }
.each { |const| @uploader_class.send(:remove_const, const)}
FileUtils.rm_rf(public_path)
end

Expand Down Expand Up @@ -56,6 +59,11 @@
expect(@uploader.thumb.class.version_names).to eq([:thumb])
end

it "should set the class name" do
@uploader_class.version :thumb
expect(@uploader.thumb.class).to eq @uploader_class.const_get :ThumbVersionUploader
end

it "should remember mount options" do
model = double('a model')
@uploader_class.version :thumb
Expand Down

0 comments on commit a9de756

Please sign in to comment.