Skip to content

Commit c9cc807

Browse files
committed
Merge pull request #578 from kevintraver/master
Add jsonapi:controllers generator
2 parents 49a7387 + b12b6b7 commit c9cc807

5 files changed

Lines changed: 53 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,10 @@ end
10431043

10441044
Of 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

lib/generators/jsonapi/USAGE

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
Description:
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
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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
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 < JSONAPI::ResourceController
3+
end
4+
<% end -%>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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

0 commit comments

Comments
 (0)