summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorThijs Wouters <thijs@morewood.be>2016-03-14 10:46:26 +0100
committerAlfredo Sumaran <alfredo@gitlab.com>2016-06-06 11:59:49 -0500
commitd8263b285193d9163089683eb77825f1cd673b14 (patch)
tree4ecdabc6af9a09556fdb212488d381b43d20ae10 /app/models
parent0e2f26dd2a10ed876f96b0496dff2de6780eeaea (diff)
downloadgitlab-ce-d8263b285193d9163089683eb77825f1cd673b14.tar.gz
Sort by label priority
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'app/models')
-rw-r--r--app/models/concerns/issuable.rb9
-rw-r--r--app/models/label.rb23
2 files changed, 31 insertions, 1 deletions
diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb
index 5d279ae602a..8871cb8a6cd 100644
--- a/app/models/concerns/issuable.rb
+++ b/app/models/concerns/issuable.rb
@@ -48,6 +48,12 @@ module Issuable
scope :non_archived, -> { join_project.where(projects: { archived: false }) }
+ def self.order_priority(labels)
+ select("#{table_name}.*, (#{Label.high_priority(name, table_name, labels).to_sql}) AS highest_priority")
+ .group("#{table_name}.id")
+ .reorder(nulls_last('highest_priority', 'ASC'))
+ end
+
delegate :name,
:email,
to: :author,
@@ -105,12 +111,13 @@ module Issuable
where(t[:title].matches(pattern).or(t[:description].matches(pattern)))
end
- def sort(method)
+ def sort(method, labels = [])
case method.to_s
when 'milestone_due_asc' then order_milestone_due_asc
when 'milestone_due_desc' then order_milestone_due_desc
when 'downvotes_desc' then order_downvotes_desc
when 'upvotes_desc' then order_upvotes_desc
+ when 'priority' then order_priority(labels)
else
order_by(method)
end
diff --git a/app/models/label.rb b/app/models/label.rb
index 59e7afe53f3..4437ca393ed 100644
--- a/app/models/label.rb
+++ b/app/models/label.rb
@@ -27,11 +27,28 @@ class Label < ActiveRecord::Base
format: { with: /\A[^&\?,]+\z/ },
uniqueness: { scope: :project_id }
+ before_save :nillify_priority
+
default_scope { order(title: :asc) }
scope :templates, -> { where(template: true) }
scope :prioritized, ->(value = true) { where(priority: value) }
+ def self.high_priority(name, table_name, labels)
+ unfiltered = unscoped
+ .select("MIN(labels.priority)")
+ .joins("INNER JOIN label_links ON label_links.label_id = labels.id")
+ .where("label_links.target_type = '#{name}'")
+ .where("label_links.target_id = #{table_name}.id")
+ .where("labels.project_id = #{table_name}.project_id")
+
+ if labels.empty?
+ unfiltered
+ else
+ unfiltered.where("labels.title NOT IN (?)", labels)
+ end
+ end
+
alias_attribute :name, :title
def self.reference_prefix
@@ -120,4 +137,10 @@ class Label < ActiveRecord::Base
id
end
end
+
+ def nillify_priority
+ unless self.priority.present?
+ self.priority = nil
+ end
+ end
end