-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1352 from bf4/railties
Fix generators (@dgynn); load Railtie only with Rails, ensures caching configured
- Loading branch information
Showing
15 changed files
with
216 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,23 @@ | ||
require 'active_model' | ||
require 'active_support' | ||
require 'action_controller' | ||
require 'action_controller/railtie' | ||
require 'active_support/core_ext/object/with_options' | ||
module ActiveModelSerializers | ||
mattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) } | ||
|
||
def self.config | ||
ActiveModel::Serializer.config | ||
end | ||
|
||
extend ActiveSupport::Autoload | ||
autoload :Model | ||
autoload :Callbacks | ||
autoload :Deserialization | ||
autoload :Logging | ||
autoload :Test | ||
end | ||
|
||
require 'active_model/serializer' | ||
require 'active_model/serializable_resource' | ||
require 'active_model/serializer/version' | ||
class << self; attr_accessor :logger; end | ||
self.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) | ||
|
||
require 'action_controller/serialization' | ||
ActiveSupport.on_load(:action_controller) do | ||
ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModelSerializers) | ||
include ::ActionController::Serialization | ||
ActionDispatch::Reloader.to_prepare do | ||
ActiveModel::Serializer.serializers_cache.clear | ||
def self.config | ||
ActiveModel::Serializer.config | ||
end | ||
end | ||
|
||
require 'active_model/serializer/railtie' | ||
require 'active_model/serializer/version' | ||
require 'active_model/serializer' | ||
require 'active_model/serializable_resource' | ||
require 'active_model_serializers/railtie' if defined?(::Rails) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'rails/railtie' | ||
require 'action_controller' | ||
require 'action_controller/railtie' | ||
require 'action_controller/serialization' | ||
|
||
module ActiveModelSerializers | ||
class Railtie < Rails::Railtie | ||
config.to_prepare do | ||
ActiveModel::Serializer.serializers_cache.clear | ||
end | ||
|
||
initializer 'active_model_serializers.action_controller' do | ||
ActiveSupport.on_load(:action_controller) do | ||
include(::ActionController::Serialization) | ||
end | ||
end | ||
|
||
# This hook is run after the action_controller railtie has set the configuration | ||
# based on the *environment* configuration and before any config/initializers are run | ||
# and also before eager_loading (if enabled). | ||
initializer 'active_model_serializers.set_configs', :after => 'action_controller.set_configs' do | ||
ActiveModelSerializers.logger = Rails.configuration.action_controller.logger | ||
ActiveModelSerializers.config.cache_store = Rails.configuration.action_controller.cache_store | ||
ActiveModelSerializers.config.perform_caching = Rails.configuration.action_controller.perform_caching | ||
end | ||
|
||
generators do | ||
require 'generators/rails/resource_override' | ||
end | ||
|
||
if Rails.env.test? | ||
ActionController::TestCase.send(:include, ActiveModelSerializers::Test::Schema) | ||
ActionController::TestCase.send(:include, ActiveModelSerializers::Test::Serializer) | ||
end | ||
end | ||
end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Execute this test in isolation | ||
require 'support/isolated_unit' | ||
|
||
class RailtieTest < ActiveSupport::TestCase | ||
include ActiveSupport::Testing::Isolation | ||
|
||
class WithRails < RailtieTest | ||
setup do | ||
require 'rails' | ||
require 'active_model_serializers' | ||
make_basic_app | ||
end | ||
|
||
test 'mixes ActionController::Serialization into ActionController::Base' do | ||
assert ActionController.const_defined?(:Serialization), | ||
"ActionController::Serialization should be defined, but isn't" | ||
assert ::ActionController::Base.included_modules.include?(::ActionController::Serialization), | ||
"ActionController::Serialization should be included in ActionController::Base, but isn't" | ||
end | ||
|
||
test 'sets the ActiveModelSerializers.logger to Rails.logger' do | ||
refute_nil Rails.logger | ||
refute_nil ActiveModelSerializers.logger | ||
assert_equal Rails.logger, ActiveModelSerializers.logger | ||
end | ||
|
||
test 'it is configured for caching' do | ||
assert_equal ActionController::Base.cache_store, ActiveModelSerializers.config.cache_store | ||
assert_equal Rails.configuration.action_controller.perform_caching, ActiveModelSerializers.config.perform_caching | ||
end | ||
end | ||
|
||
class WithoutRails < RailtieTest | ||
setup do | ||
require 'active_model_serializers' | ||
make_basic_app | ||
end | ||
|
||
test 'does not mix ActionController::Serialization into ActionController::Base' do | ||
refute ActionController.const_defined?(:Serialization), | ||
'ActionController::Serialization should not be defined, but is' | ||
end | ||
|
||
test 'has its own logger at ActiveModelSerializers.logger' do | ||
refute_nil Rails.logger | ||
refute_nil ActiveModelSerializers.logger | ||
refute_equal Rails.logger, ActiveModelSerializers.logger | ||
end | ||
|
||
test 'it is not configured for caching' do | ||
refute_nil ActionController::Base.cache_store | ||
assert_nil ActiveModelSerializers.config.cache_store | ||
refute Rails.configuration.action_controller.perform_caching | ||
refute ActiveModelSerializers.config.perform_caching | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# https://github.com/rails/rails/blob/v5.0.0.beta1/railties/test/isolation/abstract_unit.rb | ||
|
||
# Usage Example: | ||
# | ||
# require 'support/isolated_unit' | ||
# | ||
# class RailtieTest < ActiveSupport::TestCase | ||
# include ActiveSupport::Testing::Isolation | ||
# | ||
# class WithRailsDefinedOnLoad < RailtieTest | ||
# setup do | ||
# require 'rails' | ||
# require 'active_model_serializers' | ||
# make_basic_app | ||
# end | ||
# | ||
# # some tests | ||
# end | ||
# | ||
# class WithoutRailsDefinedOnLoad < RailtieTest | ||
# setup do | ||
# require 'active_model_serializers' | ||
# make_basic_app | ||
# end | ||
# | ||
# # some tests | ||
# end | ||
# end | ||
# | ||
# Note: | ||
# It is important to keep this file as light as possible | ||
# the goal for tests that require this is to test booting up | ||
# rails from an empty state, so anything added here could | ||
# hide potential failures | ||
# | ||
# It is also good to know what is the bare minimum to get | ||
# Rails booted up. | ||
require 'bundler/setup' unless defined?(Bundler) | ||
require 'active_support' | ||
require 'active_support/core_ext/string/access' | ||
|
||
# These files do not require any others and are needed | ||
# to run the tests | ||
require 'active_support/testing/autorun' | ||
require 'active_support/testing/isolation' | ||
|
||
module TestHelpers | ||
module Generation | ||
# Make a very basic app, without creating the whole directory structure. | ||
# Is faster and simpler than generating a Rails app in a temp directory | ||
def make_basic_app | ||
require 'rails' | ||
require 'action_controller/railtie' | ||
|
||
@app = Class.new(Rails::Application) do | ||
config.eager_load = false | ||
config.session_store :cookie_store, key: '_myapp_session' | ||
config.active_support.deprecation = :log | ||
config.active_support.test_order = :parallel | ||
ActiveSupport::TestCase.respond_to?(:test_order=) && ActiveSupport::TestCase.test_order = :parallel | ||
config.root = File.dirname(__FILE__) | ||
config.log_level = :info | ||
# Set a fake logger to avoid creating the log directory automatically | ||
fake_logger = Logger.new(nil) | ||
config.logger = fake_logger | ||
end | ||
@app.respond_to?(:secrets) && @app.secrets.secret_key_base = '3b7cd727ee24e8444053437c36cc66c4' | ||
|
||
yield @app if block_given? | ||
@app.initialize! | ||
end | ||
end | ||
end | ||
|
||
class ActiveSupport::TestCase | ||
include TestHelpers::Generation | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters