diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a926fae52..db89d7e6f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -37,7 +37,7 @@ class ApplicationController < ActionController::Base # Sets current team for all controllers def current_team - @current_team ||= Team.find_by(id: current_user.current_team_id) + @current_team ||= current_user.teams.find_by(id: current_user.current_team_id) end def to_user_date_format @@ -83,13 +83,12 @@ class ApplicationController < ActionController::Base private def update_current_team - @current_team = Team.find_by_id(current_user.current_team_id) - if (current_team.nil? || !current_user.is_member_of_team?(current_team)) && - current_user.teams.count.positive? + return if current_team.present? && current_team.id == current_user.current_team_id - current_user.update( - current_team_id: current_user.teams.first.id - ) + if current_user.current_team_id + @current_team = current_user.teams.find_by(id: current_user.current_team_id) + elsif current_user.teams.any? + current_user.update(current_team_id: current_user.teams.first.id) end end diff --git a/app/helpers/teams_helper.rb b/app/helpers/teams_helper.rb index 87c8d0ffe..c5e69bfb1 100644 --- a/app/helpers/teams_helper.rb +++ b/app/helpers/teams_helper.rb @@ -1,7 +1,7 @@ module TeamsHelper # resets the current team if needed def current_team_switch(team) - if team != current_team + if team != current_team && current_user.is_member_of_team?(team) current_user.current_team_id = team.id current_user.save update_current_team @@ -18,11 +18,7 @@ module TeamsHelper end end - def team_created_by(team) - User.find_by_id(team.created_by_id) - end - def switch_team_with_param - current_team_switch(Team.find_by(id: params[:team])) if params[:team] + current_team_switch(current_user.teams.find_by(id: params[:team])) if params[:team] end end diff --git a/app/views/users/settings/teams/show.html.erb b/app/views/users/settings/teams/show.html.erb index 9b3e4cb10..4893123ed 100644 --- a/app/views/users/settings/teams/show.html.erb +++ b/app/views/users/settings/teams/show.html.erb @@ -44,7 +44,9 @@