Skip to content

Commit

Permalink
Set Rails.application.config.active_record.has_many_inversing = false
Browse files Browse the repository at this point in the history
  • Loading branch information
mayorova committed Apr 16, 2024
1 parent 667cf64 commit d2fc12b
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/models/account/buyer_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Account::BuyerMethods

alias_method :application_contracts, :bought_cinstances

# This causes issues with the Rails 6.1 default config has_many_inversing = true
has_many :contracts, foreign_key: :user_account_id, dependent: :destroy

module UniqueAssociation
Expand Down
6 changes: 6 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ class Application < Rails::Application
# before initializers and causes configuration not to be respected.
config.load_defaults 6.0
# config.load_defaults 6.1

config.active_record.belongs_to_required_by_default = false
config.active_record.include_root_in_json = true

# Support for inversing belongs_to -> has_many Active Record associations.
# Overriding Rails 6.1 default, because it causes various issues
config.active_record.has_many_inversing = false

# Make `form_with` generate non-remote forms. Defaults true in Rails 5.1 to 6.0
config.action_view.form_with_generates_remote_forms = false
# Make Ruby preserve the timezone of the receiver when calling `to_time`.
Expand Down
5 changes: 4 additions & 1 deletion config/initializers/new_framework_defaults_6_1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#
# Read the Guide for Upgrading Ruby on Rails for more info on each option.

# NOTE: set to false in config/application.rb
# Support for inversing belongs_to -> has_many Active Record associations.
Rails.application.config.active_record.has_many_inversing = true
# Rails.application.config.active_record.has_many_inversing = true

# Apply random variation to the delay when retrying failed jobs.
Rails.application.config.active_job.retry_jitter = 0.15
Expand All @@ -16,12 +17,14 @@
# `before_enqueue`/`before_perform` respectively halts with `throw :abort`.
Rails.application.config.active_job.skip_after_callbacks_if_terminated = true

# NOTE: will be applied later
# Specify cookies SameSite protection level: either :none, :lax, or :strict.
#
# This change is not backwards compatible with earlier Rails versions.
# It's best enabled when your entire app is migrated and stable on 6.1.
# Rails.application.config.action_dispatch.cookies_same_site_protection = :lax

# NOTE: will be applied later
# Generate CSRF tokens that are encoded in URL-safe Base64.
#
# This change is not backwards compatible with earlier Rails versions.
Expand Down
2 changes: 2 additions & 0 deletions spec/acceptance/api/access_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'rails_helper'

# NOTE: for some reason Rails 6.1 default has_many_inversing = true affects the behavior tested here.
# property :value is not added, because AccessToken#show_value? returns false when this config is enabled.
resource 'AccessToken' do
let(:resource) { FactoryBot.build(:access_token) }
let(:expected_properties) { %w[id name scopes permission value] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class DeveloperPortal::Admin::Applications::AccessDetailsControllerTest < Develo
get :show

assert_not_nil cinstance = assigns(:cinstance)
# NOTE: this assertion fails when has_many_inversing = true (Rails 6.1 default)
assert_equal cinstance.referrer_filters.size, cinstance.referrer_filters.count
end

Expand Down
6 changes: 6 additions & 0 deletions test/unit/application_keys_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ def test_regenerate_key
assert_in_delta expected_updated_at, @application.updated_at, 1.second
end

# NOTE: with Rails 6.1 default has_many_inversing = true this test fails with the issue:
# Minitest::UnexpectedError: NoMethodError: undefined method `admins' for nil:NilClass
# app/messengers/contract_messenger.rb:7:in `setup'
test 'value can include special characters as defined in the RFC 6749' do
# generate random key with all chars of RFC 6749 except space
random_key = -> { [*"\x21".."\x7E"].shuffle.join }
Expand All @@ -207,6 +210,9 @@ def test_regenerate_key
assert app_key.errors[:value].present?
end

# NOTE: with Rails 6.1 default has_many_inversing = true this test fails with the issue:
# Minitest::UnexpectedError: NoMethodError: undefined method `admins' for nil:NilClass
# app/messengers/contract_messenger.rb:7:in `setup'
test 'is audited' do
app_key = FactoryBot.build(:application_key)

Expand Down
3 changes: 3 additions & 0 deletions test/unit/signup/result_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class ResultTest < ActiveSupport::TestCase
test '#valid? returns false if the user is not valid but the account is' do
user.username = nil
refute user.valid?
# NOTE: With Rails 6.1 default has_many_inversing = true this assertion fails,
# because account has errors: {:users=>["invalid"]}
# Note that this only happens when the objects are built, and not when created.
assert account.valid?
refute signup_result.valid?
end
Expand Down

0 comments on commit d2fc12b

Please sign in to comment.