From 90d90fad93c13670207c84b253166b1204b1a21d Mon Sep 17 00:00:00 2001 From: Larry Gebhardt Date: Thu, 18 Jun 2015 10:21:03 -0400 Subject: [PATCH] Exclude edit and new routes --- README.md | 12 ------------ lib/jsonapi/routing_ext.rb | 14 ++++++++++++++ test/test_helper.rb | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index b205e91f7..d77642732 100644 --- a/README.md +++ b/README.md @@ -890,8 +890,6 @@ contact_links_phone_numbers GET /contacts/:contact_id/links/phone-numbers( contact_phone_numbers GET /contacts/:contact_id/phone-numbers(.:format) phone_numbers#get_related_resources {:association=>"phone_numbers", :source=>"contacts"} contacts GET /contacts(.:format) contacts#index POST /contacts(.:format) contacts#create - new_contact GET /contacts/new(.:format) contacts#new - edit_contact GET /contacts/:id/edit(.:format) contacts#edit contact GET /contacts/:id(.:format) contacts#show PATCH /contacts/:id(.:format) contacts#update PUT /contacts/:id(.:format) contacts#update @@ -902,8 +900,6 @@ contact_links_phone_numbers GET /contacts/:contact_id/links/phone-numbers( phone_number_contact GET /phone-numbers/:phone_number_id/contact(.:format) contacts#get_related_resource {:association=>"contact", :source=>"phone_numbers"} phone_numbers GET /phone-numbers(.:format) phone_numbers#index POST /phone-numbers(.:format) phone_numbers#create - new_phone_number GET /phone-numbers/new(.:format) phone_numbers#new - edit_phone_number GET /phone-numbers/:id/edit(.:format) phone_numbers#edit phone_number GET /phone-numbers/:id(.:format) phone_numbers#show PATCH /phone-numbers/:id(.:format) phone_numbers#update PUT /phone-numbers/:id(.:format) phone_numbers#update @@ -933,8 +929,6 @@ gives routes that are only related to the primary resource, and none for its rel Prefix Verb URI Pattern Controller#Action contacts GET /contacts(.:format) contacts#index POST /contacts(.:format) contacts#create - new_contact GET /contacts/new(.:format) contacts#new -edit_contact GET /contacts/:id/edit(.:format) contacts#edit contact GET /contacts/:id(.:format) contacts#show PATCH /contacts/:id(.:format) contacts#update PUT /contacts/:id(.:format) contacts#update @@ -973,8 +967,6 @@ contact_links_phone_numbers GET /contacts/:contact_id/links/phone-numbers(.:f DELETE /contacts/:contact_id/links/phone-numbers/:keys(.:format) contacts#destroy_association {:association=>"phone_numbers"} contacts GET /contacts(.:format) contacts#index POST /contacts(.:format) contacts#create - new_contact GET /contacts/new(.:format) contacts#new - edit_contact GET /contacts/:id/edit(.:format) contacts#edit contact GET /contacts/:id(.:format) contacts#show PATCH /contacts/:id(.:format) contacts#update PUT /contacts/:id(.:format) contacts#update @@ -1004,8 +996,6 @@ gives the following routes: contact_phone_numbers GET /contacts/:contact_id/phone-numbers(.:format) phone_numbers#get_related_resources {:association=>"phone_numbers", :source=>"contacts"} contacts GET /contacts(.:format) contacts#index POST /contacts(.:format) contacts#create - new_contact GET /contacts/new(.:format) contacts#new - edit_contact GET /contacts/:id/edit(.:format) contacts#edit contact GET /contacts/:id(.:format) contacts#show PATCH /contacts/:id(.:format) contacts#update PUT /contacts/:id(.:format) contacts#update @@ -1034,8 +1024,6 @@ gives the following routes: phone_number_contact GET /phone-numbers/:phone_number_id/contact(.:format) contacts#get_related_resource {:association=>"contact", :source=>"phone_numbers"} phone_numbers GET /phone-numbers(.:format) phone_numbers#index POST /phone-numbers(.:format) phone_numbers#create - new_phone_number GET /phone-numbers/new(.:format) phone_numbers#new - edit_phone_number GET /phone-numbers/:id/edit(.:format) phone_numbers#edit phone_number GET /phone-numbers/:id(.:format) phone_numbers#show PATCH /phone-numbers/:id(.:format) phone_numbers#update PUT /phone-numbers/:id(.:format) phone_numbers#update diff --git a/lib/jsonapi/routing_ext.rb b/lib/jsonapi/routing_ext.rb index 46d358556..5cd068814 100644 --- a/lib/jsonapi/routing_ext.rb +++ b/lib/jsonapi/routing_ext.rb @@ -26,6 +26,13 @@ def jsonapi_resource(*resources, &block) options.merge!(res.routing_resource_options) options[:path] = format_route(@resource_type) + if options[:except] + options[:except] << :new unless options[:except].include?(:new) || options[:except].include?('new') + options[:except] << :edit unless options[:except].include?(:edit) || options[:except].include?('edit') + else + options[:except] = [:new, :edit] + end + resource @resource_type, options do @scope[:jsonapi_resource] = @resource_type @@ -62,6 +69,13 @@ def jsonapi_resources(*resources, &block) options[:path] = format_route(@resource_type) + if options[:except] + options[:except] << :new unless options[:except].include?(:new) || options[:except].include?('new') + options[:except] << :edit unless options[:except].include?(:edit) || options[:except].include?('edit') + else + options[:except] = [:new, :edit] + end + resources @resource_type, options do @scope[:jsonapi_resource] = @resource_type diff --git a/test/test_helper.rb b/test/test_helper.rb index d9dfec1bd..f273ed9a3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -113,7 +113,7 @@ def count_queries(&block) jsonapi_link :author, except: [:destroy] end - jsonapi_resource :preferences + jsonapi_resource :preferences, except: [:create, :destroy] jsonapi_resources :books jsonapi_resources :book_comments