Skip to content

Commit

Permalink
Root to buoys if locked
Browse files Browse the repository at this point in the history
  • Loading branch information
swrobel committed Jan 9, 2024
1 parent 5d19ce4 commit d49308c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
12 changes: 11 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
# 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
end

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
Expand Down
17 changes: 9 additions & 8 deletions app/views/shared/_nav.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d49308c

Please sign in to comment.