mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #1912 from richmolj/generator
Ensure generator picks up ApplicationSerializer
This commit is contained in:
commit
11bd778f17
@ -7,6 +7,7 @@ Breaking changes:
|
||||
Fixes:
|
||||
|
||||
- [#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:
|
||||
|
||||
|
||||
@ -25,7 +25,7 @@ module Rails
|
||||
def parent_class_name
|
||||
if options[:parent]
|
||||
options[:parent]
|
||||
elsif defined?(::ApplicationSerializer)
|
||||
elsif 'ApplicationSerializer'.safe_constantize
|
||||
'ApplicationSerializer'
|
||||
else
|
||||
'ActiveModel::Serializer'
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user