File tree Expand file tree Collapse file tree
test/lib/generators/jsonapi Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1043,6 +1043,10 @@ end
10431043
10441044Of course you are free to extend this as needed and override action handlers or other methods.
10451045
1046+ A jsonapi-controller generator is avaliable
1047+ ```
1048+ rails generate jsonapi:controller contact
1049+ ```
10461050
10471051###### Context
10481052
Original file line number Diff line number Diff line change 11Description:
22 Generator for JSONAPI Resources
33
4- Example :
4+ Examples :
55 rails generate jsonapi:resource Post
66
77 This will create:
88 app/resources/post_resource.rb
9+
10+ rails generate jsonapi:controller Post
11+
12+ This will create:
13+ app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change 1+ module Jsonapi
2+ class ControllerGenerator < Rails ::Generators ::NamedBase
3+ source_root File . expand_path ( '../templates' , __FILE__ )
4+
5+ def create_resource
6+ template_file = File . join (
7+ 'app/controllers' ,
8+ class_path ,
9+ "#{ file_name . pluralize } _controller.rb"
10+ )
11+ template 'jsonapi_controller.rb' , template_file
12+ end
13+ end
14+ end
Original file line number Diff line number Diff line change 1+ <% module_namespacing do -%>
2+ class <%= class_name.pluralize %> Controller < JSONAPI ::ResourceController
3+ end
4+ <% end -%>
Original file line number Diff line number Diff line change 1+ require File . expand_path ( '../../../../test_helper' , __FILE__ )
2+ require 'generators/jsonapi/controller_generator'
3+
4+ module Jsonapi
5+ class ControllerGeneratorTest < Rails ::Generators ::TestCase
6+ tests ControllerGenerator
7+ destination Rails . root . join ( '../controllers' )
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 "controller is created" do
16+ run_generator [ "post" ]
17+ assert_file 'app/controllers/posts_controller.rb' , /class PostsController < JSONAPI::ResourceController/
18+ end
19+
20+ 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/
23+ end
24+ end
25+ end
You can’t perform that action at this time.
0 commit comments