2019-12-18 21:10:41 +08:00
|
|
|
# frozen_string_literal: true
|
2018-03-06 23:32:39 +08:00
|
|
|
|
2019-12-18 21:10:41 +08:00
|
|
|
class RepositoryDatatableService
|
2020-01-13 23:31:42 +08:00
|
|
|
attr_reader :repository_rows, :all_count, :mappings
|
2018-03-06 23:32:39 +08:00
|
|
|
|
2018-03-09 21:22:00 +08:00
|
|
|
def initialize(repository, params, user, my_module = nil)
|
2018-03-06 23:32:39 +08:00
|
|
|
@repository = repository
|
2018-03-09 00:36:10 +08:00
|
|
|
@user = user
|
|
|
|
@my_module = my_module
|
|
|
|
@params = params
|
2020-01-13 23:09:07 +08:00
|
|
|
@sortable_columns = build_sortable_columns
|
2018-03-09 21:22:00 +08:00
|
|
|
create_columns_mappings
|
2021-02-22 17:34:06 +08:00
|
|
|
@repository_rows = process_query
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-03-09 21:22:00 +08:00
|
|
|
def create_columns_mappings
|
2018-03-09 21:43:12 +08:00
|
|
|
# Make mappings of custom columns, so we have same id for every
|
|
|
|
# column
|
2020-04-21 20:49:36 +08:00
|
|
|
index = @repository.default_columns_count
|
2018-03-09 21:22:00 +08:00
|
|
|
@mappings = {}
|
|
|
|
@repository.repository_columns.order(:id).each do |column|
|
2018-03-20 00:49:09 +08:00
|
|
|
@mappings[column.id] = index.to_s
|
|
|
|
index += 1
|
2018-03-09 21:22:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-09 00:36:10 +08:00
|
|
|
def process_query
|
2018-03-09 21:43:12 +08:00
|
|
|
search_value = build_conditions(@params)[:search_value]
|
2019-03-01 03:39:10 +08:00
|
|
|
order_obj = build_conditions(@params)[:order_by_column]
|
2018-03-06 23:32:39 +08:00
|
|
|
|
2019-03-01 03:39:10 +08:00
|
|
|
repository_rows = fetch_rows(search_value)
|
2020-01-13 23:09:07 +08:00
|
|
|
|
|
|
|
# Adding assigned counters
|
2018-03-09 00:36:10 +08:00
|
|
|
if @my_module
|
2020-01-13 23:09:07 +08:00
|
|
|
if @params[:assigned] == 'assigned'
|
|
|
|
repository_rows = repository_rows.joins(:my_module_repository_rows)
|
|
|
|
.where(my_module_repository_rows: { my_module_id: @my_module })
|
|
|
|
else
|
2020-06-24 18:06:53 +08:00
|
|
|
repository_rows = repository_rows
|
|
|
|
.joins(:repository)
|
2020-07-07 20:40:19 +08:00
|
|
|
.joins('LEFT OUTER JOIN "my_module_repository_rows" "current_my_module_repository_rows"'\
|
|
|
|
'ON "current_my_module_repository_rows"."repository_row_id" = "repository_rows"."id" '\
|
|
|
|
'AND "current_my_module_repository_rows"."my_module_id" = ' + @my_module.id.to_s)
|
|
|
|
.where('current_my_module_repository_rows.id IS NOT NULL '\
|
|
|
|
'OR (repository_rows.archived = FALSE AND repositories.archived = FALSE)')
|
|
|
|
.select('CASE WHEN current_my_module_repository_rows.id IS NOT NULL '\
|
|
|
|
'THEN true ELSE false END as row_assigned')
|
|
|
|
.group('current_my_module_repository_rows.id')
|
2020-01-13 23:09:07 +08:00
|
|
|
end
|
2018-03-09 00:36:10 +08:00
|
|
|
end
|
2020-04-08 03:02:16 +08:00
|
|
|
repository_rows = repository_rows
|
|
|
|
.left_outer_joins(my_module_repository_rows: { my_module: :experiment })
|
|
|
|
.select('COUNT(my_module_repository_rows.id) AS "assigned_my_modules_count"')
|
|
|
|
.select('COUNT(DISTINCT my_modules.experiment_id) AS "assigned_experiments_count"')
|
|
|
|
.select('COUNT(DISTINCT experiments.project_id) AS "assigned_projects_count"')
|
2020-01-25 16:43:05 +08:00
|
|
|
repository_rows = repository_rows.preload(Extends::REPOSITORY_ROWS_PRELOAD_RELATIONS)
|
2019-03-01 03:39:10 +08:00
|
|
|
|
2021-02-22 17:34:06 +08:00
|
|
|
sort_rows(order_obj, repository_rows)
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
|
2019-03-01 03:39:10 +08:00
|
|
|
def fetch_rows(search_value)
|
2020-07-10 17:43:06 +08:00
|
|
|
repository_rows = @repository.repository_rows
|
|
|
|
if @params[:archived] && !@repository.archived?
|
|
|
|
repository_rows = repository_rows.where(archived: @params[:archived])
|
|
|
|
end
|
2020-01-13 23:09:07 +08:00
|
|
|
|
|
|
|
@all_count =
|
|
|
|
if @my_module && @params[:assigned] == 'assigned'
|
|
|
|
repository_rows.joins(:my_module_repository_rows)
|
|
|
|
.where(my_module_repository_rows: { my_module_id: @my_module })
|
|
|
|
.count
|
|
|
|
else
|
|
|
|
repository_rows.count
|
|
|
|
end
|
2019-03-01 03:39:10 +08:00
|
|
|
|
|
|
|
if search_value.present?
|
2020-01-13 23:09:07 +08:00
|
|
|
matched_by_user = repository_rows.joins(:created_by).where_attributes_like('users.full_name', search_value)
|
|
|
|
|
2021-07-19 20:23:36 +08:00
|
|
|
repository_row_matches = repository_rows
|
|
|
|
.where_attributes_like(
|
|
|
|
['repository_rows.name', RepositoryRow::PREFIXED_ID_SQL],
|
|
|
|
search_value
|
|
|
|
)
|
2020-01-13 23:09:07 +08:00
|
|
|
results = repository_rows.where(id: repository_row_matches)
|
|
|
|
results = results.or(repository_rows.where(id: matched_by_user))
|
|
|
|
|
2021-05-20 04:48:52 +08:00
|
|
|
data_types = @repository.repository_columns.pluck(:data_type).uniq
|
|
|
|
|
|
|
|
Extends::REPOSITORY_EXTRA_SEARCH_ATTR.each do |data_type, config|
|
|
|
|
next unless data_types.include?(data_type.to_s)
|
|
|
|
|
2021-07-07 23:43:51 +08:00
|
|
|
custom_cell_matches = repository_rows.joins(config[:includes])
|
2021-05-20 04:48:52 +08:00
|
|
|
.where_attributes_like(config[:field], search_value)
|
2020-01-13 23:09:07 +08:00
|
|
|
results = results.or(repository_rows.where(id: custom_cell_matches))
|
|
|
|
end
|
|
|
|
|
|
|
|
repository_rows = results
|
2019-03-01 03:39:10 +08:00
|
|
|
end
|
|
|
|
|
2020-06-11 18:48:31 +08:00
|
|
|
repository_rows.left_outer_joins(:created_by, :archived_by)
|
2020-01-14 23:55:10 +08:00
|
|
|
.select('repository_rows.*')
|
|
|
|
.select('COUNT("repository_rows"."id") OVER() AS filtered_count')
|
|
|
|
.group('repository_rows.id')
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_conditions(params)
|
|
|
|
search_value = params[:search][:value]
|
2018-03-09 00:36:10 +08:00
|
|
|
order = params[:order].values.first
|
|
|
|
order_by_column = { column: order[:column].to_i,
|
|
|
|
dir: order[:dir] }
|
2018-03-06 23:32:39 +08:00
|
|
|
{ search_value: search_value, order_by_column: order_by_column }
|
|
|
|
end
|
|
|
|
|
2020-01-13 23:09:07 +08:00
|
|
|
def build_sortable_columns
|
2018-03-09 00:36:10 +08:00
|
|
|
array = [
|
2018-03-06 23:32:39 +08:00
|
|
|
'assigned',
|
2018-04-04 17:55:11 +08:00
|
|
|
'repository_rows.id',
|
2018-03-09 00:36:10 +08:00
|
|
|
'repository_rows.name',
|
|
|
|
'repository_rows.created_at',
|
2020-06-11 18:48:31 +08:00
|
|
|
'users.full_name',
|
|
|
|
'repository_rows.archived_on',
|
|
|
|
'archived_bies_repository_rows.full_name',
|
2018-03-06 23:32:39 +08:00
|
|
|
]
|
2018-03-09 00:36:10 +08:00
|
|
|
@repository.repository_columns.count.times do
|
|
|
|
array << 'repository_cell.value'
|
|
|
|
end
|
|
|
|
array
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort_rows(column_obj, records)
|
2018-03-16 19:27:19 +08:00
|
|
|
dir = %w(DESC ASC).find do |direction|
|
|
|
|
direction == column_obj[:dir].upcase
|
2018-03-09 21:43:12 +08:00
|
|
|
end || 'ASC'
|
2020-01-13 23:09:07 +08:00
|
|
|
|
2018-03-09 00:36:10 +08:00
|
|
|
column_index = column_obj[:column]
|
2018-04-18 21:53:48 +08:00
|
|
|
service = RepositoryTableStateService.new(@user, @repository)
|
|
|
|
col_order = service.load_state.state['ColReorder']
|
2018-03-09 00:36:10 +08:00
|
|
|
column_id = col_order[column_index].to_i
|
2018-03-06 23:32:39 +08:00
|
|
|
|
2021-07-07 23:43:51 +08:00
|
|
|
case @sortable_columns[column_id - 1]
|
|
|
|
when 'assigned'
|
2018-03-20 00:49:09 +08:00
|
|
|
return records if @my_module && @params[:assigned] == 'assigned'
|
2019-12-18 21:10:41 +08:00
|
|
|
|
2020-01-13 23:09:07 +08:00
|
|
|
records.order("assigned_my_modules_count #{dir}")
|
2021-07-07 23:43:51 +08:00
|
|
|
when 'repository_cell.value'
|
2018-03-09 00:36:10 +08:00
|
|
|
id = @mappings.key(column_id.to_s)
|
2021-07-07 23:43:51 +08:00
|
|
|
sorting_column = @repository.repository_columns.find_by(id: id)
|
2019-06-06 23:28:59 +08:00
|
|
|
return records unless sorting_column
|
|
|
|
|
|
|
|
sorting_data_type = sorting_column.data_type.constantize
|
2021-07-07 23:43:51 +08:00
|
|
|
cells = sorting_data_type.joins(:repository_cell)
|
|
|
|
.where('repository_cells.repository_column_id': sorting_column.id)
|
|
|
|
if sorting_data_type.const_defined?('EXTRA_SORTABLE_VALUE_INCLUDE')
|
|
|
|
cells = cells.joins(sorting_data_type::EXTRA_SORTABLE_VALUE_INCLUDE)
|
|
|
|
end
|
2019-06-06 23:28:59 +08:00
|
|
|
|
2020-04-08 03:02:16 +08:00
|
|
|
cells = if sorting_column.repository_checklist_value?
|
2021-07-07 23:43:51 +08:00
|
|
|
cells
|
|
|
|
.select("repository_cells.repository_row_id, \
|
|
|
|
STRING_AGG(#{sorting_data_type::SORTABLE_COLUMN_NAME}, ' ' \
|
|
|
|
ORDER BY #{sorting_data_type::SORTABLE_COLUMN_NAME}) AS value")
|
|
|
|
.group('repository_cells.repository_row_id')
|
2020-04-08 03:02:16 +08:00
|
|
|
else
|
2021-07-07 23:43:51 +08:00
|
|
|
cells
|
|
|
|
.select("repository_cells.repository_row_id, #{sorting_data_type::SORTABLE_COLUMN_NAME} AS value")
|
2020-04-08 03:02:16 +08:00
|
|
|
end
|
2019-06-06 23:28:59 +08:00
|
|
|
|
|
|
|
records.joins("LEFT OUTER JOIN (#{cells.to_sql}) AS values ON values.repository_row_id = repository_rows.id")
|
2020-01-13 23:09:07 +08:00
|
|
|
.group('values.value')
|
2019-06-06 23:28:59 +08:00
|
|
|
.order("values.value #{dir}")
|
2021-07-07 23:43:51 +08:00
|
|
|
when 'users.full_name'
|
2020-01-13 23:09:07 +08:00
|
|
|
records.group('users.full_name').order("users.full_name #{dir}")
|
2018-03-09 00:36:10 +08:00
|
|
|
else
|
2020-01-13 23:09:07 +08:00
|
|
|
records.group(@sortable_columns[column_id - 1]).order("#{@sortable_columns[column_id - 1]} #{dir}")
|
2018-03-09 00:36:10 +08:00
|
|
|
end
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
end
|