Skip to content

Commit

Permalink
Use requires_gem API in TargetRailsVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
amomchilov committed Sep 30, 2023
1 parent e29a070 commit ead1417
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
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

0 comments on commit ead1417

Please sign in to comment.