File tree 6 files changed +116
-0
lines changed
6 files changed +116
-0
lines changed Original file line number Diff line number Diff line change
1
+ Description:
2
+ Generates a deserializable resource for the given model.
3
+
4
+ Example:
5
+ `rails generate jsonapi:deserializable User`
Original file line number Diff line number Diff line change
1
+ class Jsonapi ::DeserializableGenerator < Rails ::Generators ::NamedBase
2
+ source_root File . expand_path ( '../templates' , __FILE__ )
3
+
4
+ # TODO(beauby): Implement generator-level whitelisting.
5
+ # TODO(beauby): Implement versioning.
6
+
7
+ def copy_deserializable_file
8
+ template 'deserializable.rb.erb' ,
9
+ File . join ( 'app/deserializable' , class_path ,
10
+ "deserializable_#{ file_name } .rb" )
11
+ end
12
+
13
+ private
14
+
15
+ def model_klass
16
+ # TODO(beauby): Ensure the model class exists.
17
+ class_name . safe_constantize
18
+ end
19
+
20
+ def attr_names
21
+ attrs = model_klass . new . attribute_names - [ 'id' ]
22
+ fk_attrs = model_klass . reflect_on_all_associations ( :belongs_to )
23
+ . map ( &:foreign_key )
24
+ attrs - fk_attrs
25
+ end
26
+
27
+ def has_one_rel_names
28
+ model_klass . reflect_on_all_associations ( :has_one ) . map ( &:name ) +
29
+ model_klass . reflect_on_all_associations ( :belongs_to ) . map ( &:name )
30
+ end
31
+
32
+ def has_many_rel_names
33
+ model_klass . reflect_on_all_associations ( :has_many ) . map ( &:name )
34
+ end
35
+ end
Original file line number Diff line number Diff line change
1
+ <% module_namespacing do -%>
2
+ class Deserializable<%= class_name %> < JSONAPI::Deserializable::Model
3
+ id
4
+
5
+ <% attr_names . each do |attr | -%>
6
+ attribute : <%= attr %>
7
+ <% end -%>
8
+
9
+ <% has_one_rel_names . each do |rel | -%>
10
+ has_one : <%= rel %>
11
+ <% end -%>
12
+ <% has_many_rel_names . each do |rel | -%>
13
+ has_many : <%= rel %>
14
+ <% end -%>
15
+ end
16
+ <% end -%>
Original file line number Diff line number Diff line change
1
+ Description:
2
+ Generates a serializable resource for the given model.
3
+
4
+ Example:
5
+ `rails generate jsonapi:serializable User`
Original file line number Diff line number Diff line change
1
+ class Jsonapi ::SerializableGenerator < Rails ::Generators ::NamedBase
2
+ source_root File . expand_path ( '../templates' , __FILE__ )
3
+
4
+ # TODO(beauby): Implement generator-level whitelisting.
5
+ # TODO(beauby): Implement versioning.
6
+
7
+ def copy_serializable_file
8
+ template 'serializable.rb.erb' ,
9
+ File . join ( 'app/serializable' , class_path ,
10
+ "serializable_#{ file_name } .rb" )
11
+ end
12
+
13
+ private
14
+
15
+ def model_klass
16
+ # TODO(beauby): Ensure the model class exists.
17
+ class_name . safe_constantize
18
+ end
19
+
20
+ def type
21
+ model_klass . name . underscore . pluralize
22
+ end
23
+
24
+ def attr_names
25
+ attrs = model_klass . new . attribute_names - [ 'id' ]
26
+ fk_attrs = model_klass . reflect_on_all_associations ( :belongs_to )
27
+ . map ( &:foreign_key )
28
+ attrs - fk_attrs
29
+ end
30
+
31
+ def has_one_rel_names
32
+ model_klass . reflect_on_all_associations ( :has_one ) . map ( &:name ) +
33
+ model_klass . reflect_on_all_associations ( :belongs_to ) . map ( &:name )
34
+ end
35
+
36
+ def has_many_rel_names
37
+ model_klass . reflect_on_all_associations ( :has_many ) . map ( &:name )
38
+ end
39
+ end
Original file line number Diff line number Diff line change
1
+ <% module_namespacing do -%>
2
+ class Serializable<%= class_name %> < JSONAPI::Serializable::Model
3
+ type '<%= type %> '
4
+
5
+ <% attr_names . each do |attr | -%>
6
+ attribute :<%= attr %>
7
+ <% end -%>
8
+
9
+ <% has_one_rel_names . each do |rel | -%>
10
+ has_one :<%= rel %>
11
+ <% end -%>
12
+ <% has_many_rel_names . each do |rel | -%>
13
+ has_many :<%= rel %>
14
+ <% end -%>
15
+ end
16
+ <% end -%>
You can’t perform that action at this time.
0 commit comments