diff --git a/.gitignore b/.gitignore index 9e752c1..6c21cf1 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ /tmp/ .byebug_history spec/dummy/log/* +*~ diff --git a/lib/generators/jsonapi/resource_generator.rb b/lib/generators/jsonapi/resource_generator.rb index 4b21df6..5fb3bdd 100644 --- a/lib/generators/jsonapi/resource_generator.rb +++ b/lib/generators/jsonapi/resource_generator.rb @@ -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 @@ -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) @@ -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 @@ -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) @@ -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 @@ -192,7 +201,7 @@ def model_klass end def type - model_klass.name.underscore.pluralize + class_name.underscore.pluralize end end end diff --git a/lib/generators/jsonapi/templates/create_request_spec.rb.erb b/lib/generators/jsonapi/templates/create_request_spec.rb.erb index c1c1556..792e0f6 100644 --- a/lib/generators/jsonapi/templates/create_request_spec.rb.erb +++ b/lib/generators/jsonapi/templates/create_request_spec.rb.erb @@ -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 diff --git a/lib/generators/jsonapi/templates/destroy_request_spec.rb.erb b/lib/generators/jsonapi/templates/destroy_request_spec.rb.erb index 0d375a1..87e784c 100644 --- a/lib/generators/jsonapi/templates/destroy_request_spec.rb.erb +++ b/lib/generators/jsonapi/templates/destroy_request_spec.rb.erb @@ -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 diff --git a/lib/generators/jsonapi/templates/index_request_spec.rb.erb b/lib/generators/jsonapi/templates/index_request_spec.rb.erb index a9303e2..87ebdc5 100644 --- a/lib/generators/jsonapi/templates/index_request_spec.rb.erb +++ b/lib/generators/jsonapi/templates/index_request_spec.rb.erb @@ -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 diff --git a/lib/generators/jsonapi/templates/show_request_spec.rb.erb b/lib/generators/jsonapi/templates/show_request_spec.rb.erb index f1e089c..468ca04 100644 --- a/lib/generators/jsonapi/templates/show_request_spec.rb.erb +++ b/lib/generators/jsonapi/templates/show_request_spec.rb.erb @@ -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 diff --git a/lib/generators/jsonapi/templates/update_request_spec.rb.erb b/lib/generators/jsonapi/templates/update_request_spec.rb.erb index 846f546..beb4d17 100644 --- a/lib/generators/jsonapi/templates/update_request_spec.rb.erb +++ b/lib/generators/jsonapi/templates/update_request_spec.rb.erb @@ -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