2017-06-30 21:20:27 +08:00
|
|
|
class TeamsDatatable < CustomDatatable
|
2017-02-02 16:53:59 +08:00
|
|
|
include InputSanitizeHelper
|
|
|
|
|
|
|
|
def_delegator :@view, :link_to
|
|
|
|
def_delegator :@view, :team_path
|
|
|
|
def_delegator :@view, :leave_user_team_html_path
|
|
|
|
|
|
|
|
MEMEBERS_SORT_COL = 'members'.freeze
|
|
|
|
|
|
|
|
def initialize(view, user)
|
|
|
|
super(view)
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
def sortable_columns
|
|
|
|
@sortable_columns ||= [
|
|
|
|
'Team.name',
|
2022-10-06 00:28:40 +08:00
|
|
|
'UserRole.name',
|
2017-02-02 16:53:59 +08:00
|
|
|
MEMEBERS_SORT_COL
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-01-07 20:29:17 +08:00
|
|
|
# Returns json of current items (already paginated)
|
2017-02-02 16:53:59 +08:00
|
|
|
def data
|
|
|
|
records.map do |record|
|
|
|
|
{
|
|
|
|
'DT_RowId': record.id,
|
2022-10-06 00:28:40 +08:00
|
|
|
'0': if current_user_team_role(record)&.owner?
|
|
|
|
link_to(escape_input(record.name), team_path(record))
|
2017-02-02 16:53:59 +08:00
|
|
|
else
|
2022-10-06 00:28:40 +08:00
|
|
|
escape_input(record.name)
|
2017-02-02 16:53:59 +08:00
|
|
|
end,
|
2022-10-06 00:28:40 +08:00
|
|
|
'1': escape_input(current_user_team_role(record)&.name),
|
2023-08-24 21:15:06 +08:00
|
|
|
'2': record.users.count,
|
2017-02-02 16:53:59 +08:00
|
|
|
'3': leave_team_button(record)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Overwrite default pagination method as here
|
|
|
|
# we need to be able work also with arrays
|
|
|
|
def paginate_records(records)
|
|
|
|
records.to_a.drop(offset).first(per_page)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_paginator
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
# Overwrite default sort method to handle custom members column
|
|
|
|
# which is calculated in code and not present in DB
|
|
|
|
def sort_records(records)
|
2017-06-30 21:20:27 +08:00
|
|
|
if sort_column(order_params) == MEMEBERS_SORT_COL
|
2022-10-06 00:28:40 +08:00
|
|
|
records = records.sort_by(&proc { |team| team.users.count })
|
2017-06-30 21:20:27 +08:00
|
|
|
if order_params['dir'] == 'asc'
|
2022-10-06 00:28:40 +08:00
|
|
|
records
|
2017-06-30 21:20:27 +08:00
|
|
|
elsif order_params['dir'] == 'desc'
|
2022-10-06 00:28:40 +08:00
|
|
|
records.reverse
|
2017-02-02 16:53:59 +08:00
|
|
|
end
|
2022-10-06 00:28:40 +08:00
|
|
|
elsif sort_column(order_params) == 'user_roles.name'
|
2022-10-10 21:48:38 +08:00
|
|
|
records_with_role = records.joins(user_assignments: :user)
|
|
|
|
.where(user_assignments: { user: @user })
|
2022-10-06 00:28:40 +08:00
|
|
|
.sort_by(&proc { |team| current_user_team_role(team)&.name })
|
2022-10-10 21:48:38 +08:00
|
|
|
records_with_no_role =
|
|
|
|
records.joins("LEFT OUTER JOIN user_assignments AS current_user_assignments "\
|
|
|
|
"ON current_user_assignments.assignable_type = 'Team' "\
|
|
|
|
"AND current_user_assignments.assignable_id = teams.id "\
|
|
|
|
"AND current_user_assignments.user_id = #{@user.id}")
|
|
|
|
.where(current_user_assignments: { id: nil })
|
2021-03-12 15:44:09 +08:00
|
|
|
records = records_with_no_role + records_with_role
|
|
|
|
if order_params['dir'] == 'asc'
|
|
|
|
records
|
|
|
|
else
|
|
|
|
records.reverse
|
|
|
|
end
|
2017-02-02 16:53:59 +08:00
|
|
|
else
|
|
|
|
super(records)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# If user is last admin of team, don't allow
|
|
|
|
# him/her to leave team
|
2022-10-06 00:28:40 +08:00
|
|
|
def leave_team_button(record)
|
2023-06-08 14:33:37 +08:00
|
|
|
button = "<span class=\"sn-icon sn-icon-sign-out\"></span>
|
2017-02-02 16:53:59 +08:00
|
|
|
<span class=\"hidden-xs\">
|
|
|
|
#{I18n.t('users.settings.teams.index.leave')}
|
|
|
|
</span>"
|
2022-10-06 00:28:40 +08:00
|
|
|
owner_role = UserRole.find_by(name: UserRole.public_send('owner_role').name)
|
|
|
|
if current_user_team_role(record)&.owner? && record.user_assignments.where(user_role: owner_role).none?
|
2023-06-19 21:45:22 +08:00
|
|
|
button.prepend('<div class="btn btn-secondary btn-xs"
|
2017-02-02 16:53:59 +08:00
|
|
|
type="button" disabled="disabled">')
|
|
|
|
button << '</div>'
|
|
|
|
else
|
|
|
|
button = link_to(
|
|
|
|
button.html_safe,
|
2022-10-06 00:28:40 +08:00
|
|
|
leave_user_team_html_path(current_user_team_assignment(record), format: :json, leave: true),
|
2023-06-19 21:45:22 +08:00
|
|
|
remote: true, class: 'btn btn-secondary btn-xs', type: 'button',
|
2017-02-02 16:53:59 +08:00
|
|
|
data: { action: 'leave-user-team' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
button
|
|
|
|
end
|
|
|
|
|
|
|
|
# Query database for records (this will be later paginated and filtered)
|
|
|
|
# after that "data" function will return json
|
|
|
|
def get_raw_records
|
2022-10-06 00:28:40 +08:00
|
|
|
@user.teams
|
|
|
|
.preload(user_assignments: %i(user user_role))
|
|
|
|
.distinct
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_user_team_assignment(team)
|
|
|
|
team.user_assignments.find { |ua| ua.user == @user }
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_user_team_role(team)
|
|
|
|
current_user_team_assignment(team)&.user_role
|
2017-02-02 16:53:59 +08:00
|
|
|
end
|
|
|
|
end
|