From 39b538e86bbae41fe7b4c90821ba882533543baa Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Tue, 9 Apr 2024 16:36:01 +0200 Subject: [PATCH] Avoid loading ActionController::TestCase in production Ref: https://github.com/Shopify/active_model_serializers/pull/3 > By referencing that object, it brings in all of the patches defined > in `ActionController::TestCase` to the development and production > environment, including behaviours like disabling the threading > implementation of `ActionController::Live` --- .github/workflows/ci.yml | 1 + lib/active_model_serializers.rb | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 055e50b94..a32a8cc87 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,3 +75,4 @@ jobs: bundle exec rake env: RAILS_VERSION: ${{ matrix.rails-version }} + RAILS_ENV: test diff --git a/lib/active_model_serializers.rb b/lib/active_model_serializers.rb index 7066ebf42..66e375568 100644 --- a/lib/active_model_serializers.rb +++ b/lib/active_model_serializers.rb @@ -7,12 +7,22 @@ begin require 'action_controller' require 'action_controller/serialization' - require 'action_controller/serialization_test_case' ActiveSupport.on_load(:action_controller) do if ::ActionController::Serialization.enabled ActionController::Base.send(:include, ::ActionController::Serialization) - ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions) + + # action_controller_test_case load hook was added in Rails 5.1 + # https://github.com/rails/rails/commit/0510208dd1ff23baa619884c0abcae4d141fae53 + if ActiveSupport::VERSION::STRING < '5.1' + require 'action_controller/serialization_test_case' + ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions) + else + ActiveSupport.on_load(:action_controller_test_case) do + require 'action_controller/serialization_test_case' + ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions) + end + end end end rescue LoadError