summaryrefslogtreecommitdiff
path: root/app/models/label.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/label.rb')
-rw-r--r--app/models/label.rb23
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