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

Add support for rails 7.1 #216

Merged
merged 2 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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']
chaadow marked this conversation as resolved.
Show resolved Hide resolved
- 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?
chaadow marked this conversation as resolved.
Show resolved Hide resolved
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
Loading