Ensure generator picks up ApplicationSerializer

ApplicationSerializer could exist, but not be loaded. So, we check
existence by looking at the filesystem instead of defined?.

Fixes https://github.com/rails-api/active_model_serializers/issues/1890
This commit is contained in:
Lee Richmond
2016-09-05 09:48:06 -04:00
parent 1dc2b74059
commit 3f16b75a68
3 changed files with 24 additions and 6 deletions

View File

@@ -20,11 +20,10 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
end
def test_uses_application_serializer_if_one_exists
Object.const_set(:ApplicationSerializer, Class.new)
run_generator
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ApplicationSerializer/
ensure
Object.send :remove_const, :ApplicationSerializer
stub_safe_constantize(expected: 'ApplicationSerializer') do
run_generator
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ApplicationSerializer/
end
end
def test_uses_given_parent
@@ -54,4 +53,22 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
end
end
end
private
def stub_safe_constantize(expected:)
String.class_eval do
alias_method :old, :safe_constantize
end
String.send(:define_method, :safe_constantize) do
Class if self == expected
end
yield
ensure
String.class_eval do
alias_method :safe_constantize, :old
undef_method :old
end
end
end