Skip to content

Commit 0ab12a9

Browse files
committed
Add processor generator, allow resource generator to create others
Restructure the generators directory structure Controller generator can now add routes
1 parent 978f590 commit 0ab12a9

File tree

19 files changed

+249
-61
lines changed

19 files changed

+249
-61
lines changed

jsonapi-resources.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
1919
spec.require_paths = ['lib']
2020
spec.required_ruby_version = '>= 2.3'
2121

22-
spec.add_development_dependency 'bundler', '~> 1.17.3'
22+
spec.add_development_dependency 'bundler'
2323
spec.add_development_dependency 'rake'
2424
spec.add_development_dependency 'minitest', '~> 5.10', '!= 5.10.2'
2525
spec.add_development_dependency 'minitest-spec-rails'

lib/generators/jsonapi/USAGE

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Description:
2+
Controller Generator for JSONAPI Resources
3+
4+
Examples:
5+
rails generate jsonapi:controller Post
6+
7+
This will:
8+
create: app/controllers/posts_controller.rb
9+
add: a route to routes.rb
10+
11+
Note: If the resource is namespaced the namespace will be duplicated for each route
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module Jsonapi
2+
class ControllerGenerator < Rails::Generators::NamedBase
3+
source_root File.expand_path('../templates', __FILE__)
4+
5+
class_option :base_controller, type: :string, default: 'JSONAPI::ResourceController'
6+
class_option :skip_routes, type: :boolean, desc: "Don't add routes to config/routes.rb."
7+
8+
def create_resource
9+
template_file = File.join(
10+
'app/controllers',
11+
class_path,
12+
"#{file_name.pluralize}_controller.rb"
13+
)
14+
template 'controller.rb.tt', template_file
15+
end
16+
17+
def add_routes
18+
return if options[:skip_routes]
19+
route generate_routing_code
20+
end
21+
22+
private
23+
24+
def base_controller
25+
options['base_controller']
26+
end
27+
28+
# This method creates nested route entry for namespaced resources.
29+
# For eg. rails g controller foo/bar/baz index show
30+
# Will generate -
31+
# namespace :foo do
32+
# namespace :bar do
33+
# get 'baz/index'
34+
# get 'baz/show'
35+
# end
36+
# end
37+
def generate_routing_code
38+
depth = 0
39+
lines = []
40+
41+
# Create 'namespace' ladder
42+
# namespace :foo do
43+
# namespace :bar do
44+
regular_class_path.each do |ns|
45+
lines << indent("namespace :#{ns} do\n", depth * 2)
46+
depth += 1
47+
end
48+
49+
lines << indent(%{jsonapi_resources :#{file_name.pluralize}\n}, depth * 2)
50+
51+
# Create `end` ladder
52+
# end
53+
# end
54+
until depth.zero?
55+
depth -= 1
56+
lines << indent("end\n", depth * 2)
57+
end
58+
59+
lines.join
60+
end
61+
end
62+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<% module_namespacing do -%>
2+
class <%= class_name.pluralize %>Controller < <%= base_controller %>
3+
end
4+
<% end -%>

lib/generators/jsonapi/controller_generator.rb

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Description:
2+
Processor Generator for JSONAPI Resources
3+
4+
Examples:
5+
rails generate jsonapi:processor Post
6+
7+
This will:
8+
create: app/processors/post_processor.rb
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Jsonapi
2+
class ProcessorGenerator < Rails::Generators::NamedBase
3+
source_root File.expand_path('../templates', __FILE__)
4+
5+
class_option :base_processor, type: :string, default: 'JSONAPI::Processor'
6+
7+
def create_processor
8+
template_file = File.join(
9+
'app/processors',
10+
class_path,
11+
"#{file_name.singularize}_processor.rb"
12+
)
13+
template 'processor.rb.tt', template_file
14+
end
15+
16+
private
17+
18+
def base_processor
19+
options['base_processor']
20+
end
21+
end
22+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<% module_namespacing do -%>
2+
class <%= class_name.singularize %>Processor < <%= base_processor %>
3+
end
4+
<% end -%>

lib/generators/jsonapi/resource/USAGE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Description:
2+
Generator for JSONAPI Resources
3+
4+
Options:
5+
--controller Also generate a controller
6+
--processor Also generate a processor
7+
--model Also generate a model
8+
9+
Examples:
10+
rails generate jsonapi:resource Post
11+
12+
This will:
13+
create: app/resources/post_resource.rb

0 commit comments

Comments
 (0)