Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,10 @@ end

Of course you are free to extend this as needed and override action handlers or other methods.

A jsonapi-controller generator is avaliable
```
rails generate jsonapi:controller contact
```

###### Context

Expand Down
7 changes: 6 additions & 1 deletion lib/generators/jsonapi/USAGE
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
Description:
Generator for JSONAPI Resources

Example:
Examples:
rails generate jsonapi:resource Post

This will create:
app/resources/post_resource.rb

rails generate jsonapi:controller Post

This will create:
app/controllers/posts_controller.rb
14 changes: 14 additions & 0 deletions lib/generators/jsonapi/controller_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Jsonapi
class ControllerGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)

def create_resource
template_file = File.join(
'app/controllers',
class_path,
"#{file_name.pluralize}_controller.rb"
)
template 'jsonapi_controller.rb', template_file
end
end
end
4 changes: 4 additions & 0 deletions lib/generators/jsonapi/templates/jsonapi_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<% module_namespacing do -%>
class <%= class_name.pluralize %>Controller < JSONAPI::ResourceController
end
<% end -%>
25 changes: 25 additions & 0 deletions test/lib/generators/jsonapi/controller_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require File.expand_path('../../../../test_helper', __FILE__)
require 'generators/jsonapi/controller_generator'

module Jsonapi
class ControllerGeneratorTest < Rails::Generators::TestCase
tests ControllerGenerator
destination Rails.root.join('../controllers')
setup :prepare_destination
teardown :cleanup_destination_root

def cleanup_destination_root
FileUtils.rm_rf destination_root
end

test "controller is created" do
run_generator ["post"]
assert_file 'app/controllers/posts_controller.rb', /class PostsController < JSONAPI::ResourceController/
end

test "controller is created with namespace" do
run_generator ["api/v1/post"]
assert_file 'app/controllers/api/v1/posts_controller.rb', /class Api::V1::PostsController < JSONAPI::ResourceController/
end
end
end