Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cb15a02

Browse files
committedMar 8, 2020
Add processor generator, allow resource generator to create others
Restructure the generators directory structure Controller generator can now add routes
1 parent 978f590 commit cb15a02

19 files changed

+249
-61
lines changed
 

‎jsonapi-resources.gemspec

+1-1
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

-13
This file was deleted.
+11
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
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
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

-14
This file was deleted.
+8
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
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
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

+13
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'rails/generators'
2+
3+
module Jsonapi
4+
class ResourceGenerator < Rails::Generators::NamedBase
5+
source_root File.expand_path('../templates', __FILE__)
6+
7+
class_option :base_resource, type: :string, default: 'JSONAPI::Resource'
8+
class_option :controller, type: :boolean, default: false, desc: "Create a controller."
9+
class_option :processor, type: :boolean, default: false, desc: "Create a processor."
10+
class_option :skip_routes, type: :boolean, desc: "Don't add routes to config/routes.rb."
11+
12+
hook_for :controller do |controller|
13+
invoke controller, [ file_path ]
14+
end
15+
16+
hook_for :processor do |processor|
17+
invoke processor, [ file_path ]
18+
end
19+
20+
def create_resource
21+
template_file = File.join(
22+
'app/resources',
23+
class_path,
24+
"#{file_name.singularize}_resource.rb"
25+
)
26+
template 'resource.rb.tt', template_file
27+
end
28+
29+
private
30+
31+
def base_resource
32+
options['base_resource']
33+
end
34+
end
35+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<% module_namespacing do -%>
2+
class <%= class_name.singularize %>Resource < <%= base_resource %>
3+
end
4+
<% end -%>

‎lib/generators/jsonapi/resource_generator.rb

-14
This file was deleted.

‎lib/generators/jsonapi/templates/jsonapi_controller.rb

-4
This file was deleted.

‎lib/generators/jsonapi/templates/jsonapi_resource.rb

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Rails.application.routes.draw do
2+
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
3+
end
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
require File.expand_path('../../../../test_helper', __FILE__)
2-
require 'generators/jsonapi/controller_generator'
2+
require 'generators/jsonapi/controller/controller_generator'
33

44
module Jsonapi
55
class ControllerGeneratorTest < Rails::Generators::TestCase
66
tests ControllerGenerator
7-
destination Rails.root.join('../controllers')
7+
destination File.expand_path('../tmp', __dir__ )
88
setup :prepare_destination
99
teardown :cleanup_destination_root
1010

1111
def cleanup_destination_root
1212
FileUtils.rm_rf destination_root
1313
end
1414

15+
def prepare_destination
16+
super
17+
FileUtils.cp_r File.expand_path('app_template/', __dir__ ) + '/.', destination_root
18+
end
19+
1520
test "controller is created" do
1621
run_generator ["post"]
1722
assert_file 'app/controllers/posts_controller.rb', /class PostsController < JSONAPI::ResourceController/
1823
end
1924

