Skip to content

Commit

Permalink
Give a stable name to classes created by the mount_uploader block
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Jan 21, 2023
1 parent 9013999 commit f5b09b8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/carrierwave/mount.rb
Expand Up @@ -394,7 +394,7 @@ def remove_rolled_back_#{column}
def mount_base(column, uploader=nil, options={}, &block)
include CarrierWave::Mount::Extension

uploader = build_uploader(uploader, &block)
uploader = build_uploader(uploader, column, &block)
uploaders[column.to_sym] = uploader
uploader_options[column.to_sym] = options

Expand Down Expand Up @@ -453,16 +453,15 @@ def mark_remove_#{column}_false
RUBY
end

def build_uploader(uploader, &block)
return uploader if uploader && !block_given?
def build_uploader(uploader, column, &block)
uploader ||= CarrierWave::Uploader::Base
return uploader unless block_given?

uploader = Class.new(uploader || CarrierWave::Uploader::Base)
const_set("Uploader#{uploader.object_id}".tr('-', '_'), uploader)
uploader = Class.new(uploader)
const_set("CarrierWave#{column.to_s.camelize}Uploader", uploader)

if block_given?
uploader.class_eval(&block)
uploader.recursively_apply_block_to_versions(&block)
end
uploader.class_eval(&block)
uploader.recursively_apply_block_to_versions(&block)

uploader
end
Expand Down
4 changes: 4 additions & 0 deletions spec/mount_multiple_spec.rb
Expand Up @@ -971,6 +971,10 @@ def fish
it "applies any custom modifications to the uploader class" do
expect(uploader.new).not_to respond_to(:fish)
end

it "should give the generated class a stable name" do
expect(first_image.class).to eq klass.const_get :CarrierWaveImagesUploader
end
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/mount_single_spec.rb
Expand Up @@ -696,6 +696,10 @@ def fish
it "should not apply any custom modifications to the uploader class" do
expect(@uploader.new).not_to respond_to(:fish)
end

it "should give the generated class a stable name" do
expect(@instance.image.class).to eq @class.const_get :CarrierWaveImageUploader
end
end
end

Expand Down

0 comments on commit f5b09b8

Please sign in to comment.