2018-03-06 23:32:39 +08:00
|
|
|
class RepositoryDatatableService
|
|
|
|
|
2018-03-09 21:22:00 +08:00
|
|
|
attr_reader :repository_rows, :assigned_rows, :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
|
2018-03-09 21:22:00 +08:00
|
|
|
create_columns_mappings
|
2018-03-09 00:36:10 +08:00
|
|
|
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
|
2018-04-06 18:03:39 +08:00
|
|
|
index = 6
|
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
|
|
|
order_obj = build_conditions(@params)[:order_by_column]
|
|
|
|
search_value = build_conditions(@params)[:search_value]
|
2018-03-20 00:49:09 +08:00
|
|
|
records = search_value.present? ? search(search_value) : fetch_records
|
|
|
|
@repository_rows = sort_rows(order_obj, records)
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_records
|
2018-12-13 23:17:22 +08:00
|
|
|
repository_rows = RepositoryRow.joins(:created_by)
|
2018-03-09 00:36:10 +08:00
|
|
|
.where(repository: @repository)
|
|
|
|
if @my_module
|
|
|
|
@assigned_rows = @my_module.repository_rows
|
|
|
|
.joins(:created_by)
|
|
|
|
.where(repository: @repository)
|
|
|
|
return @assigned_rows if @params[:assigned] == 'assigned'
|
|
|
|
else
|
2018-03-20 00:49:09 +08:00
|
|
|
@assigned_rows = repository_rows.joins(:my_module_repository_rows)
|
2018-03-09 00:36:10 +08:00
|
|
|
end
|
|
|
|
repository_rows
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def search(value)
|
2018-03-16 19:27:19 +08:00
|
|
|
includes_json = { repository_cells: Extends::REPOSITORY_SEARCH_INCLUDES }
|
2018-04-04 17:55:11 +08:00
|
|
|
searchable_attributes = ['repository_rows.name',
|
|
|
|
'users.full_name',
|
|
|
|
'repository_rows.id'] +
|
2018-03-16 19:27:19 +08:00
|
|
|
Extends::REPOSITORY_EXTRA_SEARCH_ATTR
|
2018-04-06 18:03:39 +08:00
|
|
|
ids = @repository.repository_rows
|
|
|
|
.left_outer_joins(:created_by)
|
|
|
|
.left_outer_joins(includes_json)
|
|
|
|
.where_attributes_like(searchable_attributes, value)
|
|
|
|
.pluck(:id)
|
|
|
|
# using distinct raises an error when combined with sort
|
|
|
|
RepositoryRow.where(id: ids.uniq)
|
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
|
|
|
|
|
|
|
|
def 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',
|
|
|
|
'users.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'
|
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
|
|
|
|
2018-03-09 00:36:10 +08:00
|
|
|
if sortable_columns[column_id - 1] == 'assigned'
|
2018-03-20 00:49:09 +08:00
|
|
|
return records if @my_module && @params[:assigned] == 'assigned'
|
2018-03-09 00:36:10 +08:00
|
|
|
if @my_module
|
|
|
|
# Depending on the sort, order nulls first or
|
|
|
|
# nulls last on repository_cells association
|
|
|
|
return records.joins(
|
|
|
|
"LEFT OUTER JOIN my_module_repository_rows ON
|
2018-03-09 21:43:12 +08:00
|
|
|
(repository_rows.id =
|
|
|
|
my_module_repository_rows.repository_row_id
|
|
|
|
AND (my_module_repository_rows.my_module_id =
|
|
|
|
#{@my_module.id}
|
|
|
|
OR my_module_repository_rows.id IS NULL))"
|
2018-03-09 00:36:10 +08:00
|
|
|
).order(
|
2018-03-09 21:43:12 +08:00
|
|
|
"my_module_repository_rows.id NULLS
|
|
|
|
#{sort_null_direction(dir)}"
|
2018-03-09 00:36:10 +08:00
|
|
|
)
|
|
|
|
else
|
|
|
|
return sort_assigned_records(records, dir)
|
|
|
|
end
|
|
|
|
elsif sortable_columns[column_id - 1] == 'repository_cell.value'
|
|
|
|
id = @mappings.key(column_id.to_s)
|
|
|
|
type = RepositoryColumn.find_by_id(id)
|
|
|
|
return records unless type
|
|
|
|
return select_type(type.data_type, records, id, dir)
|
2018-04-26 17:05:02 +08:00
|
|
|
elsif sortable_columns[column_id - 1] == 'users.full_name'
|
|
|
|
return records.joins(:created_by).order("users.full_name #{dir}")
|
2018-03-09 00:36:10 +08:00
|
|
|
else
|
2018-03-09 21:43:12 +08:00
|
|
|
return records.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
|
|
|
|
|
2018-03-09 00:36:10 +08:00
|
|
|
def sort_assigned_records(records, direction)
|
2018-03-09 21:43:12 +08:00
|
|
|
assigned = records.joins(:my_module_repository_rows)
|
|
|
|
.distinct
|
|
|
|
.pluck(:id)
|
2018-03-09 00:36:10 +08:00
|
|
|
unassigned = records.where.not(id: assigned).pluck(:id)
|
|
|
|
if direction == 'ASC'
|
|
|
|
ids = assigned + unassigned
|
|
|
|
elsif direction == 'DESC'
|
|
|
|
ids = unassigned + assigned
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
2018-03-09 00:36:10 +08:00
|
|
|
|
|
|
|
order_by_index = ActiveRecord::Base.send(
|
|
|
|
:sanitize_sql_array,
|
|
|
|
["position((',' || repository_rows.id || ',') in ?)",
|
|
|
|
ids.join(',') + ',']
|
|
|
|
)
|
|
|
|
records.order(order_by_index)
|
|
|
|
end
|
|
|
|
|
|
|
|
def select_type(type, records, id, dir)
|
2018-03-09 21:43:12 +08:00
|
|
|
case type
|
2018-04-06 18:03:39 +08:00
|
|
|
when 'RepositoryTextValue'
|
|
|
|
filter_by_text_value(records, id, dir)
|
|
|
|
when 'RepositoryListValue'
|
|
|
|
filter_by_list_value(records, id, dir)
|
|
|
|
when 'RepositoryAssetValue'
|
|
|
|
filter_by_asset_value(records, id, dir)
|
|
|
|
else
|
|
|
|
records
|
2018-03-09 21:43:12 +08:00
|
|
|
end
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
|
2018-03-09 00:36:10 +08:00
|
|
|
def sort_null_direction(val)
|
|
|
|
val == 'ASC' ? 'LAST' : 'FIRST'
|
|
|
|
end
|
|
|
|
|
2018-04-06 18:03:39 +08:00
|
|
|
def filter_by_asset_value(records, id, dir)
|
|
|
|
records.joins(
|
|
|
|
"LEFT OUTER JOIN (SELECT repository_cells.repository_row_id,
|
|
|
|
assets.file_file_name AS value
|
|
|
|
FROM repository_cells
|
|
|
|
INNER JOIN repository_asset_values
|
|
|
|
ON repository_asset_values.id = repository_cells.value_id
|
|
|
|
INNER JOIN assets
|
|
|
|
ON repository_asset_values.asset_id = assets.id
|
|
|
|
WHERE repository_cells.repository_column_id = #{id}) AS values
|
|
|
|
ON values.repository_row_id = repository_rows.id"
|
|
|
|
).order("values.value #{dir}")
|
|
|
|
end
|
|
|
|
|
2018-03-09 00:36:10 +08:00
|
|
|
def filter_by_text_value(records, id, dir)
|
2018-03-09 21:43:12 +08:00
|
|
|
records.joins(
|
2018-03-09 00:36:10 +08:00
|
|
|
"LEFT OUTER JOIN (SELECT repository_cells.repository_row_id,
|
|
|
|
repository_text_values.data AS value
|
|
|
|
FROM repository_cells
|
|
|
|
INNER JOIN repository_text_values
|
|
|
|
ON repository_text_values.id = repository_cells.value_id
|
|
|
|
WHERE repository_cells.repository_column_id = #{id}) AS values
|
|
|
|
ON values.repository_row_id = repository_rows.id"
|
|
|
|
).order("values.value #{dir}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_list_value(records, id, dir)
|
2018-03-09 21:43:12 +08:00
|
|
|
records.joins(
|
2018-03-09 00:36:10 +08:00
|
|
|
"LEFT OUTER JOIN (SELECT repository_cells.repository_row_id,
|
|
|
|
repository_list_items.data AS value
|
|
|
|
FROM repository_cells
|
|
|
|
INNER JOIN repository_list_values
|
|
|
|
ON repository_list_values.id = repository_cells.value_id
|
|
|
|
INNER JOIN repository_list_items
|
|
|
|
ON repository_list_values.repository_list_item_id =
|
|
|
|
repository_list_items.id
|
|
|
|
WHERE repository_cells.repository_column_id = #{id}) AS values
|
|
|
|
ON values.repository_row_id = repository_rows.id"
|
|
|
|
).order("values.value #{dir}")
|
|
|
|
end
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|