Skip to content

Commit 013f01d

Browse files
guilleiguaranshishirmk
authored andcommitted
Add a generator for Rails
1 parent 6b593cb commit 013f01d

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

lib/generators/serializer/USAGE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Description:
2+
Generates a serializer for the given model.
3+
4+
Example:
5+
rails generate serializer Movie
6+
7+
This will create:
8+
app/serializers/movie_serializer.rb
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'rails/generators/base'
2+
3+
class SerializerGenerator < Rails::Generators::NamedBase
4+
source_root File.expand_path('templates', __dir__)
5+
6+
argument :attributes, type: :array, default: [], banner: 'field field'
7+
8+
def create_serializer_file
9+
template 'serializer.rb.tt', File.join('app', 'serializers', class_path, "#{file_name}_serializer.rb")
10+
end
11+
12+
private
13+
14+
def attributes_names
15+
attributes.map { |a| a.name.to_sym.inspect }
16+
end
17+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<% module_namespacing do -%>
2+
class <%= class_name %>Serializer
3+
include FastJsonapi::ObjectSerializer
4+
attributes <%= attributes_names.join(", ") %>
5+
end
6+
<% end -%>

0 commit comments

Comments
 (0)