diff options
author | Thijs Wouters <thijs@morewood.be> | 2016-03-14 10:46:26 +0100 |
---|---|---|
committer | Alfredo Sumaran <alfredo@gitlab.com> | 2016-06-06 11:59:49 -0500 |
commit | d8263b285193d9163089683eb77825f1cd673b14 (patch) | |
tree | 4ecdabc6af9a09556fdb212488d381b43d20ae10 /app/models/label.rb | |
parent | 0e2f26dd2a10ed876f96b0496dff2de6780eeaea (diff) | |
download | gitlab-ce-d8263b285193d9163089683eb77825f1cd673b14.tar.gz |
Sort by label priority
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'app/models/label.rb')
-rw-r--r-- | app/models/label.rb | 23 |
1 files changed, 23 insertions, 0 deletions
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 |