2022-06-01 21:06:19 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-30 21:20:27 +08:00
|
|
|
class TeamUsersDatatable < CustomDatatable
|
2017-01-25 16:48:49 +08:00
|
|
|
include InputSanitizeHelper
|
2017-04-25 23:37:42 +08:00
|
|
|
include ActiveRecord::Sanitization::ClassMethods
|
2017-01-25 16:48:49 +08:00
|
|
|
|
|
|
|
def_delegator :@view, :link_to
|
|
|
|
def_delegator :@view, :update_user_team_path
|
|
|
|
def_delegator :@view, :destroy_user_team_html_path
|
|
|
|
|
|
|
|
def initialize(view, team, user)
|
|
|
|
super(view)
|
|
|
|
@team = team
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
def sortable_columns
|
|
|
|
@sortable_columns ||= [
|
|
|
|
'User.full_name',
|
|
|
|
'User.email',
|
2022-06-01 21:06:19 +08:00
|
|
|
'UserRole.name',
|
|
|
|
'UserAssignment.created_at',
|
2021-03-22 20:40:25 +08:00
|
|
|
'User.status'
|
2017-01-25 16:48:49 +08:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def searchable_columns
|
|
|
|
@searchable_columns ||= [
|
|
|
|
'User.full_name',
|
|
|
|
'User.email',
|
2022-06-01 21:06:19 +08:00
|
|
|
'UserAssignment.created_at'
|
2017-01-25 16:48:49 +08:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# A hack that overrides the new_search_contition method default behavior of
|
|
|
|
# the ajax-datatables-rails gem now the method checks if the column is the
|
|
|
|
# created_at and generate a custom SQL to parse it back to the caller method
|
|
|
|
def new_search_condition(column, value)
|
|
|
|
model, column = column.split('.')
|
|
|
|
model = model.constantize
|
|
|
|
if column == 'created_at'
|
|
|
|
casted_column = ::Arel::Nodes::NamedFunction.new(
|
|
|
|
'CAST',
|
|
|
|
[Arel.sql(
|
2022-10-03 22:56:15 +08:00
|
|
|
"to_char( user_assignments.created_at, '#{formated_date}' ) AS VARCHAR"
|
2017-01-25 16:48:49 +08:00
|
|
|
)]
|
|
|
|
)
|
|
|
|
else
|
|
|
|
casted_column = ::Arel::Nodes::NamedFunction.new(
|
|
|
|
'CAST',
|
|
|
|
[model.arel_table[column.to_sym].as(typecast)]
|
|
|
|
)
|
|
|
|
end
|
2017-04-25 23:37:42 +08:00
|
|
|
casted_column.matches("%#{sanitize_sql_like(value)}%")
|
2017-01-25 16:48:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-01-07 20:29:17 +08:00
|
|
|
# Returns json of current items (already paginated)
|
2017-01-25 16:48:49 +08:00
|
|
|
def data
|
|
|
|
records.map do |record|
|
|
|
|
{
|
|
|
|
'DT_RowId': record.id,
|
|
|
|
'0': escape_input(record.user.full_name),
|
|
|
|
'1': escape_input(record.user.email),
|
2022-06-01 21:06:19 +08:00
|
|
|
'2': record.user_role.name,
|
2017-01-30 22:55:21 +08:00
|
|
|
'3': I18n.l(record.created_at, format: :full_date),
|
|
|
|
'4': record.user.active_status_str,
|
2017-01-25 16:48:49 +08:00
|
|
|
'5': ApplicationController.new.render_to_string(
|
2023-06-21 20:13:20 +08:00
|
|
|
partial: 'users/settings/teams/user_dropdown',
|
2017-01-25 16:48:49 +08:00
|
|
|
locals: {
|
2022-06-01 21:06:19 +08:00
|
|
|
user_assignment: record,
|
2017-01-25 16:48:49 +08:00
|
|
|
update_role_path: update_user_team_path(record, format: :json),
|
|
|
|
destroy_uo_link: link_to(
|
|
|
|
I18n.t('users.settings.teams.edit.user_dropdown.remove_label'),
|
|
|
|
destroy_user_team_html_path(record, format: :json),
|
|
|
|
remote: true,
|
|
|
|
data: { action: 'destroy-user-team' }
|
|
|
|
),
|
|
|
|
user: @user
|
2023-07-05 18:43:23 +08:00
|
|
|
},
|
|
|
|
formats: :html
|
2017-01-25 16:48:49 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-22 20:40:25 +08:00
|
|
|
# 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
|
|
|
|
|
2017-01-25 16:48:49 +08:00
|
|
|
# Query database for records (this will be later paginated and filtered)
|
|
|
|
# after that "data" function will return json
|
|
|
|
def get_raw_records
|
2022-10-03 22:56:15 +08:00
|
|
|
@team.user_assignments.joins(:user)
|
2017-01-25 16:48:49 +08:00
|
|
|
end
|
2021-03-22 20:40:25 +08:00
|
|
|
|
|
|
|
def sort_records(records)
|
2022-06-01 21:06:19 +08:00
|
|
|
case sort_column(order_params)
|
|
|
|
when 'users.status'
|
2021-03-22 20:40:25 +08:00
|
|
|
records = records.sort_by { |record| record.user.active_status_str }
|
|
|
|
order_params['dir'] == 'asc' ? records : records.reverse
|
2022-06-01 21:06:19 +08:00
|
|
|
when 'user_roles.name'
|
|
|
|
records = records.sort_by { |record| record.user_role.name }
|
2021-03-22 20:40:25 +08:00
|
|
|
order_params['dir'] == 'asc' ? records : records.reverse
|
|
|
|
else
|
|
|
|
super(records)
|
|
|
|
end
|
|
|
|
end
|
2017-01-25 16:48:49 +08:00
|
|
|
end
|