Refactor teams settings into its own controllers

This commit is contained in:
Luka Murn 2017-02-09 14:33:12 +01:00
parent 7bee3a4ea0
commit f7bd62547e
21 changed files with 397 additions and 384 deletions

View file

@ -0,0 +1,161 @@
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
]
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
def load_user
@user = current_user
end
def load_team
@team = Team.find_by_id(params[:id])
render_403 unless is_admin_of_team(@team)
end
def create_params
params.require(:team).permit(
:name,
:description
)
end
def update_params
params.require(:team).permit(
:name,
:description
)
end
end
end
end

View file

@ -0,0 +1,156 @@
module Users
module Settings
class UserTeamsController < ApplicationController
before_action :load_user, only: :destroy
before_action :load_user_team, only: [
:update,
:leave_html,
:destroy_html,
:destroy
]
def update
respond_to do |format|
if @user_team.update(update_user_team_params)
format.json do
render json: {
status: :ok
}
end
else
format.json do
render json: @user_team.errors,
status: :unprocessable_entity
end
end
end
end
def leave_html
respond_to do |format|
format.json do
render json: {
html: render_to_string(
partial:
'users/settings/user_teams/leave_user_team_modal_body.html.erb',
locals: { user_team: @user_team }
),
heading: I18n.t(
'users.settings.user_teams.leave_uo_heading',
team: escape_input(@user_team.team.name)
)
}
end
end
end
def destroy_html
respond_to do |format|
format.json do
render json: {
html: render_to_string(
partial: 'users/settings/user_teams/' \
'destroy_user_team_modal_body.html.erb',
locals: { user_team: @user_team }
),
heading: I18n.t(
'users.settings.user_teams.destroy_uo_heading',
user: escape_input(@user_team.user.full_name),
team: escape_input(@user_team.team.name)
)
}
end
end
end
def destroy
respond_to do |format|
# If user is last administrator of team,
# he/she cannot be deleted from it.
invalid =
@user_team.admin? &&
@user_team
.team
.user_teams
.where(role: 2)
.count <= 1
unless invalid
begin
UserTeam.transaction do
# If user leaves on his/her own accord,
# new owner for projects is the first
# administrator of team
if params[:leave]
new_owner =
@user_team
.team
.user_teams
.where(role: 2)
.where.not(id: @user_team.id)
.first
.user
else
# Otherwise, the new owner for projects is
# the current user (= an administrator removing
# the user from the team)
new_owner = current_user
end
reset_user_current_team(@user_team)
@user_team.destroy(new_owner)
end
rescue Exception
invalid = true
end
end
if !invalid
if params[:leave]
flash[:notice] = I18n.t(
'users.settings.user_teams.leave_flash',
team: @user_team.team.name
)
flash.keep(:notice)
end
generate_notification(@user_team.user,
@user_team.user,
@user_team.team,
false,
false)
format.json { render json: { status: :ok } }
else
format.json do
render json: @user_team.errors,
status: :unprocessable_entity
end
end
end
end
private
def load_user
@user = current_user
end
def load_user_team
@user_team = UserTeam.find_by_id(params[:id])
@team = @user_team.team
# Don't allow the user to modify UserTeam-s if he's not admin,
# unless he/she is modifying his/her UserTeam
if current_user != @user_team.user &&
!is_admin_of_team(@user_team.team)
render_403
end
end
def reset_user_current_team(user_team)
ids = user_team.user.teams_ids
ids -= [user_team.team.id]
user_team.user.current_team_id = ids.first
user_team.user.save
end
end
end
end

View file

