mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Merge pull request #1912 from richmolj/generator
Ensure generator picks up ApplicationSerializer
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user