From d5c788f07b77d724c6359e284c89da5af4c24003 Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 19 May 2020 16:12:13 -0500 Subject: [PATCH] simplify the state/city/name routes --- config/routes.rb | 39 +++++++++++++++---------- config/state_code_constraint.rb | 13 +++++++++ spec/routing/state_code_routing_spec.rb | 24 +++++++++++++++ 3 files changed, 60 insertions(+), 16 deletions(-) create mode 100755 config/state_code_constraint.rb create mode 100644 spec/routing/state_code_routing_spec.rb diff --git a/config/routes.rb b/config/routes.rb index 86effd64..6e1f8e6d 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ # License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later Rails.application.routes.draw do + require_relative "./state_code_constraint" if Rails.env == 'development' get '/button_debug/embedded' => 'button_debug#embedded' get '/button_debug/button' => 'button_debug#button' @@ -217,26 +218,32 @@ Rails.application.routes.draw do match '/events' => 'events#index', via: [:get] match '/events/:event_slug' => 'events#show', via: %i[get post] - # Nonprofits - match ':state_code/:city/:name' => 'nonprofits#show', :as => :nonprofit_location, via: %i[get post] - match ':state_code/:city/:name/donate' => 'nonprofits#donate', :as => :nonprofit_donation, via: %i[get post] - match ':state_code/:city/:name/button' => 'nonprofits/button#guided', via: %i[get post] - # Campaigns - match ':state_code/:city/:name/campaigns' => 'campaigns#index', via: %i[get post] - match ':state_code/:city/:name/campaigns/:campaign_slug' => 'campaigns#show', via: %i[get post] - match ':state_code/:city/:name/campaigns/:campaign_slug/supporters' => 'campaigns/supporters#index', via: %i[get post] match '/peer-to-peer' => 'campaigns#peer_to_peer', via: %i[get post] - # Events - match ':state_code/:city/:name/events' => 'events#index', via: %i[get post] - match ':state_code/:city/:name/events/:event_slug' => 'events#show', via: %i[get post] - match ':state_code/:city/:name/events/:event_slug/stats' => 'events#stats', via: %i[get post] - match ':state_code/:city/:name/events/:event_slug/tickets' => 'tickets#index', via: %i[get post] - # get '/events' => 'events#index' + scope ':state_code/:city/:name' do + constraints StateCodeConstraint.new do + # Nonprofits + match '' => 'nonprofits#show', :as => :nonprofit_location, via: %i[get post] + match 'donate' => 'nonprofits#donate', :as => :nonprofit_donation, via: %i[get post] + match 'button' => 'nonprofits/button#guided', via: %i[get post] - # Dashboard - match ':state_code/:city/:name/dashboard' => 'nonprofits#dashboard', as: :np_dashboard, via: %i[get post] + # Campaigns + match 'campaigns' => 'campaigns#index', via: %i[get post] + match 'campaigns/:campaign_slug' => 'campaigns#show', via: %i[get post] + match 'campaigns/:campaign_slug/supporters' => 'campaigns/supporters#index', via: %i[get post] + + + # Events + match 'events' => 'events#index', via: %i[get post] + match 'events/:event_slug' => 'events#show', via: %i[get post] + match 'events/:event_slug/stats' => 'events#stats', via: %i[get post] + match 'events/:event_slug/tickets' => 'tickets#index', via: %i[get post] + + # Dashboard + match 'dashboard' => 'nonprofits#dashboard', as: :np_dashboard, via: %i[get post] + end + end # Misc get '/pages/wp-plugin', to: redirect('/help/wordpress-plugin') # temporary, until WP plugin updated diff --git a/config/state_code_constraint.rb b/config/state_code_constraint.rb new file mode 100755 index 00000000..43db6dba --- /dev/null +++ b/config/state_code_constraint.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +class StateCodeConstraint + US_STATES = %w(AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN + IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ + NM NY NC ND OH OK OR PA PR RI SC SD TN TX UT VT VA WA WV WI WY) + US_STATES_REGEX = Regexp.union(*US_STATES.map{|i| /^#{i.downcase}$/}) + + def matches?(request) + US_STATES_REGEX =~ request.params[:state_code] + end +end \ No newline at end of file diff --git a/spec/routing/state_code_routing_spec.rb b/spec/routing/state_code_routing_spec.rb new file mode 100644 index 00000000..e4336d6a --- /dev/null +++ b/spec/routing/state_code_routing_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require "rails_helper" + +describe 'Routing by state code', type: :routing do + it 'routes by state-codes for lower case' do + expect(post: "/wi/appleton/name").to route_to( + controller: 'nonprofits', + action: "show", + state_code: "wi", + city: "appleton", + name: "name" + ) + end + + it 'fails to route for an incorrect two letter state' do + expect(post: "/us/appleton/name").to_not be_routable + end + + it 'it fails to route if a valid state code is in the middle of the state_code' do + expect(get: '/hoho/cleveland/interesting-name').to_not be_routable + end +end \ No newline at end of file