Only call railtie when Rails is defined; assume controller loaded

Isolated Testing

- Rake test inspired by https://github.com/rails/rails/blob/v5.0.0.beta1/activejob/Rakefile
- Isolated unit inspired by
  - https://github.com/rails/rails/blob/v5.0.0.beta1/railties/test/isolation/abstract_unit.rb
  - https://github.com/rails/rails/blob/v5.0.0.beta1/activemodel/test/cases/railtie_test.rb

Misc

- Turns out `mattr_accessor(:logger) {
  ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }`
  was always nil until the Railtie was loaded, since mattr_accessor
  block defaults don't really work on modules, but on the classes that
  include them.
- Commented on important on Rails being required first for caching to
  work.
- In isolated tests, `active_support/core_ext/object/with_options` is required.
This commit is contained in:
Benjamin Fleischer
2015-12-24 12:21:51 -06:00
parent fe015d17f2
commit 509221c1e0
5 changed files with 194 additions and 15 deletions

View File

@@ -1,7 +1,6 @@
require 'active_model'
require 'active_support'
require 'action_controller'
require 'action_controller/railtie'
require 'active_support/core_ext/object/with_options'
module ActiveModelSerializers
extend ActiveSupport::Autoload
autoload :Model
@@ -10,7 +9,8 @@ module ActiveModelSerializers
autoload :Logging
autoload :Test
mattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
class << self; attr_accessor :logger; end
self.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
def self.config
ActiveModel::Serializer.config
@@ -18,7 +18,6 @@ module ActiveModelSerializers
require 'active_model/serializer/version'
require 'active_model/serializer'
require 'active_model_serializers/railtie'
require 'active_model/serializable_resource'
require 'action_controller/serialization'
require 'active_model_serializers/railtie' if defined?(::Rails)
end

View File

@@ -1,4 +1,7 @@
require 'rails/railtie'
require 'action_controller'
require 'action_controller/railtie'
require 'action_controller/serialization'
module ActiveModelSerializers
class Railtie < Rails::Railtie
@@ -7,10 +10,8 @@ module ActiveModelSerializers
end
initializer 'active_model_serializers.action_controller' do
ActiveSupport.on_load(:action_controller) do
ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModelSerializers)
include ::ActionController::Serialization
end
ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModelSerializers)
ActionController::Base.send(:include, ::ActionController::Serialization)
end
initializer 'active_model_serializers.logger' do
@@ -19,11 +20,19 @@ module ActiveModelSerializers
end
end
initializer 'active_model_serializers.caching' do
ActiveSupport.on_load(:action_controller) do
ActiveModelSerializers.config.cache_store = ActionController::Base.cache_store
ActiveModelSerializers.config.perform_caching = Rails.configuration.action_controller.perform_caching
end
# To be useful, this hook must run after Rails has initialized,
# BUT before any serializers are loaded.
# Otherwise, the call to 'cache' won't find `cache_store` or `perform_caching`
# defined, and serializer's `_cache_store` will be nil.
# IF the load order cannot be changed, then in each serializer that that defines a `cache`,
# manually specify e.g. `PostSerializer._cache_store = Rails.cache` any time
# before the serializer is used. (Even though `ActiveModel::Serializer._cache_store` is
# inheritable, we don't want to set it on `ActiveModel::Serializer` directly unless
# we want *every* serializer to be considered cacheable, regardless of specifying
# `cache # some options` in a serializer or not.
initializer 'active_model_serializers.caching' => :after_initialize do
ActiveModelSerializers.config.cache_store = ActionController::Base.cache_store
ActiveModelSerializers.config.perform_caching = Rails.configuration.action_controller.perform_caching
end
generators do