2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-05 17:20:55 -05:00
|
|
|
# == Sortable concern
|
|
|
|
#
|
|
|
|
# Set default scope for ordering objects
|
|
|
|
#
|
|
|
|
module Sortable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2018-08-06 15:38:37 -04:00
|
|
|
scope :with_order_id_desc, -> { order(id: :desc) }
|
2015-11-12 10:30:39 -05:00
|
|
|
scope :order_id_desc, -> { reorder(id: :desc) }
|
2016-01-06 11:29:44 -05:00
|
|
|
scope :order_id_asc, -> { reorder(id: :asc) }
|
2015-11-03 05:54:43 -05:00
|
|
|
scope :order_created_desc, -> { reorder(created_at: :desc) }
|
|
|
|
scope :order_created_asc, -> { reorder(created_at: :asc) }
|
|
|
|
scope :order_updated_desc, -> { reorder(updated_at: :desc) }
|
|
|
|
scope :order_updated_asc, -> { reorder(updated_at: :asc) }
|
2018-06-25 15:46:45 -04:00
|
|
|
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-02-05 17:20:55 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 08:35:31 -04:00
|
|
|
class_methods do
|
2015-02-05 19:49:41 -05:00
|
|
|
def order_by(method)
|
2015-02-05 17:20:55 -05:00
|
|
|
case method.to_s
|
2017-09-22 20:46:53 -04:00
|
|
|
when 'created_asc' then order_created_asc
|
|
|
|
when 'created_date' then order_created_desc
|
2015-02-05 22:15:05 -05:00
|
|
|
when 'created_desc' then order_created_desc
|
2017-09-22 20:46:53 -04:00
|
|
|
when 'id_asc' then order_id_asc
|
|
|
|
when 'id_desc' then order_id_desc
|
|
|
|
when 'name_asc' then order_name_asc
|
|
|
|
when 'name_desc' then order_name_desc
|
|
|
|
when 'updated_asc' then order_updated_asc
|
|
|
|
when 'updated_desc' then order_updated_desc
|
2015-02-05 17:20:55 -05:00
|
|
|
else
|
2015-02-05 19:49:41 -05:00
|
|
|
all
|
2015-02-05 17:20:55 -05:00
|
|
|
end
|
|
|
|
end
|
2016-07-26 17:21:20 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-10-27 20:26:56 -04:00
|
|
|
def highest_label_priority(target_type_column: nil, target_type: nil, target_column:, project_column:, excluded_labels: [])
|
2017-06-21 09:48:12 -04:00
|
|
|
query = Label.select(LabelPriority.arel_table[:priority].minimum)
|
|
|
|
.left_join_priorities
|
|
|
|
.joins(:label_links)
|
|
|
|
.where("label_priorities.project_id = #{project_column}")
|
|
|
|
.where("label_links.target_id = #{target_column}")
|
|
|
|
.reorder(nil)
|
2016-07-26 17:21:20 -04:00
|
|
|
|
2017-02-22 10:10:32 -05:00
|
|
|
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-10-27 20:26:56 -04:00
|
|
|
|
|
|
|
query = query.where.not(title: excluded_labels) if excluded_labels.present?
|
2016-07-26 17:21:20 -04:00
|
|
|
|
|
|
|
query
|
|
|
|
end
|
2015-02-05 17:20:55 -05:00
|
|
|
end
|
|
|
|
end
|