From d49308cb3e5821ea6896152d65a3dfebcce1a3cf Mon Sep 17 00:00:00 2001 From: Stefan Wrobel Date: Tue, 9 Jan 2024 13:02:26 -0800 Subject: [PATCH] Root to buoys if locked --- app/controllers/application_controller.rb | 12 +++++++++++- app/views/shared/_nav.slim | 17 +++++++++-------- config/routes.rb | 2 +- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a45649c..47376b5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,10 +1,16 @@ # frozen_string_literal: true class ApplicationController < ActionController::Base + helper_method :locked? + # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + def root + redirect_to "/southern-california/#{locked? ? 'buoys' : 'los-angeles'}" + end + def unlock cookies[:unlock] = { value: Rails.application.credentials.unlock_secret, expires: 5.years.from_now } redirect_to :root @@ -12,8 +18,12 @@ def unlock private + def locked? + ENV.fetch('UNLOCK_KEY', nil) && cookies[:unlock] != Rails.application.credentials.unlock_secret! + end + def check_unlocked - return unless ENV.fetch('UNLOCK_KEY', nil) && cookies[:unlock] != Rails.application.credentials.unlock_secret! + return unless locked? raise ActionController::RoutingError, 'Not Found' end diff --git a/app/views/shared/_nav.slim b/app/views/shared/_nav.slim index a54a81e..c087d0a 100644 --- a/app/views/shared/_nav.slim +++ b/app/views/shared/_nav.slim @@ -2,11 +2,12 @@ nav.navbar.navbar-light.fixed-top .row.font-weight-light.d-flex.align-items-center.pl-4px.safe-padding select onchange='window.location = this.value' - Region.optimized.ordered.each do |region| - optgroup label=region.name - - if region.buoys.any? - - selected = region == @region && !@subregion - - name = "Buoys" - - name = "#{region.name} #{name}" if selected - option value=buoys_path(region) selected=('selected' if selected) =name - - region.subregions.each do |subregion| - option value=subregion_path(region, subregion) selected=('selected' if subregion == @subregion) =subregion.name + - if !locked? || region.buoys.any? + optgroup label=region.name + - if region.buoys.any? + - selected = region == @region && !@subregion + - name = "Buoys" + - name = "#{region.name} #{name}" if selected + option value=buoys_path(region) selected=('selected' if selected) =name + - locked? || region.subregions.each do |subregion| + option value=subregion_path(region, subregion) selected=('selected' if subregion == @subregion) =subregion.name diff --git a/config/routes.rb b/config/routes.rb index e9131b5..1a29314 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,5 +15,5 @@ def self.matches?(request) get '/:region_id/:subregion_id/:spot_id', to: 'spots#show', as: 'spot' end - root to: redirect('/southern-california/los-angeles') + root to: 'application#root' end