Skip to content

Commit

Permalink
Add support for rails 7.1
Browse files Browse the repository at this point in the history
Fixes #230.

rails 7.1 recently added  this new default [configuration option](https://guides.rubyonrails.org/configuring.html#config-active-record-belongs-to-required-validates-foreign-key )

When set to false ( default in rails 7.1 ) an `if` option is added to the belongs_to option. which makes it not compatible with the current `WEAK_OPTIONS` array.

To cover this new case, and as a pre-check, we make sure that we're on rails 7.1.0 minimum as well as the ActiveRecord option ( `ActiveRecord.belongs_to_required_validates_foreign_key` ) is set to false to mitigate issues with real `if` conditions. We also check that the if condition is coupled with a `message` option set to `required`

This commit also adds rails 7.1 to the CI matrix.
  • Loading branch information
chaadow committed Oct 15, 2023
1 parent 18e4aec commit 78ab821
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
- ruby-version: '3.1'
gemfile: 'gemfiles/ar_6_1.gemfile'
- ruby-version: '3.2'
gemfile: 'gemfiles/ar_7_0.gemfile'
gemfile: ['gemfiles/ar_7_0.gemfile', 'gemfiles/ar_7_1.gemfile']
- ruby-version: 'head'
gemfile: 'gemfiles/ar_main.gemfile'

Expand Down
7 changes: 7 additions & 0 deletions gemfiles/ar_7_1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'activerecord', '~> 7.1.0'

gemspec path: '../'
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,23 @@ def report_template(status, column_name:, error_slug: nil)
end

def weak_option?
validators.all? { |validator| validator.options.slice(*WEAK_OPTIONS).any? }
validators.all? do |validator|
result = validator.options.slice(*WEAK_OPTIONS).any?

result = false if required_with_if?(validator)

result
end
end

def belongs_to_required_validates_foreign_key_disabled?
ActiveRecord.version >= Gem::Version.new('7.1.0') &&
!ActiveRecord.belongs_to_required_validates_foreign_key
end

def required_with_if?(validator)
belongs_to_required_validates_foreign_key_disabled? &&
validator.options[:message] == :required && validator.options[:if].present?
end

def check
Expand Down
32 changes: 32 additions & 0 deletions spec/checkers/column_presence_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,25 @@
context 'when `belongs_to` is required' do
let(:klass) { define_class { |klass| klass.belongs_to :user, **required } }

if ActiveRecord.version >= Gem::Version.new('7.1.0')
context 'when belongs_to_required_validates_foreign_key is set to false' do
specify do
old = ActiveRecord.belongs_to_required_validates_foreign_key
ActiveRecord.belongs_to_required_validates_foreign_key = false

expect(checker.report.first).to have_attributes(
checker_name: 'ColumnPresenceChecker',
table_or_model_name: klass.name,
column_or_attribute_name: 'user',
status: :fail,
error_message: nil,
error_slug: :association_missing_null_constraint
)

ActiveRecord.belongs_to_required_validates_foreign_key = old
end
end
end
specify do
expect(checker.report.first).to have_attributes(
checker_name: 'ColumnPresenceChecker',
Expand Down Expand Up @@ -204,6 +223,19 @@
context 'when `belongs_to` is optional' do
let(:klass) { define_class { |klass| klass.belongs_to :user, **optional } }

if ActiveRecord.version >= Gem::Version.new('7.1.0')
context 'when belongs_to_required_validates_foreign_key is set to false' do
specify do
old = ActiveRecord.belongs_to_required_validates_foreign_key
ActiveRecord.belongs_to_required_validates_foreign_key = false

expect(checker.report).to be_nil

ActiveRecord.belongs_to_required_validates_foreign_key = old
end
end
end

specify do
expect(checker.report).to be_nil
end
Expand Down

0 comments on commit 78ab821

Please sign in to comment.