Skip to content
Open
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/new_add_rails_load_defaults_mismatch_cop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#461](https://github.com/rubocop/rubocop-rails/issues/461): Add new `Rails/LoadDefaultsMismatch` cop that flags when the rails version in `config.load_defaults` version is outdated. ([@nzlaura][])
9 changes: 9 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,15 @@ Rails/LinkToBlank:
Enabled: true
VersionAdded: '0.62'

Rails/LoadDefaultsMismatch:
Description: 'Checks that `config.load_defaults` version has the same major version as the target Rails version.'
StyleGuide: 'https://rails.rubystyle.guide/#config-defaults'
Enabled: pending
Safe: false
VersionAdded: '<<next>>'
Include:
- '**/config/application.rb'

Rails/MailerName:
Description: 'Mailer should end with `Mailer` suffix.'
StyleGuide: 'https://rails.rubystyle.guide/#mailer-name'
Expand Down
56 changes: 56 additions & 0 deletions lib/rubocop/cop/rails/load_defaults_mismatch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Rails
# Checks that `config.load_defaults` is called with a version that
# matches the target Rails version.
#
# When upgrading Rails, `rails app:update` sets `load_defaults` to the
# previous Rails version so you can incrementally adopt new defaults.
# Once the upgrade is complete, `load_defaults` should be updated to
# match the current Rails version. Not updating the version can mean your app
# drifts from the newer default configuration, making future upgrades
# harder.
#
# @safety
# This cop's offense may be a false positive if you are still
# mid-upgrade and have not yet finished adopting the new defaults.
#
# @example
# # Assuming the project is using Rails 8.1
#
# # bad
# config.load_defaults 6.1
#
# # good
# config.load_defaults 8.1
#
class LoadDefaultsMismatch < Base
MSG = '`load_defaults` is set to `%<load_version>s` but the target Rails version is `%<rails_version>s`. ' \
'Update `load_defaults` to match the Rails version after completing the upgrade.'

RESTRICT_ON_SEND = %i[load_defaults].freeze

# @!method load_defaults_version(node)
def_node_matcher :load_defaults_version, <<~PATTERN
(send _ :load_defaults ${float_type? int_type?})
PATTERN

def on_send(node)
load_defaults_version(node) do |version_node|
load_version = version_node.value.to_f
rails_version = target_rails_version

return unless rails_version

return if load_version == rails_version

message = format(MSG, load_version: load_version, rails_version: rails_version)
add_offense(version_node, message: message)
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
require_relative 'rails/inverse_of'
require_relative 'rails/lexically_scoped_action_filter'
require_relative 'rails/link_to_blank'
require_relative 'rails/load_defaults_mismatch'
require_relative 'rails/mailer_name'
require_relative 'rails/match_route'
require_relative 'rails/migration_class_name'
Expand Down
62 changes: 62 additions & 0 deletions spec/rubocop/cop/rails/load_defaults_mismatch_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::LoadDefaultsMismatch, :config do
context 'when targeting Rails 8.1', :rails81 do
it 'does not register an offense when load_defaults matches the target version' do
expect_no_offenses(<<~RUBY)
config.load_defaults 8.1
RUBY
end

it 'registers an offense when load_defaults has a different minor version' do
expect_offense(<<~RUBY)
config.load_defaults 7.0
^^^ `load_defaults` is set to `7.0` but the target Rails version is `8.1`. Update `load_defaults` to match the Rails version after completing the upgrade.
RUBY
end

it 'registers an offense when load_defaults has a different major version' do
expect_offense(<<~RUBY)
config.load_defaults 6.1
^^^ `load_defaults` is set to `6.1` but the target Rails version is `8.1`. Update `load_defaults` to match the Rails version after completing the upgrade.
RUBY
end

it 'registers an offense when load_defaults major version is ahead' do
expect_offense(<<~RUBY)
config.load_defaults 8.0
^^^ `load_defaults` is set to `8.0` but the target Rails version is `8.1`. Update `load_defaults` to match the Rails version after completing the upgrade.
RUBY
end
end

context 'when targeting Rails 8.0', :rails80 do
it 'does not register an offense when load_defaults matches' do
expect_no_offenses(<<~RUBY)
config.load_defaults 8.0
RUBY
end

it 'registers an offense when load_defaults is 7.0' do
expect_offense(<<~RUBY)
config.load_defaults 7.0
^^^ `load_defaults` is set to `7.0` but the target Rails version is `8.0`. Update `load_defaults` to match the Rails version after completing the upgrade.
RUBY
end
end

context 'when load_defaults receives an integer', :rails80 do
it 'does not register an offense when it matches' do
expect_no_offenses(<<~RUBY)
config.load_defaults 8
RUBY
end

it 'registers an offense when it does not match' do
expect_offense(<<~RUBY)
config.load_defaults 6
^ `load_defaults` is set to `6.0` but the target Rails version is `8.0`. Update `load_defaults` to match the Rails version after completing the upgrade.
RUBY
end
end
end