Merge pull request #1912 from richmolj/generator

Ensure generator picks up ApplicationSerializer
This commit is contained in:
Lee Richmond 2016-09-06 12:42:37 -07:00 committed by GitHub
commit 11bd778f17
3 changed files with 24 additions and 6 deletions

View File

@ -7,6 +7,7 @@ Breaking changes:
Fixes: Fixes:
- [#1887](https://github.com/rails-api/active_model_serializers/pull/1887) Make the comment reflect what the function does (@johnnymo87) - [#1887](https://github.com/rails-api/active_model_serializers/pull/1887) Make the comment reflect what the function does (@johnnymo87)
- [#1890](https://github.com/rails-api/active_model_serializers/issues/1890) Ensure generator inherits from ApplicationSerializer when available (@richmolj)
Features: Features:

View File

@ -25,7 +25,7 @@ module Rails
def parent_class_name def parent_class_name
if options[:parent] if options[:parent]
options[:parent] options[:parent]
elsif defined?(::ApplicationSerializer) elsif 'ApplicationSerializer'.safe_constantize
'ApplicationSerializer' 'ApplicationSerializer'
else else
'ActiveModel::Serializer' 'ActiveModel::Serializer'

View File

@ -20,11 +20,10 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
end end
def test_uses_application_serializer_if_one_exists def test_uses_application_serializer_if_one_exists
Object.const_set(:ApplicationSerializer, Class.new) stub_safe_constantize(expected: 'ApplicationSerializer') do
run_generator run_generator
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ApplicationSerializer/ assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ApplicationSerializer/
ensure end
Object.send :remove_const, :ApplicationSerializer
end end
def test_uses_given_parent def test_uses_given_parent
@ -54,4 +53,22 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
end end
end 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 end