25+
test "base controller class is settable" do
26+
run_generator %w[post --base_controller BaseController]
27+
assert_file 'app/controllers/posts_controller.rb', /class PostsController < BaseController/
28+
end
29+
2030
test "controller is created with namespace" do
21-
run_generator ["api/v1/post"]
22-
assert_file 'app/controllers/api/v1/posts_controller.rb', /class Api::V1::PostsController < JSONAPI::ResourceController/
31+
run_generator ["api/post"]
32+
assert_file 'app/controllers/api/posts_controller.rb', /class Api::PostsController < JSONAPI::ResourceController/
33+
assert_file 'config/routes.rb', /^ namespace :api do\n jsonapi_resources :posts\n end$/
2334
end
2435
end
2536
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require File.expand_path('../../../../test_helper', __FILE__)
2+
require 'generators/jsonapi/processor/processor_generator'
3+
4+
module Jsonapi
5+
class ProcessorGeneratorTest < Rails::Generators::TestCase
6+
tests ProcessorGenerator
7+
destination File.expand_path('../tmp', __dir__ )
8+
setup :prepare_destination
9+
teardown :cleanup_destination_root
10+
11+
def cleanup_destination_root
12+
FileUtils.rm_rf destination_root
13+
end
14+
15+
test "processor is created" do
16+
run_generator %w[post]
17+
assert_file 'app/processors/post_processor.rb', /class PostProcessor < JSONAPI::Processor/
18+
end
19+
20+
test "base processor class is settable" do
21+
run_generator %w[post --base_processor BaseProcessor]
22+
assert_file 'app/processors/post_processor.rb', /class PostProcessor < BaseProcessor/
23+
end
24+
25+
test "processor is singular" do
26+
run_generator %w[posts]
27+
assert_file 'app/processors/post_processor.rb', /class PostProcessor < JSONAPI::Processor/
28+
end
29+
30+
test "processor is created with namespace" do
31+
run_generator %w[api/v1/post]
32+
assert_file 'app/processors/api/v1/post_processor.rb', /class Api::V1::PostProcessor < JSONAPI::Processor/
33+
end
34+
end
35+
end
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,55 @@
11
require File.expand_path('../../../../test_helper', __FILE__)
2-
require 'generators/jsonapi/resource_generator'
2+
require 'generators/jsonapi/resource/resource_generator'
33

44
module Jsonapi
55
class ResourceGeneratorTest < Rails::Generators::TestCase
66
tests ResourceGenerator
7-
destination Rails.root.join('../resources')
7+
destination File.expand_path('../tmp', __dir__ )
88
setup :prepare_destination
99
teardown :cleanup_destination_root
1010

1111
def cleanup_destination_root
1212
FileUtils.rm_rf destination_root
1313
end
1414

15+
def prepare_destination
16+
super
17+
FileUtils.cp_r File.expand_path('app_template/', __dir__ ) + '/.', destination_root
18+
end
19+
1520
test "resource is created" do
16-
run_generator ["post"]
21+
run_generator %w[post]
22+
assert_file 'app/resources/post_resource.rb', /class PostResource < JSONAPI::Resource/
23+
end
24+
25+
test "resource created with controller and processors" do
26+
run_generator %w[post --controller --processor]
1727
assert_file 'app/resources/post_resource.rb', /class PostResource < JSONAPI::Resource/
28+
assert_file 'app/controllers/posts_controller.rb', /class PostsController < JSONAPI::ResourceController/
29+
assert_file 'app/processors/post_processor.rb', /class PostProcessor < JSONAPI::Processor/
30+
end
31+
32+
test "base resource class is settable" do
33+
run_generator %w[post --base_resource BaseResource]
34+
assert_file 'app/resources/post_resource.rb', /class PostResource < BaseResource/
1835
end
1936

2037
test "resource is singular" do
21-
run_generator ["posts"]
38+
run_generator %w[posts]
2239
assert_file 'app/resources/post_resource.rb', /class PostResource < JSONAPI::Resource/
2340
end
2441

25-
test "resource is created with namespace" do
26-
run_generator ["api/v1/post"]
27-
assert_file 'app/resources/api/v1/post_resource.rb', /class Api::V1::PostResource < JSONAPI::Resource/
42+
test "namespaced resource is created" do
43+
run_generator %w[api/post]
44+
assert_file 'app/resources/api/post_resource.rb', /class Api::PostResource < JSONAPI::Resource/
45+
end
46+
47+
test "namespaced resource, controller and processor are created" do
48+
run_generator %w[api/post --controller --processor]
49+
assert_file 'app/resources/api/post_resource.rb', /class Api::PostResource < JSONAPI::Resource/
50+
assert_file 'app/controllers/api/posts_controller.rb', /class Api::PostsController < JSONAPI::ResourceController/
51+
assert_file 'app/processors/api/post_processor.rb', /class Api::PostProcessor < JSONAPI::Processor/
52+
assert_file 'config/routes.rb', /^ namespace :api do\n jsonapi_resources :posts\n end$/
2853
end
2954
end
3055
end

0 commit comments

Comments
 (0)
Please sign in to comment.