2014-03-23 00:22:25 +00:00
|
|
|
class TeamsController < ApplicationController
|
2014-03-31 21:33:16 +00:00
|
|
|
before_filter :get_team, only: [:show, :edit, :update, :destroy, :recover]
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
|
|
def index
|
2014-04-27 23:16:28 +00:00
|
|
|
@teams = Team.with_teamers_num(0).search(params[:search]).paginate(per_page: 80, page: params[:page]).ordered
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2014-05-13 23:54:51 +00:00
|
|
|
@teamer = Teamer.new
|
|
|
|
@teamer.user = @user
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@team = Team.new
|
|
|
|
raise AccessError unless @team.can_create? cuser
|
|
|
|
end
|
|
|
|
|
2014-05-19 20:16:46 +00:00
|
|
|
def replace_teamer
|
2014-05-13 23:54:51 +00:00
|
|
|
redirect_to_back
|
|
|
|
end
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
def edit
|
|
|
|
raise AccessError unless @team.can_update? cuser
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@team = Team.new params[:team]
|
|
|
|
@team.founder = cuser
|
|
|
|
raise AccessError unless @team.can_create? cuser
|
|
|
|
|
|
|
|
if @team.save
|
|
|
|
flash[:notice] = t(:teams_create)
|
|
|
|
redirect_to @team
|
|
|
|
else
|
2014-03-31 21:33:16 +00:00
|
|
|
render :new
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
raise AccessError unless @team.can_update? cuser
|
|
|
|
if @team.update_attributes params[:team]
|
|
|
|
if params[:rank]
|
|
|
|
@team.teamers.present.each do |member|
|
2017-08-12 23:13:35 +00:00
|
|
|
# Contains new rank as given by submitted parameters
|
|
|
|
new_rank = params[:rank]["#{member.id}"]
|
|
|
|
# Can only set own rank to equal or lower than current rank
|
|
|
|
if cuser.admin? or (new_rank.to_i <= cuser.teamers.active.of_team(@team).first.rank)
|
2017-12-03 19:53:07 +00:00
|
|
|
# Don't allow setting members back to rank joining
|
|
|
|
next if new_rank == Teamer::RANK_JOINER && member.rank != Teamer::RANK_JOINER
|
|
|
|
|
2017-08-12 23:13:35 +00:00
|
|
|
# Update team when rank changes from joiner to member or higher
|
|
|
|
if member.rank == Teamer::RANK_JOINER && new_rank.to_i >= Teamer::RANK_MEMBER
|
2014-03-23 00:22:25 +00:00
|
|
|
member.user.update_attribute :team, @team
|
|
|
|
end
|
2017-08-12 23:13:35 +00:00
|
|
|
member.update_attribute :rank, new_rank
|
2014-03-23 00:22:25 +00:00
|
|
|
member.update_attribute :comment, params[:comment]["#{member.id}"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
flash[:notice] = t(:teams_update)
|
|
|
|
redirect_to edit_team_path(@team)
|
|
|
|
else
|
2014-03-31 21:33:16 +00:00
|
|
|
render :edit
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
raise AccessError unless @team.can_destroy? cuser
|
|
|
|
@team.destroy
|
|
|
|
redirect_to(teams_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def recover
|
|
|
|
raise AccessError unless @team.can_destroy? cuser
|
|
|
|
@team.recover
|
|
|
|
redirect_to(teams_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def get_team
|
|
|
|
@team = Team.find params[:id]
|
|
|
|
end
|
|
|
|
end
|