mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +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:
committed by
Benjamin Fleischer
parent
0c2153ac5e
commit
94db22c1e0
6
lib/generators/rails/USAGE
Normal file
6
lib/generators/rails/USAGE
Normal file
@@ -0,0 +1,6 @@
|
||||
Description:
|
||||
Generates a serializer for the given resource.
|
||||
|
||||
Example:
|
||||
`rails generate serializer Account name created_at`
|
||||
|
||||
10
lib/generators/rails/resource_override.rb
Normal file
10
lib/generators/rails/resource_override.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
require 'rails/generators'
|
||||
require 'rails/generators/rails/resource/resource_generator'
|
||||
|
||||
module Rails
|
||||
module Generators
|
||||
class ResourceGenerator
|
||||
hook_for :serializer, default: true, boolean: true
|
||||
end
|
||||
end
|
||||
end
|
||||
36
lib/generators/rails/serializer_generator.rb
Normal file
36
lib/generators/rails/serializer_generator.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
module Rails
|
||||
module Generators
|
||||
class SerializerGenerator < NamedBase
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
check_class_collision :suffix => 'Serializer'
|
||||
|
||||
argument :attributes, :type => :array, :default => [], :banner => 'field:type field:type'
|
||||
|
||||
class_option :parent, :type => :string, :desc => 'The parent class for the generated serializer'
|
||||
|
||||
def create_serializer_file
|
||||
template 'serializer.rb.erb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def attributes_names
|
||||
[:id] + attributes.reject(&:reference?).map! { |a| a.name.to_sym }
|
||||
end
|
||||
|
||||
def association_names
|
||||
attributes.select(&:reference?).map! { |a| a.name.to_sym }
|
||||
end
|
||||
|
||||
def parent_class_name
|
||||
if options[:parent]
|
||||
options[:parent]
|
||||
elsif defined?(::ApplicationSerializer)
|
||||
'ApplicationSerializer'
|
||||
else
|
||||
'ActiveModel::Serializer'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
8
lib/generators/rails/templates/serializer.rb.erb
Normal file
8
lib/generators/rails/templates/serializer.rb.erb
Normal file
@@ -0,0 +1,8 @@
|
||||
<% module_namespacing do -%>
|
||||
class <%= class_name %>Serializer < <%= parent_class_name %>
|
||||
attributes <%= attributes_names.map(&:inspect).join(", ") %>
|
||||
<% association_names.each do |attribute| -%>
|
||||
has_one :<%= attribute %>
|
||||
<% end -%>
|
||||
end
|
||||
<% end -%>
|
||||
Reference in New Issue
Block a user