Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Override resource generator to work with our structure better (#50)
- Loading branch information
1 parent
f5840ec
commit 9b450e7
Showing
8 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
lib/generators/rails/api_resource_route/api_resource_route_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'rails/generators/rails/resource_route/resource_route_generator' | ||
|
||
module Rails | ||
module Generators | ||
class ApiResourceRouteGenerator < ResourceRouteGenerator | ||
def add_resource_route | ||
return if options[:actions].present? | ||
route_config = regular_class_path.collect{ |namespace| "namespace :#{namespace} do " }.join(" ") | ||
route_config << "jsonapi_resources :#{file_name.pluralize}" | ||
route_config << " end" * regular_class_path.size | ||
route route_config | ||
end | ||
end | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
lib/generators/rails/jsonapi_resource/jsonapi_resource_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Rails | ||
module Generators | ||
class JsonapiResourceGenerator < ::Rails::Generators::NamedBase | ||
source_root File.expand_path('templates', __dir__) | ||
|
||
def create_resource | ||
template_file = File.join( | ||
'app/resources', | ||
class_path, | ||
"#{file_name.singularize}_resource.rb" | ||
) | ||
template 'resource.rb', template_file | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<% module_namespacing do -%> | ||
class <%= class_name.singularize %>Resource < JSONAPI::Resource | ||
end | ||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module Rails | ||
module Generators | ||
class PolicyGenerator < ::Rails::Generators::NamedBase | ||
source_root File.expand_path('templates', __dir__) | ||
|
||
def create_policy | ||
template_file = File.join( | ||
'app/policies', | ||
class_path, | ||
"#{file_name.singularize}_policy.rb" | ||
) | ||
template 'policy.rb', template_file | ||
end | ||
|
||
hook_for :test_framework | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<% module_namespacing do -%> | ||
class <%= class_name %>Policy < ApplicationPolicy | ||
end | ||
<% end -%> |
22 changes: 22 additions & 0 deletions
22
lib/generators/rails/resource_controller/resource_controller_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'generators/jsonapi/controller_generator' | ||
|
||
module Rails | ||
module Generators | ||
class ResourceControllerGenerator < ::Rails::Generators::NamedBase | ||
source_root File.expand_path('../templates', __FILE__) | ||
check_class_collision suffix: 'Controller' | ||
|
||
def create_controller | ||
template_file = File.join( | ||
'app/controllers', | ||
class_path, | ||
"#{file_name.pluralize}_controller.rb" | ||
) | ||
template 'jsonapi_controller.rb', template_file | ||
end | ||
|
||
hook_for :authorization | ||
hook_for :serialization | ||
end | ||
end | ||
end |
4 changes: 4 additions & 0 deletions
4
lib/generators/rails/resource_controller/templates/jsonapi_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<% module_namespacing do -%> | ||
class <%= class_name.pluralize %>Controller < ApplicationController | ||
end | ||
<% end -%> |