Class: Organizations::SwitchController

Inherits:
ApplicationController show all
Defined in:
app/controllers/organizations/switch_controller.rb

Overview

Controller for switching between organizations. Handles the POST /organizations/switch/:id route.

Examples:

Using the switch route

POST /organizations/switch/123
# Redirects to root_path with new current_organization set

Instance Method Summary collapse

Methods included from ControllerHelpers

#accept_pending_organization_invitation!, #clear_pending_organization_invitation!, #create_organization_and_switch!, #current_membership, #current_organization, #current_organization=, #handle_pending_invitation_acceptance_for, #no_organization_redirect_path, #organization_signed_in?, #pending_invitation_acceptance_redirect_path_for, #pending_organization_invitation, #pending_organization_invitation?, #pending_organization_invitation_email, #pending_organization_invitation_token, #redirect_path_after_invitation_accepted, #redirect_path_after_organization_switched, #redirect_path_when_invitation_requires_authentication, #redirect_path_when_no_organization, #redirect_to_no_organization!, #require_organization!, #require_organization_admin!, #require_organization_owner!, #require_organization_permission_to!, #require_organization_role!, #switch_to_organization!

Instance Method Details

#createObject

Switch to a different organization POST /organizations/switch/:id



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/organizations/switch_controller.rb', line 14

def create
  user = organizations_current_user(refresh: true)
  return respond_unauthorized unless user

  org = user.organizations.find_by(id: params[:id])

  if org
    switch_to_organization!(org, user: user)

    respond_to do |format|
      format.html { redirect_to after_switch_path(org, user: user), notice: "Switched to #{org.name}" }
      format.json { render json: { organization: { id: org.id, name: org.name } } }
    end
  else
    respond_to do |format|
      format.html { redirect_back fallback_location: main_app.root_path, alert: "Organization not found or you're not a member" }
      format.json { render json: { error: "Organization not found" }, status: :not_found }
    end
  end
end