Class: Organizations::OrganizationsController

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

Overview

Controller for managing organizations. Provides CRUD operations for organizations the user owns/manages.

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

POST /organizations Create a new organization



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/organizations/organizations_controller.rb', line 41

def create
  begin
    @organization = create_organization_and_switch!(current_user, organization_params.to_h)

    respond_to do |format|
      format.html { redirect_to after_create_redirect_path(@organization), notice: "Organization created successfully." }
      format.json { render json: organization_json(@organization), status: :created }
    end
  rescue Organizations::Models::Concerns::HasOrganizations::OrganizationLimitReached => e
    respond_to do |format|
      format.html do
        @organization = Organization.new(organization_params)
        flash.now[:alert] = e.message
        render :new, status: :unprocessable_entity
      end
      format.json { render json: { error: e.message }, status: :unprocessable_entity }
    end
  rescue ActiveRecord::RecordInvalid => e
    respond_to do |format|
      format.html do
        @organization = Organization.new(organization_params)
        flash.now[:alert] = e.record.errors.full_messages.join(", ")
        render :new, status: :unprocessable_entity
      end
      format.json { render json: { errors: e.record.errors }, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /organizations/:id Delete organization (owner only)



93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/organizations/organizations_controller.rb', line 93

def destroy
  @organization.destroy!

  # Clear session if this was the current organization
  clear_organization_session! if current_organization&.id == @organization.id

  respond_to do |format|
    format.html { redirect_to organizations_path, notice: "Organization deleted successfully." }
    format.json { head :no_content }
  end
end

#editObject

GET /organizations/:id/edit Form to edit organization



72
73
# File 'app/controllers/organizations/organizations_controller.rb', line 72

def edit
end

#indexObject

GET /organizations List all organizations the user belongs to



14
15
16
17
18
19
20
21
22
# File 'app/controllers/organizations/organizations_controller.rb', line 14

def index
  # Optimized query: preload memberships and use counter cache or subquery for counts
  @memberships = current_user.memberships.includes(:organization)

  respond_to do |format|
    format.html { @organizations = @memberships.map(&:organization) }
    format.json { render json: organizations_json_optimized(@memberships) }
  end
end

#newObject

GET /organizations/new Form to create a new organization



35
36
37
# File 'app/controllers/organizations/organizations_controller.rb', line 35

def new
  @organization = Organization.new
end

#showObject

GET /organizations/:id Show organization details



26
27
28
29
30
31
# File 'app/controllers/organizations/organizations_controller.rb', line 26

def show
  respond_to do |format|
    format.html
    format.json { render json: organization_json(@organization) }
  end
end

#updateObject

PATCH/PUT /organizations/:id Update organization



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/organizations/organizations_controller.rb', line 77

def update
  if @organization.update(organization_params)
    respond_to do |format|
      format.html { redirect_to organization_path(@organization), notice: "Organization updated successfully." }
      format.json { render json: organization_json(@organization) }
    end
  else
    respond_to do |format|
      format.html { render :edit, status: :unprocessable_entity }
      format.json { render json: { errors: @organization.errors }, status: :unprocessable_entity }
    end
  end
end