@ -4,265 +4,9 @@ class Users::SettingsController < ApplicationController
include InputSanitizeHelper
before_action :load_user, only: [
:teams,
:team,
:create_team,
:teams_datatable,
:team_users_datatable,
:user_current_team,
:destroy_user_team
:user_current_team
]
before_action :check_team_permission, only: [
:team,
:update_team,
:destroy_team,
:team_name,
:team_description,
:team_users_datatable
]
before_action :check_user_team_permission, only: [
:update_user_team,
:leave_user_team_html,
:destroy_user_team_html,
:destroy_user_team
]
def teams
@user_teams =
@user
.user_teams
.includes(team: :users)
.order(created_at: :asc)
@member_of = @user_teams.count
end
def team
@user_team = UserTeam.find_by(user: @user, team: @team)
end
def update_team
respond_to do |format|
if @team.update(update_team_params)
@team.update(last_modified_by: current_user)
format.json {
render json: {
status: :ok,
description_label: render_to_string(
partial: "users/settings/teams/description_label.html.erb",
locals: { team: @team }
)
}
}
else
format.json {
render json: @team.errors,
status: :unprocessable_entity
}
end
end
end
def team_name
respond_to do |format|
format.json {
render json: {
html: render_to_string({
partial: "users/settings/teams/name_modal_body.html.erb",
locals: { team: @team }
})
}
}
end
end
def team_description
respond_to do |format|
format.json {
render json: {
html: render_to_string({
partial: "users/settings/teams/description_modal_body.html.erb",
locals: { team: @team }
})
}
}
end
end
def teams_datatable
respond_to do |format|
format.json do
render json: ::TeamsDatatable.new(view_context, @user)
end
end
end
def team_users_datatable
respond_to do |format|
format.json {
render json: ::TeamUsersDatatable.new(view_context, @team, @user)
}
end
end
def new_team
@new_team = Team.new
end
def create_team
@new_team = Team.new(create_team_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: :team, team_id: @new_team.id
else
render :new_team
end
end
def destroy_team
@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: :teams
end
def update_user_team
respond_to do |format|
if @user_team.update(update_user_team_params)
format.json {
render json: {
status: :ok
}
}
else
format.json {
render json: @user_team.errors,
status: :unprocessable_entity
}
end
end
end
def leave_user_team_html
respond_to do |format|
format.json do
render json: {
html: render_to_string(
partial: 'users/settings/teams/leave_user_team_modal_body.html.erb',
locals: { user_team: @user_team }
),
heading: I18n.t(
'users.settings.teams.index.leave_uo_heading',
team: escape_input(@user_team.team.name)
)
}
end
end
end
def destroy_user_team_html
respond_to do |format|
format.json do
render json: {
html: render_to_string(
partial: 'users/settings/teams/' \
'destroy_user_team_modal_body.html.erb',
locals: { user_team: @user_team }
),
heading: I18n.t(
'users.settings.teams.edit.destroy_uo_heading',
user: escape_input(@user_team.user.full_name),
team: escape_input(@user_team.team.name)
)
}
end
end
end
def destroy_user_team
respond_to do |format|
# If user is last administrator of team,
# he/she cannot be deleted from it.
invalid =
@user_team.admin? &&
@user_team
.team
.user_teams
.where(role: 2)
.count <= 1
if !invalid then
begin
UserTeam.transaction do
# If user leaves on his/her own accord,
# new owner for projects is the first
# administrator of team
if params[:leave]
new_owner =
@user_team
.team
.user_teams
.where(role: 2)
.where.not(id: @user_team.id)
.first
.user
else
# Otherwise, the new owner for projects is
# the current user (= an administrator removing
# the user from the team)
new_owner = current_user
end
reset_user_current_team(@user_team)
@user_team.destroy(new_owner)
end
rescue Exception
invalid = true
end
end
if !invalid
if params[:leave] then
flash[:notice] = I18n.t(
'users.settings.teams.index.leave_flash',
team: @user_team.team.name
)
flash.keep(:notice)
end
generate_notification(@user_team.user,
@user_team.user,
@user_team.team,
false,
false)
format.json {
render json: {
status: :ok
}
}
else
format.json {
render json: @user_team.errors,
status: :unprocessable_entity
}
end
end
end
def user_current_team
team_id = params[:user][:current_team_id].to_i
if @user.teams_ids.include?(team_id)
@ -284,56 +28,4 @@ class Users::SettingsController < ApplicationController
def load_user
@user = current_user
end
def check_team_permission
@team = Team.find_by_id(params[:team_id])
unless is_admin_of_team(@team)
render_403
end
end
def check_user_team_permission
@user_team = UserTeam.find_by_id(params[:user_team_id])
@team = @user_team.team
# Don't allow the user to modify UserTeam-s if he's not admin,
# unless he/she is modifying his/her UserTeam
if current_user != @user_team.user &&
!is_admin_of_team(@user_team.team)
render_403
end
end
def create_team_params
params.require(:team).permit(
:name,
:description
)
end
def update_team_params
params.require(:team).permit(
:name,
:description
)
end
def create_user_params
params.require(:user).permit(
:full_name,
:email
)
end
def update_user_team_params
params.require(:user_team).permit(
:role
)
end
def reset_user_current_team(user_team)
ids = user_team.user.teams_ids
ids -= [user_team.team.id]
user_team.user.current_team_id = ids.first
user_team.user.save
end
end

View file

@ -13,6 +13,7 @@ module UserSettingsHelper
end
def on_settings_team_page?
action_name.in?(%w(teams team new_team create_team))
controller_name == 'teams' &&
action_name.in?(%w(index new create show))
end
end

View file

@ -1,7 +1,7 @@
<ul data-hook="user-settings-navigation-html"
class="nav nav-tabs nav-settings">
<li role="presentation" class="<%= 'active' if on_settings_account_page? %>">
<%= link_to t("users.settings.navigation.account"), preferences_path %>
<%= link_to t("users.settings.navigation.account"), edit_user_registration_path %>
</li>
<li role="presentation" class="<%= "active" if on_settings_team_page? %>">
<%= link_to t("users.settings.navigation.teams"), teams_path %>

View file

@ -119,4 +119,4 @@
<%= javascript_include_tag "users/settings/account/preferences" %>
<%= javascript_include_tag "users/settings/account/preferences/index" %>

View file

@ -1,5 +1,5 @@
<ol class="breadcrumb breadcrumb-teams">
<% if action_name == "teams" %>
<% if action_name == "index" %>
<li class="active">
<%= t("users.settings.teams.breadcrumbs.all") %>
</li>
@ -9,11 +9,11 @@
</li>
<% end %>
<% if action_name == 'team' %>
<% if action_name == 'show' %>
<li class="active">
<%= @team.name %>
</li>
<% elsif action_name == 'new_team' || action_name == 'create_team' %>
<% elsif action_name == 'new' || action_name == 'create' %>
<li class="active">
<%= t('users.settings.teams.breadcrumbs.new_team') %>
</li>

View file

@ -43,7 +43,7 @@
</div>
</div>
<%= render partial: "users/settings/teams/leave_user_team_modal.html.erb" %>
<%= render partial: "users/settings/user_teams/leave_user_team_modal.html.erb" %>
<%= stylesheet_link_tag 'datatables' %>
<%= javascript_include_tag "users/settings/teams" %>
<%= javascript_include_tag "users/settings/teams_datatable" %>
<%= javascript_include_tag "users/settings/teams/index" %>
<%= javascript_include_tag "users/settings/teams/datatable" %>

View file

@ -122,6 +122,6 @@
)
%>
<%= render partial: 'users/settings/teams/destroy_modal.html.erb', locals: { team: @team } %>
<%= render partial: 'users/settings/teams/destroy_user_team_modal.html.erb' %>
<%= render partial: 'users/settings/user_teams/destroy_user_team_modal.html.erb' %>
<%= stylesheet_link_tag 'datatables' %>
<%= javascript_include_tag 'users/settings/team' %>
<%= javascript_include_tag 'users/settings/teams/show' %>

