Skip to content

Commit

Permalink
Introduce ActiveSupport Deprecator
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Jun 1, 2024
1 parent 65e5e8e commit b25477d
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 22 deletions.
9 changes: 9 additions & 0 deletions lib/carrierwave.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'active_support/core_ext/object/try'
require 'active_support/core_ext/class/attribute'
require 'active_support/concern'
require 'active_support/deprecation'

module CarrierWave

Expand All @@ -21,6 +22,10 @@ def clean_cached_files!(seconds=60*60*24)
def tmp_path
@tmp_path ||= File.expand_path(File.join('..', 'tmp'), root)
end

def deprecator
@deprecator ||= ActiveSupport::Deprecation.new("#{CarrierWave::VERSION.split('.')[0].to_i + 1}.0", "CarrierWave")
end
end

end
Expand Down Expand Up @@ -64,6 +69,10 @@ class Railtie < Rails::Railtie
end
end

initializer "carrierwave.deprecator" do |app|
app.deprecators[:carrierwave] = CarrierWave.deprecator

This comment has been minimized.

Copy link
@zzak

zzak Jul 2, 2024

app.deprecators is only available in Rails 7.1+: rails/rails#46049

Was this intentional?

This comment has been minimized.

Copy link
@mshibuya

mshibuya Jul 3, 2024

Author Member

Ah I didn't realize it, and I just thought it's working as CI didn't break...
Let me check later 👍

This comment has been minimized.

Copy link
@zzak

zzak Jul 3, 2024

I only knew because I tried master on a Rails 7.0 app, thanks for your hard work!

This comment has been minimized.

Copy link
@mshibuya

mshibuya Jul 10, 2024

Author Member

Now it should be fixed by 8761240 🙇‍♂️

end

