2015-04-26 12:48:37 +05:30
|
|
|
# == Sortable concern
|
|
|
|
#
|
|
|
|
# Set default scope for ordering objects
|
|
|
|
#
|
|
|
|
module Sortable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2015-11-26 14:37:03 +05:30
|
|
|
scope :order_id_desc, -> { reorder(id: :desc) }
|
2016-01-14 18:37:52 +05:30
|
|
|
scope :order_id_asc, -> { reorder(id: :asc) }
|
2015-11-26 14:37:03 +05:30
|
|
|
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) }
|
2015-04-26 12:48:37 +05:30
|
|
|
scope :order_name_asc, -> { reorder(name: :asc) }
|
|
|
|
scope :order_name_desc, -> { reorder(name: :desc) }
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def order_by(method)
|
|
|
|
case method.to_s
|
2018-03-17 18:26:18 +05:30
|
|
|
when 'created_asc' then order_created_asc
|
|
|
|
when 'created_date' then order_created_desc
|
2015-04-26 12:48:37 +05:30
|
|
|
when 'created_desc' then order_created_desc
|
2018-03-17 18:26:18 +05:30
|
|
|
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-04-26 12:48:37 +05:30
|
|
|
else
|
|
|
|
all
|
|
|
|
end
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def highest_label_priority(target_type_column: nil, target_type: nil, target_column:, project_column:, excluded_labels: [])
|
2017-09-10 17:25:29 +05:30
|
|
|
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-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
|