diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-10-14 20:06:26 -0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-10-19 14:58:27 -0200 |
commit | 3c2aaec1f2624ad4817e7ac52120985682afa448 (patch) | |
tree | 45832f21636e5a33557f715a5d95c36c6be290a9 /app/models | |
parent | 99e928f103182b58156edb107b55344eaafc6772 (diff) | |
download | gitlab-ce-3c2aaec1f2624ad4817e7ac52120985682afa448.tar.gz |
Fix sorting by label priorities
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/concerns/issuable.rb | 3 | ||||
-rw-r--r-- | app/models/concerns/sortable.rb | 6 | ||||
-rw-r--r-- | app/models/label.rb | 11 | ||||
-rw-r--r-- | app/models/todo.rb | 2 |
4 files changed, 18 insertions, 4 deletions
diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb index 76de927ceab..d726cb6b7aa 100644 --- a/app/models/concerns/issuable.rb +++ b/app/models/concerns/issuable.rb @@ -146,7 +146,8 @@ module Issuable def order_labels_priority(excluded_labels: []) condition_field = "#{table_name}.id" - highest_priority = highest_label_priority(name, condition_field, excluded_labels: excluded_labels).to_sql + project_field = "#{table_name}.project_id" + highest_priority = highest_label_priority(name, project_field, condition_field, excluded_labels: excluded_labels).to_sql select("#{table_name}.*, (#{highest_priority}) AS highest_priority"). group(arel_table[:id]). diff --git a/app/models/concerns/sortable.rb b/app/models/concerns/sortable.rb index 1ebecd86af9..83e551fd152 100644 --- a/app/models/concerns/sortable.rb +++ b/app/models/concerns/sortable.rb @@ -38,10 +38,12 @@ module Sortable private - def highest_label_priority(object_types, condition_field, excluded_labels: []) - query = Label.select(Label.arel_table[:priority].minimum). + def highest_label_priority(object_types, project_field, condition_field, excluded_labels: []) + query = Label.select(LabelPriority.arel_table[:priority].minimum). + left_join_priorities. joins(:label_links). where(label_links: { target_type: object_types }). + where("label_priorities.project_id = #{project_field}"). where("label_links.target_id = #{condition_field}"). reorder(nil) diff --git a/app/models/label.rb b/app/models/label.rb index ea11d9d7864..1d775a83f96 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -42,6 +42,17 @@ class Label < ActiveRecord::Base where.not(id: prioritized(project).select(:id)) end + def self.left_join_priorities + labels = Label.arel_table + priorities = LabelPriority.arel_table + + label_priorities = labels.join(priorities, Arel::Nodes::OuterJoin). + on(labels[:id].eq(priorities[:label_id])). + join_sources + + joins(label_priorities) + end + alias_attribute :name, :title def self.reference_prefix diff --git a/app/models/todo.rb b/app/models/todo.rb index 6ae9956ade5..fd90a893d2e 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -52,7 +52,7 @@ class Todo < ActiveRecord::Base # Todos with highest priority first then oldest todos # Need to order by created_at last because of differences on Mysql and Postgres when joining by type "Merge_request/Issue" def order_by_labels_priority - highest_priority = highest_label_priority(["Issue", "MergeRequest"], "todos.target_id").to_sql + highest_priority = highest_label_priority(["Issue", "MergeRequest"], "todos.project_id", "todos.target_id").to_sql select("#{table_name}.*, (#{highest_priority}) AS highest_priority"). order(Gitlab::Database.nulls_last_order('highest_priority', 'ASC')). |