mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
I was seeing transient failures where adapters may not be registered. e.g. https://travis-ci.org/rails-api/active_model_serializers/builds/77735382 Since we're using the Adapter, JsonApi, and Json classes as namespaces, some of the conventions we use for modules don't apply. Basically, we don't want to define the class anywhere besides itself. Otherwise, the inherited hooks may not run, and some adapters may not be registered. For example: If we have a class Api `class Api; end` And Api is also used as a namespace for `Api::Product` And the classes are defined in different files. In one file: ```ruby class Api autoload :Product def self.inherited(subclass) puts p [:inherited, subclass.name] puts end end ``` And in another: ```ruby class Api class Product < Api def sell_sell_sell! # TODO: sell end end end ``` If we load the Api class file first, the inherited hook will be defined on the class so that when we load the Api::Product class, we'll see the output: ```plain [ :inherited, Api::Product] ``` However, if we load the Api::Product class first, since it defines the `Api` class and then inherited from it, the Api file was never loaded, the hook never defined, and thus never run. By defining the class as `class Api::Product < Api` We ensure the the Api class MUST be defined, and thus, the hook will be defined and run and so sunshine and unicorns. Appendix: The below would work, but triggers a circular reference warning. It's also not recommended to mix require with autoload. ```ruby require 'api' class Api class Product < Api def sell_sell_sell! # TODO: sell end end end ``` This failure scenario was introduced by removing the circular reference warnings in https://github.com/rails-api/active_model_serializers/pull/1067 Style note: To make diffs on the adapters smalleer and easier to read, I've maintained the same identention that was in the original file. I've decided to prefer ease of reading the diff over style, esp. since we may later return to the preferred class declaration style. with '#' will be ignored, and an empty message aborts the commit.
54 lines
1.3 KiB
Ruby
54 lines
1.3 KiB
Ruby
require 'bundler/setup'
|
|
|
|
begin
|
|
require 'simplecov'
|
|
# HACK: till https://github.com/colszowka/simplecov/pull/400 is merged and released.
|
|
# Otherwise you may get:
|
|
# simplecov-0.10.0/lib/simplecov/defaults.rb:50: warning: global variable `$ERROR_INFO' not initialized
|
|
require 'support/simplecov'
|
|
AppCoverage.start
|
|
rescue LoadError
|
|
STDERR.puts 'Running without SimpleCov'
|
|
end
|
|
|
|
require 'timecop'
|
|
require 'rails'
|
|
require 'action_controller'
|
|
require 'action_controller/test_case'
|
|
require 'action_controller/railtie'
|
|
require 'active_support/json'
|
|
require 'fileutils'
|
|
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
|
|
|
|
require 'minitest/autorun'
|
|
# Ensure backward compatibility with Minitest 4
|
|
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
|
|
|
|
require 'capture_warnings'
|
|
@capture_warnings = CaptureWarnings.new(fail_build = true)
|
|
@capture_warnings.before_tests
|
|
if Minitest.respond_to?(:after_run)
|
|
Minitest.after_run do
|
|
@capture_warnings.after_tests
|
|
end
|
|
else
|
|
at_exit do
|
|
STDOUT.puts 'Minitest.after_run not available.'
|
|
@capture_warnings.after_tests
|
|
end
|
|
end
|
|
|
|
require 'active_model_serializers'
|
|
|
|
require 'support/stream_capture'
|
|
|
|
require 'support/rails_app'
|
|
|
|
require 'support/test_case'
|
|
|
|
require 'support/serialization_testing'
|
|
|
|
require 'fixtures/active_record'
|
|
|
|
require 'fixtures/poro'
|