simplify the state/city/name routes

This commit is contained in:
Eric 2020-05-19 16:12:13 -05:00
parent 8c9dc3af37
commit d5c788f07b
3 changed files with 60 additions and 16 deletions

View file

@ -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]
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]
# 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 ':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'
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 ':state_code/:city/:name/dashboard' => 'nonprofits#dashboard', as: :np_dashboard, via: %i[get post]
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

13
config/state_code_constraint.rb Executable file
View file

@ -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

View file

@ -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