Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate TargetRailsVersion to the new requires_gem RuboCop API #1137

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/change_migrate_to_requires_gem_api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1137](https://github.com/rubocop/rubocop-rails/pull/1137): Migrate to `TargetRailsVersion` the new [`requires_gem` API](https://github.com/rubocop/rubocop/pull/12186). ([@amomchilov][])
31 changes: 29 additions & 2 deletions lib/rubocop/cop/mixin/target_rails_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,40 @@ module RuboCop
module Cop
# Common functionality for checking target rails version.
module TargetRailsVersion
# Informs the base RuboCop gem that it the Rails version is checked via `requires_gem` API,
# without needing to call this `#support_target_rails_version` method.
USES_REQUIRES_GEM_API = true

def minimum_target_rails_version(version)
@minimum_target_rails_version = version
if respond_to?(:requires_gem)
case version
when Integer, Float then requires_gem(TARGET_GEM_NAME, ">= #{version}")
when String then requires_gem(TARGET_GEM_NAME, version)
end
else
# Fallback path for previous versions of RuboCop which don't support the `requires_gem` API yet.
@minimum_target_rails_version = version
end
end

def support_target_rails_version?(version)
@minimum_target_rails_version <= version
if respond_to?(:requires_gem)
return false unless gem_requirements

gem_requirement = gem_requirements[TARGET_GEM_NAME]
return true unless gem_requirement # If we have no requirement, then we support all versions

gem_requirement.satisfied_by?(Gem::Version.new(version))
else
# Fallback path for previous versions of RuboCop which don't support the `requires_gem` API yet.
@minimum_target_rails_version <= version
end
end

# Look for `railties` instead of `rails`, to support apps that only use a subset of `rails`
# See https://github.com/rubocop/rubocop/pull/11289
TARGET_GEM_NAME = 'railties'
private_constant :TARGET_GEM_NAME
end
end
end
62 changes: 31 additions & 31 deletions spec/rubocop/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@
remote: https://rubygems.org/
specs:
actionmailer (4.1.0)
actionpack (= 4.1.0)
actionview (= 4.1.0)
mail (~> 2.5.4)
rails (4.1.0)
actionmailer (= 4.1.0)
actionpack (= 4.1.0)
actionview (= 4.1.0)
activemodel (= 4.1.0)
activerecord (= 4.1.0)
activesupport (= 4.1.0)
bundler (>= 1.3.0, < 2.0)
railties (= 4.1.0)
sprockets-rails (~> 2.0)
railties (4.1.0)
actionpack (4.1.0)
actionview (4.1.0)
mail (2.5.4)
rails (4.1.0)
actionmailer (= 4.1.0)
actionpack (= 4.1.0)
actionview (= 4.1.0)
activemodel (= 4.1.0)
activerecord (= 4.1.0)
activesupport (= 4.1.0)
bundler (>= 1.3.0, < 2.0)
railties (= 4.1.0)
sprockets-rails (~> 2.0)
railties (4.1.0)

PLATFORMS
ruby
Expand All @@ -96,20 +96,20 @@
remote: https://rubygems.org/
specs:
actionmailer (4.1.0)
actionpack (= 4.1.0)
actionview (= 4.1.0)
mail (~> 2.5.4)
rails (400.33.22)
actionmailer (= 4.1.0)
actionpack (= 4.1.0)
actionview (= 4.1.0)
activemodel (= 4.1.0)
activerecord (= 4.1.0)
activesupport (= 4.1.0)
bundler (>= 1.3.0, < 2.0)
railties (= 4.1.0)
sprockets-rails (~> 2.0)
railties (400.33.22)
actionpack (4.1.0)
actionview (4.1.0)
mail (2.5.4)
rails (400.33.22)
actionmailer (= 4.1.0)
actionpack (= 4.1.0)
actionview (= 4.1.0)
activemodel (= 4.1.0)
activerecord (= 4.1.0)
activesupport (= 4.1.0)
bundler (>= 1.3.0, < 2.0)
railties (= 4.1.0)
sprockets-rails (~> 2.0)
railties (400.33.22)

PLATFORMS
ruby
Expand All @@ -131,9 +131,9 @@
remote: https://rubygems.org/
specs:
actionmailer (4.1.0)
actionpack (= 4.1.0)
actionview (= 4.1.0)
mail (~> 2.5.4)
actionpack (4.1.0)
actionview (4.1.0)
mail (2.5.4)

PLATFORMS
ruby
Expand Down