Skip to content

Commit 96e2faf

Browse files
authored
Use ActiveSupport::Deprecation (#2327)
* 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
1 parent 280e5b3 commit 96e2faf

15 files changed

+143
-197
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

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

89
#### Fixes

lib/grape.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
require 'active_support/core_ext/object/blank'
2727
require 'active_support/core_ext/object/duplicable'
2828
require 'active_support/dependencies/autoload'
29+
require 'active_support/deprecation'
2930
require 'active_support/notifications'
3031
require 'i18n'
3132

lib/grape/dsl/desc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def desc(description, options = {}, &config_block)
6868
end
6969

7070
config_class.configure(&config_block)
71-
warn '[DEPRECATION] Passing a options hash and a block to `desc` is deprecated. Move all hash options to block.' unless options.empty?
71+
ActiveSupport::Deprecation.warn('Passing a options hash and a block to `desc` is deprecated. Move all hash options to block.') if options.any?
7272
options = config_class.settings
7373
else
7474
options = options.merge(description: description)

lib/grape/dsl/inside_route.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,13 @@ def return_no_content
280280
# Deprecated method to send files to the client. Use `sendfile` or `stream`
281281
def file(value = nil)
282282
if value.is_a?(String)
283-
warn '[DEPRECATION] Use sendfile or stream to send files.'
283+
ActiveSupport::Deprecation.warn('Use sendfile or stream to send files.')
284284
sendfile(value)
285285
elsif !value.is_a?(NilClass)
286-
warn '[DEPRECATION] Use stream to use a Stream object.'
286+
ActiveSupport::Deprecation.warn('Use stream to use a Stream object.')
287287
stream(value)
288288
else
289-
warn '[DEPRECATION] Use sendfile or stream to send files.'
289+
ActiveSupport::Deprecation.warn('Use sendfile or stream to send files.')
290290
sendfile
291291
end
292292
end

lib/grape/exceptions/missing_group_type.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,4 @@ def initialize
1010
end
1111
end
1212

13-
Grape::Exceptions::MissingGroupTypeError = Class.new(Grape::Exceptions::MissingGroupType) do
14-
def initialize(*)
15-
super
16-
warn '[DEPRECATION] `Grape::Exceptions::MissingGroupTypeError` is deprecated. Use `Grape::Exceptions::MissingGroupType` instead.'
17-
end
18-
end
13+
Grape::Exceptions::MissingGroupTypeError = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Grape::Exceptions::MissingGroupTypeError', 'Grape::Exceptions::MissingGroupType')

lib/grape/exceptions/unsupported_group_type.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,4 @@ def initialize
1010
end
1111
end
1212

13-
Grape::Exceptions::UnsupportedGroupTypeError = Class.new(Grape::Exceptions::UnsupportedGroupType) do
14-
def initialize(*)
15-
super
16-
warn '[DEPRECATION] `Grape::Exceptions::UnsupportedGroupTypeError` is deprecated. Use `Grape::Exceptions::UnsupportedGroupType` instead.'
17-
end
18-
end
13+
Grape::Exceptions::UnsupportedGroupTypeError = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Grape::Exceptions::UnsupportedGroupTypeError', 'Grape::Exceptions::UnsupportedGroupType')

lib/grape/router/route.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ def warn_route_methods(name, location, expected = nil)
8484
path, line = *location.scan(SOURCE_LOCATION_REGEXP).first
8585
path = File.realpath(path) if Pathname.new(path).relative?
8686
expected ||= name
87-
warn <<~WARNING
88-
#{path}:#{line}: The route_xxx methods such as route_#{name} have been deprecated, please use #{expected}.
89-
WARNING
87+
ActiveSupport::Deprecation.warn("#{path}:#{line}: The route_xxx methods such as route_#{name} have been deprecated, please use #{expected}.")
9088
end
9189
end
9290
end

lib/grape/validations/validators/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ def fail_fast?
9595
end
9696

9797
Grape::Validations::Base = Class.new(Grape::Validations::Validators::Base) do
98-
def initialize(*)
98+
def self.inherited(*)
99+
ActiveSupport::Deprecation.warn 'Grape::Validations::Base is deprecated! Use Grape::Validations::Validators::Base instead.'
99100
super
100-
warn '[DEPRECATION] `Grape::Validations::Base` is deprecated. Use `Grape::Validations::Validators::Base` instead.'
101101
end
102102
end

lib/grape/validations/validators/values_validator.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ def initialize(attrs, options, required, scope, **opts)
1010
@values = options[:value]
1111
@proc = options[:proc]
1212

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

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

18-
warn '[DEPRECATION] The values validator proc option is deprecated. ' \
19-
'The lambda expression can now be assigned directly to values.' if @proc
17+
ActiveSupport::Deprecation.warn('The values validator proc option is deprecated. The lambda expression can now be assigned directly to values.') if @proc
2018
else
2119
@excepts = nil
2220
@values = nil

spec/grape/api/custom_validations_spec.rb

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,17 @@
11
# frozen_string_literal: true
22

3-
describe Grape::Validations do
4-
context 'deprecated Grape::Validations::Base' do
5-
subject do
6-
Class.new(Grape::API) do
7-
params do
8-
requires :text, validator_with_old_base: true
9-
end
10-
get do
11-
end
12-
end
13-
end
14-
15-
let(:validator_with_old_base) do
16-
Class.new(Grape::Validations::Base) do
17-
def validate_param!(_attr_name, _params)
18-
true
19-
end
20-
end
21-
end
22-
23-
before do
24-
described_class.register_validator('validator_with_old_base', validator_with_old_base)
25-
allow(Warning).to receive(:warn)
26-
end
3+
require 'shared/deprecated_class_examples'
274

28-
after do
29-
described_class.deregister_validator('validator_with_old_base')
30-
end
31-
32-
def app
33-
subject
5+
describe Grape::Validations do
6+
describe 'Grape::Validations::Base' do
7+
let(:deprecated_class) do
8+
Class.new(Grape::Validations::Base)
349
end
3510

36-
it 'puts a deprecation warning' do
37-
expect(Warning).to receive(:warn) do |message|
38-
expect(message).to include('`Grape::Validations::Base` is deprecated')
39-
end
40-
41-
get '/'
42-
end
11+
it_behaves_like 'deprecated class'
4312
end
4413

45-
context 'using a custom length validator' do
14+
describe 'using a custom length validator' do
4615
subject do
4716
Class.new(Grape::API) do
4817
params do
@@ -64,6 +33,7 @@ def validate_param!(attr_name, params)
6433
end
6534
end
6635
end
36+
let(:app) { Rack::Builder.new(subject) }
6737

6838
before do
6939
described_class.register_validator('default_length', default_length_validator)
@@ -73,10 +43,6 @@ def validate_param!(attr_name, params)
7343
described_class.deregister_validator('default_length')
7444
end
7545

76-
def app
77-
subject
78-
end
79-
8046
it 'under 140 characters' do
8147
get '/', text: 'abc'
8248
expect(last_response.status).to eq 200
@@ -96,7 +62,7 @@ def app
9662
end
9763
end
9864

99-
context 'using a custom body-only validator' do
65+
describe 'using a custom body-only validator' do
10066
subject do
10167
Class.new(Grape::API) do
10268
params do
@@ -115,6 +81,7 @@ def validate(request)
11581
end
11682
end
11783
end
84+
let(:app) { Rack::Builder.new(subject) }
11885

11986
before do
12087
described_class.register_validator('in_body', in_body_validator)
@@ -124,10 +91,6 @@ def validate(request)
12491
described_class.deregister_validator('in_body')
12592
end
12693

127-
def app
128-
subject
129-
end
130-
13194
it 'allows field in body' do
13295
get '/', text: 'abc'
13396
expect(last_response.status).to eq 200
@@ -141,7 +104,7 @@ def app
141104
end
142105
end
143106

144-
context 'using a custom validator with message_key' do
107+
describe 'using a custom validator with message_key' do
145108
subject do
146109
Class.new(Grape::API) do
147110
params do
@@ -160,6 +123,7 @@ def validate_param!(attr_name, _params)
160123
end
161124
end
162125
end
126+
let(:app) { Rack::Builder.new(subject) }
163127

164128
before do
165129
described_class.register_validator('with_message_key', message_key_validator)
@@ -169,18 +133,14 @@ def validate_param!(attr_name, _params)
169133
described_class.deregister_validator('with_message_key')
170134
end
171135

172-
def app
173-
subject
174-
end
175-
176136
it 'fails with message' do
177137
get '/', text: 'foobar'
178138
expect(last_response.status).to eq 400
179139
expect(last_response.body).to eq 'text is missing'
180140
end
181141
end
182142

183-
context 'using a custom request/param validator' do
143+
describe 'using a custom request/param validator' do
184144
subject do
185145
Class.new(Grape::API) do
186146
params do
@@ -208,6 +168,7 @@ def validate(request)
208168
end
209169
end
210170
end
171+
let(:app) { Rack::Builder.new(subject) }
211172

212173
before do
213174
described_class.register_validator('admin', admin_validator)
@@ -217,10 +178,6 @@ def validate(request)
217178
described_class.deregister_validator('admin')
218179
end
219180

220-
def app
221-
subject
222-
end
223-
224181
it 'fail when non-admin user sets an admin field' do
225182
get '/', admin_field: 'tester', non_admin_field: 'toaster'
226183
expect(last_response.status).to eq 400

0 commit comments

Comments
 (0)