-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
178 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
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,63 @@ | ||
require 'rails' | ||
|
||
module AuxiliaryRails | ||
class ApiResourceGenerator < ::Rails::Generators::NamedBase | ||
desc 'Stubs out a new API resource including an empty entity and spec.' | ||
|
||
class_option :api_module, | ||
type: :string, | ||
default: 'app', | ||
desc: 'API module name' | ||
class_option :api_version, | ||
type: :numeric, | ||
default: 1, | ||
desc: 'API version' | ||
class_option :skip_helper, | ||
type: :boolean, | ||
default: true, | ||
desc: 'Indicates if helper generation needs to be skipped' | ||
source_root File.expand_path('templates/apis', __dir__) | ||
|
||
def create_api_resource_file | ||
template 'api_resource_template.rb.erb', | ||
"app/#{api_module_path}/resources/#{plural_file_name}_resource.rb" | ||
end | ||
|
||
def create_api_entity_file | ||
template 'api_entity_template.rb.erb', | ||
"app/#{api_module_path}/entities/#{file_name}_entity.rb" | ||
end | ||
|
||
def create_api_helper_file | ||
return if options[:skip_helper] | ||
|
||
template 'api_helper_template.rb.erb', | ||
"app/#{api_module_path}/helpers/#{plural_file_name}_api_helper.rb" | ||
end | ||
|
||
def create_api_resource_spec_file | ||
template 'api_resource_spec_template.rb.erb', | ||
"spec/#{api_module_path}/#{plural_file_name}_resource_spec.rb" | ||
end | ||
|
||
private | ||
|
||
def api_module_name | ||
"#{options[:api_module].camelize}V#{options[:api_version]}API" | ||
end | ||
|
||
def api_module_path | ||
"apis/#{options[:api_module]}_v#{options[:api_version]}_api" | ||
end | ||
|
||
def api_url_path | ||
api_name = 'api' | ||
api_name += "-#{options[:api_module]}" if options[:api_module] != 'app' | ||
"/#{api_name}/v#{options[:api_version]}/#{plural_name}" | ||
end | ||
|
||
def entity_class_name | ||
"#{api_module_name}::Entities::#{class_name}Entity" | ||
end | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
lib/generators/auxiliary_rails/templates/apis/api_entity_template.rb.erb
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,7 @@ | ||
module <%= api_module_name %>::Entities | ||
class <%= class_name %>Entity < Grape::Entity | ||
expose :id | ||
# TODO: define `<%= class_name %>Entity` exposes | ||
expose :created_at, :updated_at | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
lib/generators/auxiliary_rails/templates/apis/api_helper_template.rb.erb
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,5 @@ | ||
module <%= api_module_name %>::Helpers | ||
module <%= plural_name.camelize %>APIHelper | ||
# TODO: define methods for `<%= plural_name.camelize %>Helper` | ||
end | ||
end |
32 changes: 32 additions & 0 deletions
32
lib/generators/auxiliary_rails/templates/apis/api_resource_spec_template.rb.erb
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,32 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe <%= api_module_name %>::Resources::<%= plural_name.camelize %>Resource, type: :request do | ||
describe 'GET <%= api_url_path %>' do | ||
subject { get '<%= api_url_path %>' } | ||
|
||
# TODO: write some tests | ||
skip | ||
end | ||
|
||
describe 'POST <%= api_url_path %>' do | ||
subject { post '<%= api_url_path %>' } | ||
|
||
# TODO: write some tests | ||
skip | ||
end | ||
|
||
describe 'GET <%= api_url_path %>/:id' do | ||
# TODO: write some tests | ||
skip | ||
end | ||
|
||
describe 'PUT <%= api_url_path %>/:id' do | ||
# TODO: write some tests | ||
skip | ||
end | ||
|
||
describe 'DELETE <%= api_url_path %>/:id' do | ||
# TODO: write some tests | ||
skip | ||
end | ||
end |
70 changes: 70 additions & 0 deletions
70
lib/generators/auxiliary_rails/templates/apis/api_resource_template.rb.erb
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,70 @@ | ||
module <%= api_module_name %>::Resources | ||
class <%= plural_name.camelize %>Resource < Grape::API | ||
helpers do | ||
def collection | ||
# TODO: add `policy_scope`? | ||
@<%= plural_name %> ||= <%= class_name %>.all | ||
end | ||
|
||
def resource | ||
@<%= singular_name %> ||= <%= class_name %>.find(params[:id]) | ||
end | ||
end | ||
|
||
resource :<%= plural_name %> do | ||
desc 'Returns resource collection' | ||
get do | ||
# TODO: authorize resource, :index? | ||
present collection, | ||
with: <%= entity_class_name %> | ||
end | ||
|
||
desc 'Creates a <%= class_name %>' | ||
params do | ||
requires :<%= singular_name %>, type: Hash do | ||
# TODO: define resource params or remove `params` block | ||
end | ||
end | ||
post do | ||
@<%= singular_name %> = <%= class_name %>.new(declared(params)[:<%= singular_name %>]) | ||
# TODO: authorize resource, :create? | ||
resource.save! | ||
present resource, | ||
with: <%= entity_class_name %> | ||
end | ||
|
||
params do | ||
requires :id, type: Integer, desc: '<%= class_name %> ID' | ||
end | ||
route_param :id do | ||
desc 'Returns a <%= class_name %>' | ||
get do | ||
# TODO: authorize resource, :show? | ||
present resource, | ||
with: <%= entity_class_name %> | ||
end | ||
|
||
desc 'Updates a <%= class_name %>' | ||
params do | ||
requires :<%= singular_name %>, type: Hash do | ||
# TODO: define resource params or remove `params` block | ||
end | ||
end | ||
put do | ||
resource.assign_attributes(declared(params)[:<%= singular_name %>]) | ||
# TODO: authorize resource, :update? | ||
resource.save! | ||
present resource, | ||
with: <%= entity_class_name %> | ||
end | ||
|
||
desc 'Deletes a <%= class_name %>' | ||
delete do | ||
# TODO: authorize resource, :destroy? | ||
resource.destroy! | ||
body false | ||
end | ||
end | ||
end | ||
end | ||
end |