Skip to content

Commit

Permalink
Don't force ActionController::TestCase to load
Browse files Browse the repository at this point in the history
Instead of referencing ActionController::TestCase directly during boot,
we can use a lazy load hook to apply our mixins if it is loaded later.

The action_controller_test_case load hook was added in Rails 5.1, and
the run_once option was added in Rails 5.2. I've left the behaviour
unchanged for older Rails versions where they're not available.
  • Loading branch information
eugeneius committed Feb 21, 2024
1 parent 2170177 commit bb43abd
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/active_model_serializers/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,19 @@ class Railtie < Rails::Railtie
end
# :nocov:

def extend_action_controller_test_case(&block)
if Rails::VERSION::MAJOR >= 6 || Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR >= 2
ActiveSupport.on_load(:action_controller_test_case, run_once: true, &block)
else
ActionController::TestCase.instance_eval(&block)
end
end

if Rails.env.test?
ActionController::TestCase.send(:include, ActiveModelSerializers::Test::Schema)
ActionController::TestCase.send(:include, ActiveModelSerializers::Test::Serializer)
extend_action_controller_test_case do
include ActiveModelSerializers::Test::Schema
include ActiveModelSerializers::Test::Serializer
end
end
end
end

0 comments on commit bb43abd

Please sign in to comment.