Skip to content

Add api_version option #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/tmp/
.byebug_history
spec/dummy/log/*
*~
27 changes: 18 additions & 9 deletions lib/generators/jsonapi/resource_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class ResourceGenerator < ::Rails::Generators::NamedBase
default: nil,
aliases: ['--actions', '-a'],
desc: 'Array of controller actions to support, e.g. "index show destroy"'
class_option :api_version,
type: :string,
default: 'v1',
desc: 'Version for the api'


desc "This generator creates a resource file at app/resources, as well as corresponding controller/specs/route/etc"
def copy_resource_file
Expand Down Expand Up @@ -50,6 +55,10 @@ def omit_comments?
@options['omit-comments']
end

def api_version
@options['api_version']
end

def generate_controller
to = File.join('app/controllers', class_path, "#{file_name.pluralize}_controller.rb")
template('controller.rb.erb', to)
Expand All @@ -74,7 +83,7 @@ def docs_controller?
end

def generate_swagger
code = " jsonapi_resource '/v1/#{type}'"
code = " jsonapi_resource '/#{api_version}/#{type}'"
code << ", only: [#{actions.map { |a| ":#{a}" }.join(', ')}]" if actions.length < 5
code << "\n"
inject_into_file 'app/controllers/docs_controller.rb', before: /^end/ do
Expand Down Expand Up @@ -106,42 +115,42 @@ def generate_route
code = " resources :#{type}"
code << ", only: [#{actions.map { |a| ":#{a}" }.join(', ')}]" if actions.length < 5
code << "\n"
inject_into_file 'config/routes.rb', after: "scope path: '/v1' do\n" do
inject_into_file 'config/routes.rb', after: "scope path: '/#{api_version}' do\n" do
code
end
end

def generate_tests
if actions?('index')
to = File.join "spec/api/v1/#{file_name.pluralize}",
to = File.join "spec/api/#{api_version}/#{file_name.pluralize}",
class_path,
"index_spec.rb"
template('index_request_spec.rb.erb', to)
end

if actions?('show')
to = File.join "spec/api/v1/#{file_name.pluralize}",
to = File.join "spec/api/#{api_version}/#{file_name.pluralize}",
class_path,
"show_spec.rb"
template('show_request_spec.rb.erb', to)
end

if actions?('create')
to = File.join "spec/api/v1/#{file_name.pluralize}",
to = File.join "spec/api/#{api_version}/#{file_name.pluralize}",
class_path,
"create_spec.rb"
template('create_request_spec.rb.erb', to)
end

if actions?('update')
to = File.join "spec/api/v1/#{file_name.pluralize}",
to = File.join "spec/api/#{api_version}/#{file_name.pluralize}",
class_path,
"update_spec.rb"
template('update_request_spec.rb.erb', to)
end

if actions?('destroy')
to = File.join "spec/api/v1/#{file_name.pluralize}",
to = File.join "spec/api/#{api_version}/#{file_name.pluralize}",
class_path,
"destroy_spec.rb"
template('destroy_request_spec.rb.erb', to)
Expand Down Expand Up @@ -178,7 +187,7 @@ def api_namespace
if ns.blank?
ns = prompt \
header: "What is your API namespace?",
description: "This will be used as a route prefix, e.g. if you want the route '/books_api/v1/authors' your namespace would be 'books_api'",
description: "This will be used as a route prefix, e.g. if you want the route '/books_api/#{api_version}/authors' your namespace would be 'books_api'",
default: 'api'
update_config!('namespace' => ns)
end
Expand All @@ -192,7 +201,7 @@ def model_klass
end

def type
model_klass.name.underscore.pluralize
class_name.underscore.pluralize
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require 'rails_helper'

RSpec.describe "<%= type %>#create", type: :request do
subject(:make_request) do
jsonapi_post "/<%= api_namespace %>/v1/<%= type %>", payload
jsonapi_post "/<%= api_namespace %>/<%= api_version %>/<%= type %>", payload
end

describe 'basic create' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require 'rails_helper'

RSpec.describe "<%= type %>#destroy", type: :request do
subject(:make_request) do
jsonapi_delete "/<%= api_namespace %>/v1/<%= type %>/#{<%= file_name %>.id}"
jsonapi_delete "/<%= api_namespace %>/<%= api_version %>/<%= type %>/#{<%= file_name %>.id}"
end

describe 'basic destroy' do
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/jsonapi/templates/index_request_spec.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RSpec.describe "<%= file_name.pluralize %>#index", type: :request do
let(:params) { {} }

subject(:make_request) do
jsonapi_get "/<%= api_namespace %>/v1/<%= file_name.pluralize %>",
jsonapi_get "/<%= api_namespace %>/<%= api_version %>/<%= file_name.pluralize %>",
params: params
end

Expand Down
2 changes: 1 addition & 1 deletion lib/generators/jsonapi/templates/show_request_spec.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RSpec.describe "<%= file_name.pluralize %>#show", type: :request do
let(:params) { {} }

subject(:make_request) do
jsonapi_get "/<%= api_namespace %>/v1/<%= file_name.pluralize %>/#{<%= file_name %>.id}",
jsonapi_get "/<%= api_namespace %>/<%= api_version %>/<%= file_name.pluralize %>/#{<%= file_name %>.id}",
params: params
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require 'rails_helper'

RSpec.describe "<%= type %>#update", type: :request do
subject(:make_request) do
jsonapi_put "/<%= api_namespace %>/v1/<%= type %>/#{<%= file_name %>.id}", payload
jsonapi_put "/<%= api_namespace %>/<%= api_version %>/<%= type %>/#{<%= file_name %>.id}", payload
end

describe 'basic update' do
Expand Down