From 35fc840b41d1257fe3560af777c291a2852ac749 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Sat, 1 Feb 2025 14:20:21 +0300 Subject: [PATCH] Make api show/update/destroy way actions resourceful --- app/abilities/api_ability.rb | 4 ++-- app/controllers/api/ways_controller.rb | 12 ++++++------ config/routes.rb | 4 +--- test/controllers/api/ways_controller_test.rb | 4 ++-- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/app/abilities/api_ability.rb b/app/abilities/api_ability.rb index e3ea9e434..bca6b779b 100644 --- a/app/abilities/api_ability.rb +++ b/app/abilities/api_ability.rb @@ -38,8 +38,8 @@ class ApiAbility if user.terms_agreed? can [:create, :update, :upload, :close, :subscribe, :unsubscribe], Changeset if scope?(token, :write_api) can :create, ChangesetComment if scope?(token, :write_api) - can [:create, :update, :destroy], [Node] if scope?(token, :write_api) - can [:create, :update, :delete], [Way, Relation] if scope?(token, :write_api) + can [:create, :update, :destroy], [Node, Way] if scope?(token, :write_api) + can [:create, :update, :delete], [Relation] if scope?(token, :write_api) end if user.moderator? diff --git a/app/controllers/api/ways_controller.rb b/app/controllers/api/ways_controller.rb index 632fdb9a6..285ed4604 100644 --- a/app/controllers/api/ways_controller.rb +++ b/app/controllers/api/ways_controller.rb @@ -1,13 +1,13 @@ module Api class WaysController < ApiController - before_action :check_api_writable, :only => [:create, :update, :delete] - before_action :authorize, :only => [:create, :update, :delete] + before_action :check_api_writable, :only => [:create, :update, :destroy] + before_action :authorize, :only => [:create, :update, :destroy] authorize_resource - before_action :require_public_data, :only => [:create, :update, :delete] - before_action :set_request_formats, :except => [:create, :update, :delete] - before_action :check_rate_limit, :only => [:create, :update, :delete] + before_action :require_public_data, :only => [:create, :update, :destroy] + before_action :set_request_formats, :except => [:create, :update, :destroy] + before_action :check_rate_limit, :only => [:create, :update, :destroy] def index raise OSM::APIBadUserInput, "The parameter ways is required, and must be of the form ways=id[,id[,id...]]" unless params["ways"] @@ -60,7 +60,7 @@ module Api end # This is the API call to delete a way - def delete + def destroy way = Way.find(params[:id]) new_way = Way.from_xml(request.raw_post) diff --git a/config/routes.rb b/config/routes.rb index 0d6a51ad9..ff5c74b22 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -41,9 +41,6 @@ OpenStreetMap::Application.routes.draw do get "way/:id/relations" => "relations#relations_for_way", :as => :way_relations, :id => /\d+/ post "way/:id/:version/redact" => "old_ways#redact", :as => :way_version_redact, :version => /\d+/, :id => /\d+/ get "way/:id/:version" => "old_ways#show", :as => :api_old_way, :id => /\d+/, :version => /\d+/ - get "way/:id" => "ways#show", :as => :api_way, :id => /\d+/ - put "way/:id" => "ways#update", :id => /\d+/ - delete "way/:id" => "ways#delete", :id => /\d+/ get "relation/:id/relations" => "relations#relations_for_relation", :as => :relation_relations, :id => /\d+/ get "relation/:id/history" => "old_relations#history", :as => :api_relation_history, :id => /\d+/ @@ -61,6 +58,7 @@ OpenStreetMap::Application.routes.draw do put "node/create" => "nodes#create", :as => nil resources :ways, :only => [:index, :create] + resources :ways, :path => "way", :id => /\d+/, :only => [:show, :update, :destroy] put "way/create" => "ways#create", :as => nil resources :relations, :only => [:index, :create] diff --git a/test/controllers/api/ways_controller_test.rb b/test/controllers/api/ways_controller_test.rb index e81b49b81..e9b2b4601 100644 --- a/test/controllers/api/ways_controller_test.rb +++ b/test/controllers/api/ways_controller_test.rb @@ -39,7 +39,7 @@ module Api ) assert_routing( { :path => "/api/0.6/way/1", :method => :delete }, - { :controller => "api/ways", :action => "delete", :id => "1" } + { :controller => "api/ways", :action => "destroy", :id => "1" } ) assert_recognizes( @@ -284,7 +284,7 @@ module Api # Test deleting ways. # ------------------------------------- - def test_delete + def test_destroy private_user = create(:user, :data_public => false) private_open_changeset = create(:changeset, :user => private_user) private_closed_changeset = create(:changeset, :closed, :user => private_user) -- 2.39.5