mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06: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
|
module ActiveModelSerializers
|
||||||
class Railtie < Rails::Railtie
|
class Railtie < Rails::Railtie
|
||||||
|
config.to_prepare do
|
||||||
|
ActiveModel::Serializer.serializers_cache.clear
|
||||||
|
end
|
||||||
|
|
||||||
initializer 'active_model_serializers.action_controller' do
|
initializer 'active_model_serializers.action_controller' do
|
||||||
ActiveSupport.on_load(:action_controller) do
|
ActiveSupport.on_load(:action_controller) do
|
||||||
ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModelSerializers)
|
ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModelSerializers)
|
||||||
include ::ActionController::Serialization
|
include ::ActionController::Serialization
|
||||||
ActionDispatch::Reloader.to_prepare do
|
|
||||||
ActiveModel::Serializer.serializers_cache.clear
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -25,9 +26,8 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
initializer 'active_model_serializers.generators' do |app|
|
generators do
|
||||||
app.load_generators
|
require 'generators/rails/resource_override'
|
||||||
require 'generators/serializer/resource_override'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if Rails.env.test?
|
if Rails.env.test?
|
||||||
|
|||||||
@ -4,9 +4,7 @@ require 'rails/generators/rails/resource/resource_generator'
|
|||||||
module Rails
|
module Rails
|
||||||
module Generators
|
module Generators
|
||||||
class ResourceGenerator
|
class ResourceGenerator
|
||||||
def add_serializer
|
hook_for :serializer, default: true, boolean: true
|
||||||
invoke 'serializer'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -15,11 +15,11 @@ module Rails
|
|||||||
private
|
private
|
||||||
|
|
||||||
def attributes_names
|
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
|
end
|
||||||
|
|
||||||
def association_names
|
def association_names
|
||||||
attributes.select { |attr| attr.reference? }.map { |a| a.name.to_sym }
|
attributes.select(&:reference?).map! { |a| a.name.to_sym }
|
||||||
end
|
end
|
||||||
|
|
||||||
def parent_class_name
|
def parent_class_name
|
||||||
@ -34,4 +34,3 @@ module Rails
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -1,4 +1,5 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
require 'generators/rails/resource_override'
|
||||||
|
|
||||||
class ResourceGeneratorTest < Rails::Generators::TestCase
|
class ResourceGeneratorTest < Rails::Generators::TestCase
|
||||||
destination File.expand_path('../../../tmp/generators', __FILE__)
|
destination File.expand_path('../../../tmp/generators', __FILE__)
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
require 'generators/serializer/serializer_generator'
|
require 'generators/rails/resource_override'
|
||||||
|
require 'generators/rails/serializer_generator'
|
||||||
|
|
||||||
class SerializerGeneratorTest < Rails::Generators::TestCase
|
class SerializerGeneratorTest < Rails::Generators::TestCase
|
||||||
destination File.expand_path('../../../tmp/generators', __FILE__)
|
destination File.expand_path('../../../tmp/generators', __FILE__)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user