mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Only load generators when needed
- use hook_for to hook in the serializer and remove load_generators - move generators so they can be found by rails - move to_prepare block to railtie config This commit improves the way the generators are loaded and how they extend the resource generator. * The initializer block has been changed to a `generator` block which is only executed when generators are needed. * The call to `app.load_generators` has been removed. There is no need to load *all* generators. * The `resource_override.rb` has been changed to use `hook_for` to extend the resource generator. * The directory for the generators has been moved to match the way Rails looks to load generators. With `hook_for` it would now be possible for a user to pass `--no-serializer` to skip that option. The `--serialize` option also now shows up in the generator help with `rails g resource --help`. These changes follow the way the Draper gem extends the `controller` generator.
This commit is contained in:
parent
0c2153ac5e
commit
94db22c1e0
@ -2,13 +2,14 @@ require 'rails/railtie'
|
||||
|
||||
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
|
||||
ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModelSerializers)
|
||||
include ::ActionController::Serialization
|
||||
ActionDispatch::Reloader.to_prepare do
|
||||
ActiveModel::Serializer.serializers_cache.clear
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -25,9 +26,8 @@ module ActiveModelSerializers
|
||||
end
|
||||
end
|
||||
|
||||
initializer 'active_model_serializers.generators' do |app|
|
||||
app.load_generators
|
||||
require 'generators/serializer/resource_override'
|
||||
generators do
|
||||
require 'generators/rails/resource_override'
|
||||
end
|
||||
|
||||
if Rails.env.test?
|
||||
|
||||
@ -4,9 +4,7 @@ require 'rails/generators/rails/resource/resource_generator'
|
||||
module Rails
|
||||
module Generators
|
||||
class ResourceGenerator
|
||||
def add_serializer
|
||||
invoke 'serializer'
|
||||
end
|
||||
hook_for :serializer, default: true, boolean: true
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -15,11 +15,11 @@ module Rails
|
||||
private
|
||||
|
||||
def attributes_names
|
||||
[:id] + attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym }
|
||||
[:id] + attributes.reject(&:reference?).map! { |a| a.name.to_sym }
|
||||
end
|
||||
|
||||
def association_names
|
||||
attributes.select { |attr| attr.reference? }.map { |a| a.name.to_sym }
|
||||
attributes.select(&:reference?).map! { |a| a.name.to_sym }
|
||||
end
|
||||
|
||||
def parent_class_name
|
||||
@ -34,4 +34,3 @@ module Rails
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
require 'test_helper'
|
||||
require 'generators/rails/resource_override'
|
||||
|
||||
class ResourceGeneratorTest < Rails::Generators::TestCase
|
||||
destination File.expand_path('../../../tmp/generators', __FILE__)
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
require 'test_helper'
|
||||
require 'generators/serializer/serializer_generator'
|
||||
require 'generators/rails/resource_override'
|
||||
require 'generators/rails/serializer_generator'
|
||||
|
||||
class SerializerGeneratorTest < Rails::Generators::TestCase
|
||||
destination File.expand_path('../../../tmp/generators', __FILE__)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user