View file

@ -15,7 +15,7 @@
<button type="button" class="btn btn-default" data-dismiss="modal"><%=t 'general.cancel' %></button>
<button type="button"
class="btn btn-primary"
data-action="submit"><%=t 'users.settings.teams.edit.destroy_uo_confirm' %></button>
data-action="submit"><%=t 'users.settings.user_teams.destroy_uo_confirm' %></button>
</div>
</div>
</div>

View file

@ -3,18 +3,18 @@
remote: :true,
method: :delete,
data: { id: 'destroy-user-team-form' } do |f| %>
<p><%= t("users.settings.teams.edit.destroy_uo_message",
<p><%= t("users.settings.user_teams.destroy_uo_message",
user: user_team.user.full_name,
team: user_team.team.name) %></p>
<% if user_team.user.confirmed? %>
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign"></span>
&nbsp;
<%= t("users.settings.teams.edit.destroy_uo_alert_heading") %>
<%= t("users.settings.user_teams.destroy_uo_alert_heading") %>
<ul>
<li><%= t("users.settings.teams.edit.destroy_uo_alert_line_1") %></li>
<li><%= t("users.settings.teams.edit.destroy_uo_alert_line_2").html_safe %></li>
<li><%= t("users.settings.teams.edit.destroy_uo_alert_line_3") %></li>
<li><%= t("users.settings.user_teams.destroy_uo_alert_line_1") %></li>
<li><%= t("users.settings.user_teams.destroy_uo_alert_line_2").html_safe %></li>
<li><%= t("users.settings.user_teams.destroy_uo_alert_line_3") %></li>
</ul>
</div>
<% end %>

View file

@ -18,7 +18,7 @@
<button type="button" class="btn btn-default" data-dismiss="modal"><%= t('general.cancel') %></button>
<button type="button"
class="btn btn-primary"
data-action="submit"><%= t('users.settings.teams.index.leave_uo_confirm') %></button>
data-action="submit"><%= t('users.settings.user_teams.leave_uo_confirm') %></button>
</div>
</div>
</div>

View file

@ -4,15 +4,15 @@
method: :delete,
data: { id: 'leave-user-team-form' } do |f| %>
<%= hidden_field_tag :leave, true %>
<p><%= t("users.settings.teams.index.leave_uo_message", team: user_team.team.name) %></p>
<p><%= t("users.settings.user_teams.leave_uo_message", team: user_team.team.name) %></p>
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign"></span>
&nbsp;
<%= t("users.settings.teams.index.leave_uo_alert_heading") %>
<%= t("users.settings.user_teams.leave_uo_alert_heading") %>
<ul>
<li><%= t("users.settings.teams.index.leave_uo_alert_line_1") %></li>
<li><%= t("users.settings.teams.index.leave_uo_alert_line_2").html_safe %></li>
<li><%= t("users.settings.teams.index.leave_uo_alert_line_3")%></li>
<li><%= t("users.settings.user_teams.leave_uo_alert_line_1") %></li>
<li><%= t("users.settings.user_teams.leave_uo_alert_line_2").html_safe %></li>
<li><%= t("users.settings.user_teams.leave_uo_alert_line_3")%></li>
</ul>
</div>
<% end %>

View file

@ -14,13 +14,13 @@ Rails.application.config.assets.precompile += %w(jsPlumb-2.0.4-min.js)
Rails.application.config.assets.precompile += %w(jsnetworkx.js)
Rails.application.config.assets.precompile += %w(handsontable.full.min.js)
Rails.application.config.assets.precompile +=
%w(users/settings/account/preferences.js)
Rails.application.config.assets.precompile += %w(users/settings/teams.js)
%w(users/settings/account/preferences/index.js)
Rails.application.config.assets.precompile += %w(users/settings/teams/index.js)
Rails.application.config.assets.precompile +=
%w(users/settings/teams_datatable.js)
%w(users/settings/teams/datatable.js)
Rails.application.config.assets.precompile +=
%w(users/settings/teams/add_user_modal.js)
Rails.application.config.assets.precompile += %w(users/settings/team.js)
Rails.application.config.assets.precompile += %w(users/settings/teams/show.js)
Rails.application.config.assets.precompile += %w(my_modules/activities.js)
Rails.application.config.assets.precompile += %w(my_modules/protocols.js)
Rails.application.config.assets.precompile +=

View file

@ -1105,14 +1105,6 @@ en:
thead_members: "Members"
na: "n/a"
leave: "Leave team"
leave_uo_heading: "Leave team %{team}"
leave_uo_message: "Are you sure you wish to leave team %{team}? This action is irreversible."
leave_uo_alert_heading: "Leaving team has following consequences:"
leave_uo_alert_line_1: "you will lose access to all content belonging to the team (including projects, tasks, protocols and activities);"
leave_uo_alert_line_2: "all projects in the team where you were the sole <b>Owner</b> will receive a new owner from the team administrators;"
leave_uo_alert_line_3: "all repository protocols in the team belonging to you will be reassigned onto a new owner from team administrators."
leave_uo_confirm: "Leave"
leave_flash: "Successfuly left team %{team}."
new:
name_label: "Team name"
name_placeholder: "My team"
@ -1141,13 +1133,6 @@ en:
user_dropdown:
role_label: "User role"
remove_label: "Remove"
destroy_uo_heading: "Remove user %{user} from team %{team}"
destroy_uo_message: "Are you sure you wish to remove user %{user} from team %{team}?"
destroy_uo_alert_heading: "Removing user from team has following consequences:"
destroy_uo_alert_line_1: "user will lose access to all content belonging to the team (including projects, tasks, protocols and activities);"
destroy_uo_alert_line_2: "all projects in the team where user was the sole <b>Owner</b> will be reassigned onto you as a new owner;"
destroy_uo_alert_line_3: "all repository protocols in the team belonging to user will be reassigned onto you."
destroy_uo_confirm: "Remove user"
delete_team_heading: "Delete team"
can_delete_message: "This team can be deleted because it doesn't have any projects."
delete_text: "Delete team."
@ -1157,6 +1142,22 @@ en:
message: "Are you sure you wish to delete team %{team}? All of the users will be removed from the team as well. This action is irreversible."
confirm: "Delete team"
flash_success: "Team %{team} was successfully deleted."
user_teams:
leave_uo_heading: "Leave team %{team}"
leave_uo_message: "Are you sure you wish to leave team %{team}? This action is irreversible."
leave_uo_alert_heading: "Leaving team has following consequences:"
leave_uo_alert_line_1: "you will lose access to all content belonging to the team (including projects, tasks, protocols and activities);"
leave_uo_alert_line_2: "all projects in the team where you were the sole <b>Owner</b> will receive a new owner from the team administrators;"
leave_uo_alert_line_3: "all repository protocols in the team belonging to you will be reassigned onto a new owner from team administrators."
leave_uo_confirm: "Leave"
destroy_uo_heading: "Remove user %{user} from team %{team}"
destroy_uo_message: "Are you sure you wish to remove user %{user} from team %{team}?"
destroy_uo_alert_heading: "Removing user from team has following consequences:"
destroy_uo_alert_line_1: "user will lose access to all content belonging to the team (including projects, tasks, protocols and activities);"
destroy_uo_alert_line_2: "all projects in the team where user was the sole <b>Owner</b> will be reassigned onto you as a new owner;"
destroy_uo_alert_line_3: "all repository protocols in the team belonging to user will be reassigned onto you."
destroy_uo_confirm: "Remove user"
leave_flash: "Successfuly left team %{team}."
protocols:
nav:

View file

@ -43,47 +43,49 @@ Rails.application.routes.draw do
post 'users/settings/user_current_team',
to: 'users/settings#user_current_team',
as: 'user_current_team'
get 'users/settings/teams',
to: 'users/settings#teams',
to: 'users/settings/teams#index',
as: 'teams'
get 'users/settings/teams/new',
to: 'users/settings#new_team',
as: 'new_team'
post 'users/settings/teams/new',
to: 'users/settings#create_team',
as: 'create_team'
get 'users/settings/teams/:team_id',
to: 'users/settings#team',
as: 'team'
put 'users/settings/teams/:team_id',
to: 'users/settings#update_team',
as: 'update_team'
get 'users/settings/teams/:team_id/name',
to: 'users/settings#team_name',
as: 'team_name'
get 'users/settings/teams/:team_id/description',
to: 'users/settings#team_description',
as: 'team_description'
post 'users/settings/teams/teams_datatable',
to: 'users/settings#teams_datatable',
post 'users/settings/teams/datatable',
to: 'users/settings/teams#datatable',
as: 'teams_datatable'
post 'users/settings/teams/:team_id/users_datatable',
to: 'users/settings#team_users_datatable',
get 'users/settings/teams/new',
to: 'users/settings/teams#new',
as: 'new_team'
post 'users/settings/teams',
to: 'users/settings/teams#create',
as: 'create_team'
get 'users/settings/teams/:id',
to: 'users/settings/teams#show',
as: 'team'
post 'users/settings/teams/:id/users_datatable',
to: 'users/settings/teams#users_datatable',
as: 'team_users_datatable'
delete 'users/settings/teams/:team_id',
to: 'users/settings#destroy_team',
get 'users/settings/teams/:id/name_html',
to: 'users/settings/teams#name_html',
as: 'team_name'
get 'users/settings/teams/:id/description_html',
to: 'users/settings/teams#description_html',
as: 'team_description'
put 'users/settings/teams/:id',
to: 'users/settings/teams#update',
as: 'update_team'
delete 'users/settings/teams/:id',
to: 'users/settings/teams#destroy',
as: 'destroy_team'
put 'users/settings/user_teams/:user_team_id',
to: 'users/settings#update_user_team',
put 'users/settings/user_teams/:id',
to: 'users/settings/user_teams#update',
as: 'update_user_team'
get 'users/settings/user_teams/:user_team_id/leave_html',
to: 'users/settings#leave_user_team_html',
get 'users/settings/user_teams/:id/leave_html',
to: 'users/settings/user_teams#leave_html',
as: 'leave_user_team_html'
get 'users/settings/user_teams/:user_team_id/destroy_html',
to: 'users/settings#destroy_user_team_html',
get 'users/settings/user_teams/:id/destroy_html',
to: 'users/settings/user_teams#destroy_html',
as: 'destroy_user_team_html'
delete 'users/settings/user_teams/:user_team_id',
to: 'users/settings#destroy_user_team',
delete 'users/settings/user_teams/:id',
to: 'users/settings/user_teams#destroy',
as: 'destroy_user_team'
# Invite users