Class: Studio::GeoSettingsController

Inherits:
ApplicationController
  • Object
show all
Includes:
GeoDetection
Defined in:
app/controllers/studio/geo_settings_controller.rb

Overview

/admin/geo — the shared geo manager, plus /geo/check, the probe that reports what the server thinks of THIS visitor.

A plain host-inherited controller whose view is a bare content wrapper, like /admin/emails and /admin/style, so it renders inside each host's application layout and picks up that app's navbar and theme.

Three things an operator can do here, and each exists because doing it any other way means a deploy:

1. turn the gate on or off — the kill switch;
2. edit the blocked list — countries, and subdivisions of the home country;
3. SIMULATE a blocked location, so the blocked experience can be walked
 from a desk without a VPN.

Routes are opt-in (Studio.draw_geo_routes) because turf-monster owns these exact helper names until its adoption lands. See Studio.routes.

Constant Summary collapse

TABS =
%w[states countries].freeze

Instance Method Summary collapse

Methods included from GeoDetection

#detect_geo_state, #geo_blocked?, #geo_country, #geo_override_active?, #geo_region_token, #geo_state, #require_geo_allowed, #reset_geo_detection!

Instance Method Details

#checkObject

GET /geo/check — force a fresh detection and report the verdict.

Forcing is the point: an operator moving between networks needs an answer about where they are NOW, not the one cached for the next day. A simulated location is left alone, or the probe would silently cancel the simulation it is supposed to describe.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/studio/geo_settings_controller.rb', line 40

def check
  unless geo_override_active?
    reset_geo_detection!
    detect_geo_state
  end

  render json: {
    country: geo_country,
    subdivision: geo_state,
    region: geo_region_token,
    simulated: geo_override_active?,
    blocked: geo_blocked?,
    # The legacy key turf-monster's client already reads. Kept so an app can
    # adopt this endpoint without shipping a JavaScript change on the same day.
    state: geo_state
  }
end

#editObject



60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/studio/geo_settings_controller.rb', line 60

def edit
  @geo_setting = Studio::GeoSetting.current
  # Which editor is open. It rides in the URL so a save can send the operator
  # back to the one they were working in rather than resetting to states.
  @tab = TABS.include?(params[:tab]) ? params[:tab] : "states"
  # Where the simulate button would put you. The page NAMES it — "Simulate
  # WA" beats "Simulate a blocked location", because the operator is about to
  # be moved and should read where to.
  @simulated_region = simulated_region
end

#toggle_overrideObject

POST /admin/geo/toggle — stand in a blocked location, or stop.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/studio/geo_settings_controller.rb', line 85

def toggle_override
  if geo_override_active?
    session.delete(:geo_override)
    return redirect_back fallback_location: admin_geo_path, notice: "Geo simulation cleared."
  end

  region = simulated_region
  # Nothing is blocked yet, so there is no blocked experience to walk. Say
  # that plainly instead of pinning the operator to a location that behaves
  # exactly like the one they are already in.
  if region.nil?
    return redirect_back fallback_location: admin_geo_path,
                         alert: "Nothing is blocked yet — add a country or a state first."
  end

  session[:geo_override] = region
  redirect_back fallback_location: admin_geo_path, notice: "Simulating #{place_name(region)}."
end

#updateObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/studio/geo_settings_controller.rb', line 71

def update
  @geo_setting = Studio::GeoSetting.current

  @geo_setting.enabled = ActiveModel::Type::Boolean.new.cast(params.dig(:geo_setting, :enabled))
  @geo_setting.banned_subdivisions = 
  @geo_setting.banned_countries = 
  @geo_setting.save!

  redirect_to admin_geo_path(tab: ), notice: "Geo settings updated."
rescue StandardError => e
  redirect_to admin_geo_path(tab: ), alert: "Failed to update: #{e.message}"
end