Skip to content

Commit 4c226a1

Browse files
committed
Use requires_gem to focus on rack >= 3.1.0
1 parent a4824e5 commit 4c226a1

File tree

2 files changed

+5
-17
lines changed

2 files changed

+5
-17
lines changed

lib/rubocop/cop/rails/unprocessable_content_status.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module Rails
1818
class UnprocessableContentStatus < Base
1919
extend AutoCorrector
2020

21+
requires_gem 'rack', '>= 3.1.0'
22+
2123
MSG = 'Use `:unprocessable_content` instead of `:unprocessable_entity`. ' \
2224
'The `:unprocessable_entity` status is deprecated.'
2325

@@ -30,8 +32,6 @@ class UnprocessableContentStatus < Base
3032
PATTERN
3133

3234
def on_sym(node)
33-
return unless rack_3_1_or_newer?
34-
3535
return unless unprocessable_entity_symbol?(node)
3636
return if in_hash_key_context?(node)
3737

@@ -43,8 +43,6 @@ def on_sym(node)
4343
end
4444

4545
def on_pair(node)
46-
return unless rack_3_1_or_newer?
47-
4846
status_argument?(node) do
4947
add_offense(node.value) do |corrector|
5048
corrector.replace(node.value, ':unprocessable_content')
@@ -54,12 +52,6 @@ def on_pair(node)
5452

5553
private
5654

57-
def rack_3_1_or_newer?
58-
Gem::Version.new(Rack::VERSION) >= Gem::Version.new('3.1.0')
59-
rescue ArgumentError, NoMethodError
60-
false
61-
end
62-
6355
def in_hash_key_context?(node)
6456
node.parent&.pair_type? && node.parent.key == node
6557
end

spec/rubocop/cop/rails/unprocessable_content_status_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::Rails::UnprocessableContentStatus, :config do
4-
context 'when Rack is older than 3.1 or not available' do
5-
before do
6-
allow(cop).to receive(:rack_3_1_or_newer?).and_return(false)
7-
end
4+
context 'when Rack is older than 3.1' do
5+
let(:gem_versions) { { 'rack' => '3.0.0' } }
86

97
it 'does nothing' do
108
expect_no_offenses(<<~RUBY)
@@ -14,9 +12,7 @@
1412
end
1513

1614
context 'when Rack is 3.1 or later' do
17-
before do
18-
allow(cop).to receive(:rack_3_1_or_newer?).and_return(true)
19-
end
15+
let(:gem_versions) { { 'rack' => '3.1.0' } }
2016

2117
it 'registers an offense when using :unprocessable_entity in hash argument' do
2218
expect_offense(<<~RUBY)

0 commit comments

Comments
 (0)