2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
# == Sortable concern
|
|
|
|
#
|
|
|
|
# Set default scope for ordering objects
|
|
|
|
#
|
|
|
|
module Sortable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2020-03-13 15:44:24 +05:30
|
|
|
scope :with_order_id_desc, -> { order(self.arel_table['id'].desc) }
|
2021-04-29 21:17:54 +05:30
|
|
|
scope :with_order_id_asc, -> { order(self.arel_table['id'].asc) }
|
2020-03-13 15:44:24 +05:30
|
|
|
scope :order_id_desc, -> { reorder(self.arel_table['id'].desc) }
|
|
|
|
scope :order_id_asc, -> { reorder(self.arel_table['id'].asc) }
|
|
|
|
scope :order_created_desc, -> { reorder(self.arel_table['created_at'].desc) }
|
|
|
|
scope :order_created_asc, -> { reorder(self.arel_table['created_at'].asc) }
|
|
|
|
scope :order_updated_desc, -> { reorder(self.arel_table['updated_at'].desc) }
|
|
|
|
scope :order_updated_asc, -> { reorder(self.arel_table['updated_at'].asc) }
|
2018-11-08 19:23:39 +05:30
|
|
|
scope :order_name_asc, -> { reorder(Arel::Nodes::Ascending.new(arel_table[:name].lower)) }
|
|
|
|
scope :order_name_desc, -> { reorder(Arel::Nodes::Descending.new(arel_table[:name].lower)) }
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
class_methods do
|
2015-04-26 12:48:37 +05:30
|
|
|
def order_by(method)
|
2019-07-07 11:18:12 +05:30
|
|
|
simple_sorts.fetch(method.to_s, -> { all }).call
|
|
|
|
end
|
|
|
|
|
|
|
|
def simple_sorts
|
|
|
|
{
|
|
|
|
'created_asc' => -> { order_created_asc },
|
2019-12-04 20:38:33 +05:30
|
|
|
'created_at_asc' => -> { order_created_asc },
|
2019-07-07 11:18:12 +05:30
|
|
|
'created_date' => -> { order_created_desc },
|
|
|
|
'created_desc' => -> { order_created_desc },
|
2019-12-04 20:38:33 +05:30
|
|
|
'created_at_desc' => -> { order_created_desc },
|
2019-07-07 11:18:12 +05:30
|
|
|
'id_asc' => -> { order_id_asc },
|
|
|
|
'id_desc' => -> { order_id_desc },
|
|
|
|
'name_asc' => -> { order_name_asc },
|
|
|
|
'name_desc' => -> { order_name_desc },
|
|
|
|
'updated_asc' => -> { order_updated_asc },
|
2019-12-04 20:38:33 +05:30
|
|
|
'updated_at_asc' => -> { order_updated_asc },
|
|
|
|
'updated_desc' => -> { order_updated_desc },
|
|
|
|
'updated_at_desc' => -> { order_updated_desc }
|
2019-07-07 11:18:12 +05:30
|
|
|
}
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
def build_keyset_order_on_joined_column(scope:, attribute_name:, column:, direction:, nullable:)
|
|
|
|
reversed_direction = direction == :asc ? :desc : :asc
|
|
|
|
|
|
|
|
# rubocop: disable GitlabSecurity/PublicSend
|
|
|
|
order = ::Gitlab::Pagination::Keyset::Order.build(
|
|
|
|
[
|
|
|
|
::Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
|
|
|
|
attribute_name: attribute_name,
|
|
|
|
column_expression: column,
|
|
|
|
order_expression: column.send(direction).send(nullable),
|
|
|
|
reversed_order_expression: column.send(reversed_direction).send(nullable),
|
|
|
|
order_direction: direction,
|
|
|
|
distinct: false,
|
|
|
|
add_to_projections: true,
|
|
|
|
nullable: nullable
|
|
|
|
),
|
|
|
|
::Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
|
|
|
|
attribute_name: 'id',
|
|
|
|
order_expression: arel_table['id'].desc
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
# rubocop: enable GitlabSecurity/PublicSend
|
|
|
|
|
|
|
|
order.apply_cursor_conditions(scope).reorder(order)
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
private
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
def highest_label_priority(target_column:, project_column:, target_type_column: nil, target_type: nil, excluded_labels: [])
|
2021-09-30 23:02:18 +05:30
|
|
|
query = Label.select(LabelPriority.arel_table[:priority].minimum.as('label_priority'))
|
2017-09-10 17:25:29 +05:30
|
|
|
.left_join_priorities
|
|
|
|
.joins(:label_links)
|
|
|
|
.where("label_priorities.project_id = #{project_column}")
|
|
|
|
.where("label_links.target_id = #{target_column}")
|
|
|
|
.reorder(nil)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
query =
|
|
|
|
if target_type_column
|
|
|
|
query.where("label_links.target_type = #{target_type_column}")
|
|
|
|
else
|
|
|
|
query.where(label_links: { target_type: target_type })
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
query = query.where.not(title: excluded_labels) if excluded_labels.present?
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
query
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|