Add a generator for Rails

This commit is contained in:
Guillermo Iguaran 2018-02-11 01:11:25 -05:00 committed by Shishir Kakaraddi
parent 6b593cb36a
commit 013f01dd47
3 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,8 @@
Description:
Generates a serializer for the given model.
Example:
rails generate serializer Movie
This will create:
app/serializers/movie_serializer.rb

View File

@ -0,0 +1,17 @@
require 'rails/generators/base'
class SerializerGenerator < Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)
argument :attributes, type: :array, default: [], banner: 'field field'
def create_serializer_file
template 'serializer.rb.tt', File.join('app', 'serializers', class_path, "#{file_name}_serializer.rb")
end
private
def attributes_names
attributes.map { |a| a.name.to_sym.inspect }
end
end

View File

@ -0,0 +1,6 @@
<% module_namespacing do -%>
class <%= class_name %>Serializer
include FastJsonapi::ObjectSerializer
attributes <%= attributes_names.join(", ") %>
end
<% end -%>