@@ -2,85 +2,166 @@ module Jsonapi
2
2
class ResourceGenerator < ::Rails ::Generators ::NamedBase
3
3
source_root File . expand_path ( '../templates' , __FILE__ )
4
4
5
- class_option :'no-controller' , type : :boolean , default : false
6
- class_option :'no-serializer' , type : :boolean , default : false
7
- class_option :'no-payload' , type : :boolean , default : false
8
- class_option :'no-strong-resources' , type : :boolean , default : false
9
- class_option :'no-test' , type : :boolean , default : false
10
-
11
- desc "This generator creates a resource file at app/resources"
5
+ class_option :'omit-comments' ,
6
+ type : :boolean ,
7
+ default : false ,
8
+ aliases : [ '--omit-comments' , '-c' ] ,
9
+ desc : 'Generate without documentation comments'
10
+ class_option :'omit-controller' ,
11
+ type : :boolean ,
12
+ default : false ,
13
+ aliases : [ '--omit-controller' ] ,
14
+ desc : 'Generate without controller'
15
+ class_option :'omit-serializer' ,
16
+ type : :boolean ,
17
+ default : false ,
18
+ aliases : [ '--omit-serializer' , '-s' ] ,
19
+ desc : 'Generate without serializer'
20
+ class_option :'omit-payload' ,
21
+ type : :boolean ,
22
+ default : false ,
23
+ aliases : [ '--omit-payload' , '-p' ] ,
24
+ desc : 'Generate without spec payload'
25
+ class_option :'omit-strong-resource' ,
26
+ type : :boolean ,
27
+ default : false ,
28
+ aliases : [ '--omit-strong-resource' , '-r' ] ,
29
+ desc : 'Generate without strong resource'
30
+ class_option :'omit-route' ,
31
+ type : :boolean ,
32
+ default : false ,
33
+ aliases : [ '--omit-route' ] ,
34
+ desc : 'Generate without specs'
35
+ class_option :'omit-tests' ,
36
+ type : :boolean ,
37
+ default : false ,
38
+ aliases : [ '--omit-tests' , '-t' ] ,
39
+ desc : 'Generate without specs'
40
+
41
+ desc "This generator creates a resource file at app/resources, as well as corresponding controller/specs/route/etc"
12
42
def copy_resource_file
13
- unless @options [ 'no-controller' ]
14
- to = File . join ( 'app/controllers' , class_path , "#{ file_name . pluralize } _controller.rb" )
15
- template ( 'controller.rb.erb' , to )
43
+ unless model_klass
44
+ raise "You must define a #{ class_name } model before generating the corresponding resource."
16
45
end
17
46
18
- unless @options [ 'no-serializer' ]
19
- to = File . join ( 'app/serializers' , class_path , "serializable_#{ file_name } .rb" )
20
- template ( 'serializer.rb.erb' , to )
21
- end
47
+ generate_controller unless omit_controller?
48
+ generate_serializer unless omit_serializer?
49
+ generate_application_resource unless application_resource_defined?
50
+ generate_spec_payload unless omit_spec_payload?
51
+ generate_strong_resource unless omit_strong_resource?
52
+ generate_route unless omit_route?
53
+ generate_tests unless omit_tests?
54
+ generate_resource
55
+ end
22
56
23
- unless 'ApplicationResource' . safe_constantize
24
- to = File . join ( 'app/resources' , class_path , "application_resource.rb" )
25
- template ( 'application_resource.rb.erb' , to )
26
- end
57
+ private
27
58
28
- unless @options [ 'no-payload' ]
29
- to = File . join ( 'spec/payloads' , class_path , "#{ file_name } .rb" )
30
- template ( 'payload.rb.erb' , to )
31
- end
59
+ def omit_comments?
60
+ @options [ 'omit-comments' ]
61
+ end
62
+
63
+ def generate_controller
64
+ to = File . join ( 'app/controllers' , class_path , "#{ file_name . pluralize } _controller.rb" )
65
+ template ( 'controller.rb.erb' , to )
66
+ end
32
67
33
- unless @options [ 'no-strong-resources' ]
34
- inject_into_file 'config/initializers/strong_resources.rb' , after : "StrongResources.configure do\n " do <<-STR
68
+ def omit_controller?
69
+ @options [ 'omit-controller' ]
70
+ end
71
+
72
+ def generate_serializer
73
+ to = File . join ( 'app/serializers' , class_path , "serializable_#{ file_name } .rb" )
74
+ template ( 'serializer.rb.erb' , to )
75
+ end
76
+
77
+ def omit_serializer?
78
+ @options [ 'omit-serializer' ]
79
+ end
80
+
81
+ def generate_application_resource
82
+ to = File . join ( 'app/resources' , class_path , "application_resource.rb" )
83
+ template ( 'application_resource.rb.erb' , to )
84
+ end
85
+
86
+ def application_resource_defined?
87
+ 'ApplicationResource' . safe_constantize . present?
88
+ end
89
+
90
+ def generate_spec_payload
91
+ to = File . join ( 'spec/payloads' , class_path , "#{ file_name } .rb" )
92
+ template ( 'payload.rb.erb' , to )
93
+ end
94
+
95
+ def omit_spec_payload?
96
+ @options [ 'no-payload' ]
97
+ end
98
+
99
+ def generate_strong_resource
100
+ code = <<-STR
35
101
strong_resource :#{ file_name } do
36
102
# Your attributes go here, e.g.
37
103
# attribute :name, :string
38
104
end
39
105
40
- STR
41
- end
106
+ STR
107
+ inject_into_file 'config/initializers/strong_resources.rb' , after : "StrongResources.configure do\n " do
108
+ code
42
109
end
110
+ end
111
+
112
+ def omit_strong_resource?
113
+ @options [ 'no-strong-resources' ]
114
+ end
43
115
44
- unless @options [ 'no-route' ]
45
- inject_into_file 'config/routes.rb' , after : "scope '/api' do \n scope '/v1' do \n " do <<-STR
116
+ def generate_route
117
+ code = <<-STR
46
118
resources :#{ type }
47
- STR
48
- end
119
+ STR
120
+ inject_into_file 'config/routes.rb' , after : "scope path: '/api' do\n scope path: '/v1' do\n " do
121
+ code
49
122
end
123
+ end
50
124
51
- unless @options [ 'no-test' ]
52
- to = File . join "spec/api/v1/#{ file_name . pluralize } " ,
53
- class_path ,
54
- "index_spec.rb"
55
- template ( 'index_request_spec.rb.erb' , to )
56
-
57
- to = File . join "spec/api/v1/#{ file_name . pluralize } " ,
58
- class_path ,
59
- "show_spec.rb"
60
- template ( 'show_request_spec.rb.erb' , to )
61
-
62
- to = File . join "spec/api/v1/#{ file_name . pluralize } " ,
63
- class_path ,
64
- "create_spec.rb"
65
- template ( 'create_request_spec.rb.erb' , to )
66
-
67
- to = File . join "spec/api/v1/#{ file_name . pluralize } " ,
68
- class_path ,
69
- "update_spec.rb"
70
- template ( 'update_request_spec.rb.erb' , to )
71
-
72
- to = File . join "spec/api/v1/#{ file_name . pluralize } " ,
73
- class_path ,
74
- "destroy_spec.rb"
75
- template ( 'destroy_request_spec.rb.erb' , to )
76
- end
125
+ def omit_route?
126
+ @options [ 'no-route' ]
127
+ end
128
+
129
+ def generate_tests
130
+ to = File . join "spec/api/v1/#{ file_name . pluralize } " ,
131
+ class_path ,
132
+ "index_spec.rb"
133
+ template ( 'index_request_spec.rb.erb' , to )
134
+
135
+ to = File . join "spec/api/v1/#{ file_name . pluralize } " ,
136
+ class_path ,
137
+ "show_spec.rb"
138
+ template ( 'show_request_spec.rb.erb' , to )
139
+
140
+ to = File . join "spec/api/v1/#{ file_name . pluralize } " ,
141
+ class_path ,
142
+ "create_spec.rb"
143
+ template ( 'create_request_spec.rb.erb' , to )
144
+
145
+ to = File . join "spec/api/v1/#{ file_name . pluralize } " ,
146
+ class_path ,
147
+ "update_spec.rb"
148
+ template ( 'update_request_spec.rb.erb' , to )
149
+
150
+ to = File . join "spec/api/v1/#{ file_name . pluralize } " ,
151
+ class_path ,
152
+ "destroy_spec.rb"
153
+ template ( 'destroy_request_spec.rb.erb' , to )
154
+ end
77
155
156
+ def omit_tests?
157
+ @options [ 'no-test' ]
158
+ end
159
+
160
+ def generate_resource
78
161
to = File . join ( 'app/resources' , class_path , "#{ file_name } _resource.rb" )
79
162
template ( 'resource.rb.erb' , to )
80
163
end
81
164
82
- private
83
-
84
165
def model_klass
85
166
class_name . safe_constantize
86
167
end
0 commit comments