2017-02-09 21:33:12 +08:00
|
|
|
module Users
|
|
|
|
module Settings
|
|
|
|
class TeamsController < ApplicationController
|
|
|
|
before_action :load_user, only: [
|
|
|
|
:index,
|
|
|
|
:datatable,
|
|
|
|
:new,
|
|
|
|
:create,
|
|
|
|
:show,
|
|
|
|
:users_datatable
|
|
|
|
]
|
|
|
|
|
|
|
|
before_action :load_team, only: [
|
|
|
|
:show,
|
|
|
|
:users_datatable,
|
|
|
|
:name_html,
|
|
|
|
:description_html,
|
|
|
|
:update,
|
|
|
|
:destroy
|
|
|
|
]
|
|
|
|
|
2018-01-09 22:28:27 +08:00
|
|
|
before_action :check_create_team_permission,
|
|
|
|
only: %i(new create)
|
|
|
|
|
2018-04-05 22:30:02 +08:00
|
|
|
layout 'fluid'
|
|
|
|
|
2017-02-09 21:33:12 +08:00
|
|
|
def index
|
|
|
|
@user_teams =
|
|
|
|
@user
|
|
|
|
.user_teams
|
|
|
|
.includes(team: :users)
|
|
|
|
.order(created_at: :asc)
|
|
|
|
@member_of = @user_teams.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def datatable
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: ::TeamsDatatable.new(view_context, @user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@new_team = Team.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@new_team = Team.new(create_params)
|
|
|
|
@new_team.created_by = @user
|
|
|
|
|
|
|
|
if @new_team.save
|
|
|
|
# Okay, team is created, now
|
|
|
|
# add the current user as admin
|
|
|
|
UserTeam.create(
|
|
|
|
user: @user,
|
|
|
|
team: @new_team,
|
|
|
|
role: 2
|
|
|
|
)
|
|
|
|
|
|
|
|
# Redirect to new team page
|
|
|
|
redirect_to action: :show, id: @new_team.id
|
|
|
|
else
|
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@user_team = UserTeam.find_by(user: @user, team: @team)
|
|
|
|
end
|
|
|
|
|
|
|
|
def users_datatable
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: ::TeamUsersDatatable.new(view_context, @team, @user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def name_html
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'users/settings/teams/name_modal_body.html.erb',
|
|
|
|
locals: { team: @team }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def description_html
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'users/settings/teams/description_modal_body.html.erb',
|
|
|
|
locals: { team: @team }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
respond_to do |format|
|
|
|
|
if @team.update(update_params)
|
|
|
|
@team.update(last_modified_by: current_user)
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
status: :ok,
|
|
|
|
description_label: render_to_string(
|
|
|
|
partial: 'users/settings/teams/description_label.html.erb',
|
|
|
|
locals: { team: @team }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
format.json do
|
|
|
|
render json: @team.errors,
|
|
|
|
status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@team.destroy
|
|
|
|
|
|
|
|
flash[:notice] = I18n.t(
|
|
|
|
'users.settings.teams.edit.modal_destroy_team.flash_success',
|
|
|
|
team: @team.name
|
|
|
|
)
|
|
|
|
|
|
|
|
# Redirect back to all teams page
|
|
|
|
redirect_to action: :index
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-01-09 22:28:27 +08:00
|
|
|
def check_create_team_permission
|
2018-01-10 17:47:45 +08:00
|
|
|
render_403 unless can_create_teams?
|
2018-01-09 22:28:27 +08:00
|
|
|
end
|
|
|
|
|
2017-02-09 21:33:12 +08:00
|
|
|
def load_user
|
|
|
|
@user = current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_team
|
|
|
|
@team = Team.find_by_id(params[:id])
|
2018-01-24 20:21:53 +08:00
|
|
|
render_403 unless can_update_team?(@team)
|
2017-02-09 21:33:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_params
|
|
|
|
params.require(:team).permit(
|
|
|
|
:name,
|
|
|
|
:description
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_params
|
|
|
|
params.require(:team).permit(
|
|
|
|
:name,
|
|
|
|
:description
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|