Skip to content

Commit

Permalink
Use ActiveSupport::Deprecation (#2327)
Browse files Browse the repository at this point in the history
* Replace warn [DEPRECATION] to ActiveSupport::Deprecation.warn
Use ActiveSupport::Deprecation::DeprecatedConstantProxy for exceptions with deprecation
Replace Validator::Base initialize by inherited

* Add shared example deprecated_class and update specs accordingly
Fix rubocop

* Changelog entry
  • Loading branch information
ericproulx committed May 16, 2023
1 parent 280e5b3 commit 96e2faf
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 197 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Features

* [#2326](https://github.com/ruby-grape/grape/pull/2326): Use ActiveSupport extensions - [@ericproulx](https://github.com/ericproulx).
* [#2327](https://github.com/ruby-grape/grape/pull/2327): Use ActiveSupport deprecation - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
1 change: 1 addition & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/object/duplicable'
require 'active_support/dependencies/autoload'
require 'active_support/deprecation'
require 'active_support/notifications'
require 'i18n'

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/dsl/desc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def desc(description, options = {}, &config_block)
end

config_class.configure(&config_block)
warn '[DEPRECATION] Passing a options hash and a block to `desc` is deprecated. Move all hash options to block.' unless options.empty?
ActiveSupport::Deprecation.warn('Passing a options hash and a block to `desc` is deprecated. Move all hash options to block.') if options.any?
options = config_class.settings
else
options = options.merge(description: description)
Expand Down
6 changes: 3 additions & 3 deletions lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,13 @@ def return_no_content
# Deprecated method to send files to the client. Use `sendfile` or `stream`
def file(value = nil)
if value.is_a?(String)
warn '[DEPRECATION] Use sendfile or stream to send files.'
ActiveSupport::Deprecation.warn('Use sendfile or stream to send files.')
sendfile(value)
elsif !value.is_a?(NilClass)
warn '[DEPRECATION] Use stream to use a Stream object.'
ActiveSupport::Deprecation.warn('Use stream to use a Stream object.')
stream(value)
else
warn '[DEPRECATION] Use sendfile or stream to send files.'
ActiveSupport::Deprecation.warn('Use sendfile or stream to send files.')
sendfile
end
end
Expand Down
7 changes: 1 addition & 6 deletions lib/grape/exceptions/missing_group_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@ def initialize
end
end

Grape::Exceptions::MissingGroupTypeError = Class.new(Grape::Exceptions::MissingGroupType) do
def initialize(*)
super
warn '[DEPRECATION] `Grape::Exceptions::MissingGroupTypeError` is deprecated. Use `Grape::Exceptions::MissingGroupType` instead.'
end
end
Grape::Exceptions::MissingGroupTypeError = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Grape::Exceptions::MissingGroupTypeError', 'Grape::Exceptions::MissingGroupType')
7 changes: 1 addition & 6 deletions lib/grape/exceptions/unsupported_group_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@ def initialize
end
end

Grape::Exceptions::UnsupportedGroupTypeError = Class.new(Grape::Exceptions::UnsupportedGroupType) do
def initialize(*)
super
warn '[DEPRECATION] `Grape::Exceptions::UnsupportedGroupTypeError` is deprecated. Use `Grape::Exceptions::UnsupportedGroupType` instead.'
end
end
Grape::Exceptions::UnsupportedGroupTypeError = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Grape::Exceptions::UnsupportedGroupTypeError', 'Grape::Exceptions::UnsupportedGroupType')
4 changes: 1 addition & 3 deletions lib/grape/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ def warn_route_methods(name, location, expected = nil)
path, line = *location.scan(SOURCE_LOCATION_REGEXP).first
path = File.realpath(path) if Pathname.new(path).relative?
expected ||= name
warn <<~WARNING
#{path}:#{line}: The route_xxx methods such as route_#{name} have been deprecated, please use #{expected}.
WARNING
ActiveSupport::Deprecation.warn("#{path}:#{line}: The route_xxx methods such as route_#{name} have been deprecated, please use #{expected}.")
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/validations/validators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ def fail_fast?
end

Grape::Validations::Base = Class.new(Grape::Validations::Validators::Base) do
def initialize(*)
def self.inherited(*)
ActiveSupport::Deprecation.warn 'Grape::Validations::Base is deprecated! Use Grape::Validations::Validators::Base instead.'
super
warn '[DEPRECATION] `Grape::Validations::Base` is deprecated. Use `Grape::Validations::Validators::Base` instead.'
end
end
6 changes: 2 additions & 4 deletions lib/grape/validations/validators/values_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ def initialize(attrs, options, required, scope, **opts)
@values = options[:value]
@proc = options[:proc]

warn '[DEPRECATION] The values validator except option is deprecated. ' \
'Use the except validator instead.' if @excepts
ActiveSupport::Deprecation.warn('The values validator except option is deprecated. Use the except validator instead.') if @excepts

raise ArgumentError, 'proc must be a Proc' if @proc && !@proc.is_a?(Proc)

warn '[DEPRECATION] The values validator proc option is deprecated. ' \
'The lambda expression can now be assigned directly to values.' if @proc
ActiveSupport::Deprecation.warn('The values validator proc option is deprecated. The lambda expression can now be assigned directly to values.') if @proc
else
@excepts = nil
@values = nil
Expand Down
71 changes: 14 additions & 57 deletions spec/grape/api/custom_validations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,17 @@
# frozen_string_literal: true

describe Grape::Validations do
context 'deprecated Grape::Validations::Base' do
subject do
Class.new(Grape::API) do
params do
requires :text, validator_with_old_base: true
end
get do
end
end
end

let(:validator_with_old_base) do
Class.new(Grape::Validations::Base) do
def validate_param!(_attr_name, _params)
true
end
end
end

before do
described_class.register_validator('validator_with_old_base', validator_with_old_base)
allow(Warning).to receive(:warn)
end
require 'shared/deprecated_class_examples'

after do
described_class.deregister_validator('validator_with_old_base')
end

def app
subject
describe Grape::Validations do
describe 'Grape::Validations::Base' do
let(:deprecated_class) do
Class.new(Grape::Validations::Base)
end

it 'puts a deprecation warning' do
expect(Warning).to receive(:warn) do |message|
expect(message).to include('`Grape::Validations::Base` is deprecated')
end

get '/'
end
it_behaves_like 'deprecated class'
end

context 'using a custom length validator' do
describe 'using a custom length validator' do
subject do
Class.new(Grape::API) do
params do
Expand All @@ -64,6 +33,7 @@ def validate_param!(attr_name, params)
end
end
end
let(:app) { Rack::Builder.new(subject) }

before do
described_class.register_validator('default_length', default_length_validator)
Expand All @@ -73,10 +43,6 @@ def validate_param!(attr_name, params)
described_class.deregister_validator('default_length')
end

def app
subject
end

it 'under 140 characters' do
get '/', text: 'abc'
expect(last_response.status).to eq 200
Expand All @@ -96,7 +62,7 @@ def app
end
end

context 'using a custom body-only validator' do
describe 'using a custom body-only validator' do
subject do
Class.new(Grape::API) do
params do
Expand All @@ -115,6 +81,7 @@ def validate(request)
end
end
end
let(:app) { Rack::Builder.new(subject) }

before do
described_class.register_validator('in_body', in_body_validator)
Expand All @@ -124,10 +91,6 @@ def validate(request)
described_class.deregister_validator('in_body')
end

def app
subject
end

it 'allows field in body' do
get '/', text: 'abc'
expect(last_response.status).to eq 200
Expand All @@ -141,7 +104,7 @@ def app
end
end

context 'using a custom validator with message_key' do
describe 'using a custom validator with message_key' do
subject do
Class.new(Grape::API) do
params do
Expand All @@ -160,6 +123,7 @@ def validate_param!(attr_name, _params)
end
end
end
let(:app) { Rack::Builder.new(subject) }

before do
described_class.register_validator('with_message_key', message_key_validator)
Expand All @@ -169,18 +133,14 @@ def validate_param!(attr_name, _params)
described_class.deregister_validator('with_message_key')
end

def app
subject
end

it 'fails with message' do
get '/', text: 'foobar'
expect(last_response.status).to eq 400
expect(last_response.body).to eq 'text is missing'
end
end

context 'using a custom request/param validator' do
describe 'using a custom request/param validator' do
subject do
Class.new(Grape::API) do
params do
Expand Down Expand Up @@ -208,6 +168,7 @@ def validate(request)
end
end
end
let(:app) { Rack::Builder.new(subject) }

before do
described_class.register_validator('admin', admin_validator)
Expand All @@ -217,10 +178,6 @@ def validate(request)
described_class.deregister_validator('admin')
end

def app
subject
end

it 'fail when non-admin user sets an admin field' do
get '/', admin_field: 'tester', non_admin_field: 'toaster'
expect(last_response.status).to eq 400
Expand Down
Loading

0 comments on commit 96e2faf

Please sign in to comment.