diff options
author | Rémy Coutable <remy@rymai.me> | 2016-05-13 17:26:18 +0200 |
---|---|---|
committer | Alfredo Sumaran <alfredo@gitlab.com> | 2016-06-06 11:59:49 -0500 |
commit | 499bb9f305e78d0e3488c2eee6328ce76af39920 (patch) | |
tree | ffc4aa229ae08c995afabf893ed36167c950302e /app/models | |
parent | d8263b285193d9163089683eb77825f1cd673b14 (diff) | |
download | gitlab-ce-499bb9f305e78d0e3488c2eee6328ce76af39920.tar.gz |
Improve Issuable.order_labels_priority
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/concerns/issuable.rb | 30 | ||||
-rw-r--r-- | app/models/issue.rb | 4 | ||||
-rw-r--r-- | app/models/label.rb | 25 |
3 files changed, 30 insertions, 29 deletions
diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb index 8871cb8a6cd..92526a99147 100644 --- a/app/models/concerns/issuable.rb +++ b/app/models/concerns/issuable.rb @@ -48,12 +48,6 @@ 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, @@ -111,18 +105,24 @@ module Issuable where(t[:title].matches(pattern).or(t[:description].matches(pattern))) end - def sort(method, labels = []) + def sort(method, excluded_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) + when 'priority' then order_labels_priority(excluded_labels: excluded_labels) else order_by(method) end end + def order_labels_priority(excluded_labels: []) + select("#{table_name}.*, (#{highest_label_priority(excluded_labels).to_sql}) AS highest_priority"). + group(arel_table[:id]). + reorder(Gitlab::Database.nulls_last_order('highest_priority', 'ASC')) + end + def with_label(title, sort = nil) if title.is_a?(Array) && title.size > 1 joins(:labels).where(labels: { title: title }).group(*grouping_columns(sort)).having("COUNT(DISTINCT labels.title) = #{title.size}") @@ -146,6 +146,20 @@ module Issuable grouping_columns end + + private + + def highest_label_priority(excluded_labels) + query = Label.select(Label.arel_table[:priority].minimum). + joins(:label_links). + where(label_links: { target_type: name }). + where("label_links.target_id = #{table_name}.id"). + reorder(nil) + + query.where.not(title: excluded_labels) if excluded_labels.present? + + query + end end def today? diff --git a/app/models/issue.rb b/app/models/issue.rb index bd0fbc96d18..235922710ad 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -75,10 +75,10 @@ class Issue < ActiveRecord::Base @link_reference_pattern ||= super("issues", /(?<issue>\d+)/) end - def self.sort(method) + def self.sort(method, excluded_labels: []) case method.to_s when 'due_date_asc' then order_due_date_asc - when 'due_date_desc' then order_due_date_desc + when 'due_date_desc' then order_due_date_desc else super end diff --git a/app/models/label.rb b/app/models/label.rb index 4437ca393ed..7fd77880558 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -19,7 +19,6 @@ class Label < ActiveRecord::Base validates :color, color: true, allow_blank: false validates :project, presence: true, unless: Proc.new { |service| service.template? } - validates :priority, presence: false, default: false # Don't allow '?', '&', and ',' for label titles validates :title, @@ -32,21 +31,11 @@ class Label < ActiveRecord::Base 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 + + def self.prioritized(bool = true) + query = bool ? where.not(priority: nil) : where(priority: nil) + + query.reorder(Gitlab::Database.nulls_last_order(:priority), :title) end alias_attribute :name, :title @@ -139,8 +128,6 @@ class Label < ActiveRecord::Base end def nillify_priority - unless self.priority.present? - self.priority = nil - end + self.priority = nil if priority.blank? end end |