config.before_eager_load do
CarrierWave::Storage::Fog.eager_load
end
Expand Down
2 changes: 1 addition & 1 deletion lib/carrierwave/processing/rmagick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def create_info_block(options)
proc do |img|
options.each do |k, v|
if v.is_a?(String) && (matches = v.match(/^["'](.+)["']/))
ActiveSupport::Deprecation.warn "Passing quoted strings like #{v} to #manipulate! is deprecated, pass them without quoting."
CarrierWave.deprecator.warn "Passing quoted strings like #{v} to #manipulate! is deprecated, pass them without quoting."
v = matches[1]
end
img.public_send(:"#{k}=", v)
Expand Down
2 changes: 1 addition & 1 deletion lib/carrierwave/uploader/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ def cache_stored_file!
end

def sanitized_file
ActiveSupport::Deprecation.warn('#sanitized_file is deprecated, use #file instead.')
file
end
CarrierWave.deprecator.deprecate_methods(self, sanitized_file: :file)

##
# Returns a String which uniquely identifies the currently cached file for later retrieval
Expand Down
8 changes: 4 additions & 4 deletions lib/carrierwave/uploader/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,19 @@ def #{name}
def add_deprecated_config(name)
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.#{name}(value=nil)
ActiveSupport::Deprecation.warn "##{name} is deprecated and has no effect"
CarrierWave.deprecator.warn "##{name} is deprecated and has no effect"
end
def self.#{name}=(value)
ActiveSupport::Deprecation.warn "##{name} is deprecated and has no effect"
CarrierWave.deprecator.warn "##{name} is deprecated and has no effect"
end
def #{name}=(value)
ActiveSupport::Deprecation.warn "##{name} is deprecated and has no effect"
CarrierWave.deprecator.warn "##{name} is deprecated and has no effect"
end
def #{name}
ActiveSupport::Deprecation.warn "##{name} is deprecated and has no effect"
CarrierWave.deprecator.warn "##{name} is deprecated and has no effect"
end
RUBY
end
Expand Down
2 changes: 1 addition & 1 deletion lib/carrierwave/uploader/content_type_allowlist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def content_type_allowlist
def check_content_type_allowlist!(new_file)
allowlist = content_type_allowlist
if !allowlist && respond_to?(:content_type_whitelist) && content_type_whitelist
ActiveSupport::Deprecation.warn "#content_type_whitelist is deprecated, use #content_type_allowlist instead." unless instance_variable_defined?(:@content_type_whitelist_warned)
CarrierWave.deprecator.warn "#content_type_whitelist is deprecated, use #content_type_allowlist instead." unless instance_variable_defined?(:@content_type_whitelist_warned)
@content_type_whitelist_warned = true
allowlist = content_type_whitelist
end
Expand Down
4 changes: 2 additions & 2 deletions lib/carrierwave/uploader/content_type_denylist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ def content_type_denylist
def check_content_type_denylist!(new_file)
denylist = content_type_denylist
if !denylist && respond_to?(:content_type_blacklist) && content_type_blacklist
ActiveSupport::Deprecation.warn "#content_type_blacklist is deprecated, use #content_type_denylist instead." unless instance_variable_defined?(:@content_type_blacklist_warned)
CarrierWave.deprecator.warn "#content_type_blacklist is deprecated, use #content_type_denylist instead." unless instance_variable_defined?(:@content_type_blacklist_warned)
@content_type_blacklist_warned = true
denylist = content_type_blacklist
end

return unless denylist

ActiveSupport::Deprecation.warn "Use of #content_type_denylist is deprecated for the security reason, use #content_type_allowlist instead to explicitly state what are safe to accept" unless instance_variable_defined?(:@content_type_denylist_warned)
CarrierWave.deprecator.warn "Use of #content_type_denylist is deprecated for the security reason, use #content_type_allowlist instead to explicitly state what are safe to accept" unless instance_variable_defined?(:@content_type_denylist_warned)
@content_type_denylist_warned = true

content_type = new_file.content_type
Expand Down
2 changes: 1 addition & 1 deletion lib/carrierwave/uploader/extension_allowlist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def extension_allowlist
def check_extension_allowlist!(new_file)
allowlist = extension_allowlist
if !allowlist && respond_to?(:extension_whitelist) && extension_whitelist
ActiveSupport::Deprecation.warn "#extension_whitelist is deprecated, use #extension_allowlist instead." unless instance_variable_defined?(:@extension_whitelist_warned)
CarrierWave.deprecator.warn "#extension_whitelist is deprecated, use #extension_allowlist instead." unless instance_variable_defined?(:@extension_whitelist_warned)
@extension_whitelist_warned = true
allowlist = extension_whitelist
end
Expand Down
4 changes: 2 additions & 2 deletions lib/carrierwave/uploader/extension_denylist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ def extension_denylist
def check_extension_denylist!(new_file)
denylist = extension_denylist
if !denylist && respond_to?(:extension_blacklist) && extension_blacklist
ActiveSupport::Deprecation.warn "#extension_blacklist is deprecated, use #extension_denylist instead." unless instance_variable_defined?(:@extension_blacklist_warned)
CarrierWave.deprecator.warn "#extension_blacklist is deprecated, use #extension_denylist instead." unless instance_variable_defined?(:@extension_blacklist_warned)
@extension_blacklist_warned = true
denylist = extension_blacklist
end

return unless denylist

ActiveSupport::Deprecation.warn "Use of #extension_denylist is deprecated for the security reason, use #extension_allowlist instead to explicitly state what are safe to accept" unless instance_variable_defined?(:@extension_denylist_warned)
CarrierWave.deprecator.warn "Use of #extension_denylist is deprecated for the security reason, use #extension_allowlist instead to explicitly state what are safe to accept" unless instance_variable_defined?(:@extension_denylist_warned)
@extension_denylist_warned = true

extension = new_file.extension.to_s
Expand Down
4 changes: 2 additions & 2 deletions spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def extension_allowlist

context 'when validating denylist integrity' do
before do
allow(ActiveSupport::Deprecation).to receive(:warn).with(/#extension_denylist/)
allow(CarrierWave.deprecator).to receive(:warn).with(/#extension_denylist/)
@uploader.class_eval do
def extension_denylist
%w(jpg)
Expand Down Expand Up @@ -1327,7 +1327,7 @@ def extension_allowlist

context 'when validating denylist integrity' do
before do
allow(ActiveSupport::Deprecation).to receive(:warn).with(/#extension_denylist/)
allow(CarrierWave.deprecator).to receive(:warn).with(/#extension_denylist/)
@uploader.class_eval do
def extension_denylist
%w(jpg)
Expand Down
4 changes: 2 additions & 2 deletions spec/processing/rmagick_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@

it 'shows deprecation but still accepts strings enclosed with double quotes' do
expect_any_instance_of(::Magick::Image::Info).to receive(:size=).once.with("200x200")
expect(ActiveSupport::Deprecation).to receive(:warn).with(any_args)
expect(CarrierWave.deprecator).to receive(:warn).with(any_args)
instance.manipulate! :read => {:size => %{"200x200"}}
end

it 'shows deprecation but still accepts strings enclosed with single quotes' do
expect_any_instance_of(::Magick::Image::Info).to receive(:size=).once.with("200x200")
expect(ActiveSupport::Deprecation).to receive(:warn).with(any_args)
expect(CarrierWave.deprecator).to receive(:warn).with(any_args)
instance.manipulate! :read => {:size => %{'200x200'}}
end

Expand Down
2 changes: 1 addition & 1 deletion spec/uploader/content_type_allowlist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
it "uses the whitelist but shows deprecation" do
allow(uploader).to receive(:content_type_whitelist).and_return(['image/gif'])

expect(ActiveSupport::Deprecation).to receive(:warn).with('#content_type_whitelist is deprecated, use #content_type_allowlist instead.')
expect(CarrierWave.deprecator).to receive(:warn).with('#content_type_whitelist is deprecated, use #content_type_allowlist instead.')
expect {
uploader.cache!(bork_file)
}.to raise_error(CarrierWave::IntegrityError)
Expand Down
4 changes: 2 additions & 2 deletions spec/uploader/content_type_denylist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
end

it "shows up" do
expect(ActiveSupport::Deprecation).to receive(:warn).with('Use of #content_type_denylist is deprecated for the security reason, use #content_type_allowlist instead to explicitly state what are safe to accept')
expect(CarrierWave.deprecator).to receive(:warn).with('Use of #content_type_denylist is deprecated for the security reason, use #content_type_allowlist instead to explicitly state what are safe to accept')
expect { uploader.cache!(ruby_file) }.to raise_error(CarrierWave::IntegrityError)
end
end
Expand Down Expand Up @@ -75,7 +75,7 @@
it "uses the blacklist but shows deprecation" do
allow(uploader).to receive(:content_type_blacklist).and_return(['image/png'])

expect(ActiveSupport::Deprecation).to receive(:warn).with('#content_type_blacklist is deprecated, use #content_type_denylist instead.')
expect(CarrierWave.deprecator).to receive(:warn).with('#content_type_blacklist is deprecated, use #content_type_denylist instead.')
expect { uploader.cache!(ruby_file) }.to raise_error(CarrierWave::IntegrityError)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/uploader/extension_allowlist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
it "uses the whitelist but shows deprecation" do
allow(@uploader).to receive(:extension_whitelist).and_return(%w(txt doc xls))

expect(ActiveSupport::Deprecation).to receive(:warn).with('#extension_whitelist is deprecated, use #extension_allowlist instead.')
expect(CarrierWave.deprecator).to receive(:warn).with('#extension_whitelist is deprecated, use #extension_allowlist instead.')
expect {
@uploader.cache!(File.open(file_path('test.jpg')))
}.to raise_error(CarrierWave::IntegrityError)
Expand Down
4 changes: 2 additions & 2 deletions spec/uploader/extension_denylist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
end

it "shows up" do
expect(ActiveSupport::Deprecation).to receive(:warn).with('Use of #extension_denylist is deprecated for the security reason, use #extension_allowlist instead to explicitly state what are safe to accept')
expect(CarrierWave.deprecator).to receive(:warn).with('Use of #extension_denylist is deprecated for the security reason, use #extension_allowlist instead to explicitly state what are safe to accept')
expect { subject }.to raise_error(CarrierWave::IntegrityError)
end
end
Expand Down Expand Up @@ -125,7 +125,7 @@
it "uses the blacklist but shows deprecation" do
allow(uploader).to receive(:extension_blacklist).and_return(%w(jpg gif png))

expect(ActiveSupport::Deprecation).to receive(:warn).with('#extension_blacklist is deprecated, use #extension_denylist instead.')
expect(CarrierWave.deprecator).to receive(:warn).with('#extension_blacklist is deprecated, use #extension_denylist instead.')
expect { subject }.to raise_error(CarrierWave::IntegrityError)
end

Expand Down

0 comments on commit b25477d

Please sign in to comment.