forked from hackorum-dev/hackorum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteams_controller.rb
More file actions
66 lines (54 loc) · 1.78 KB
/
teams_controller.rb
File metadata and controls
66 lines (54 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
# frozen_string_literal: true
module Settings
class TeamsController < Settings::BaseController
before_action :set_team, only: [:show, :destroy]
before_action :require_team_member!, only: [:show]
before_action :require_team_admin!, only: [:destroy]
skip_before_action :require_authentication, only: [:index]
def index
@your_teams = user_signed_in? ? current_user.teams.includes(team_members: :user) : []
@all_teams = Team.includes(team_members: :user).order(:name)
end
def show
@team_members = @team.team_members.includes(:user)
@can_manage = user_signed_in? && @team.admin?(current_user)
@can_invite = user_signed_in? && @team.admin?(current_user)
end
def create
@team = Team.new(team_params)
Team.transaction do
@team.save!
TeamMember.add_member(team: @team, user: current_user, role: :admin)
end
redirect_to settings_team_path(@team), notice: "Team created"
rescue ActiveRecord::RecordInvalid => e
redirect_to settings_teams_path, alert: e.record.errors.full_messages.to_sentence
end
def destroy
@team.destroy
redirect_to settings_teams_path, notice: "Team deleted"
end
private
def active_settings_section
:teams
end
def set_team
@team = Team.find(params[:id])
end
def team_params
params.require(:team).permit(:name)
end
def require_team_admin!
unless user_signed_in? && @team.admin?(current_user)
redirect_to settings_team_path(@team), alert: "Admins only" and return
end
end
def require_team_member!
unless user_signed_in?
redirect_to new_session_path, alert: "Please sign in"
return
end
render_404 unless @team.member?(current_user)
end
end
end