Commit dbc1991b authored by Robert Speicher's avatar Robert Speicher

Merge branch '673-fix-sort-by-weight' into 'master'

Fix Error 500 occuring when sorting merge requests by "More weight" or "Less weight"

This MR moves weight scopes & sort methods to `Issue`.

This ensure `MergeRequest` won't try to sort by weight, which would fail
since it doesn't have weight.

It also show the "sort by weight" links only when in the context of issues.

See merge request !483
parents 866ea74d dc77c401
...@@ -45,8 +45,6 @@ module Issuable ...@@ -45,8 +45,6 @@ module Issuable
scope :order_milestone_due_desc, -> { outer_join_milestone.reorder('milestones.due_date IS NULL ASC, milestones.due_date DESC, milestones.id DESC') } scope :order_milestone_due_desc, -> { outer_join_milestone.reorder('milestones.due_date IS NULL ASC, milestones.due_date DESC, milestones.id DESC') }
scope :order_milestone_due_asc, -> { outer_join_milestone.reorder('milestones.due_date IS NULL ASC, milestones.due_date ASC, milestones.id ASC') } scope :order_milestone_due_asc, -> { outer_join_milestone.reorder('milestones.due_date IS NULL ASC, milestones.due_date ASC, milestones.id ASC') }
scope :without_label, -> { joins("LEFT OUTER JOIN label_links ON label_links.target_type = '#{name}' AND label_links.target_id = #{table_name}.id").where(label_links: { id: nil }) } scope :without_label, -> { joins("LEFT OUTER JOIN label_links ON label_links.target_type = '#{name}' AND label_links.target_id = #{table_name}.id").where(label_links: { id: nil }) }
scope :order_weight_desc, -> { reorder('weight IS NOT NULL, weight DESC') }
scope :order_weight_asc, -> { reorder('weight ASC') }
scope :left_joins_milestones, -> { joins("LEFT OUTER JOIN milestones ON #{table_name}.milestone_id = milestones.id") } scope :left_joins_milestones, -> { joins("LEFT OUTER JOIN milestones ON #{table_name}.milestone_id = milestones.id") }
scope :order_milestone_due_desc, -> { left_joins_milestones.reorder('milestones.due_date IS NULL, milestones.id IS NULL, milestones.due_date DESC') } scope :order_milestone_due_desc, -> { left_joins_milestones.reorder('milestones.due_date IS NULL, milestones.id IS NULL, milestones.due_date DESC') }
...@@ -122,8 +120,6 @@ module Issuable ...@@ -122,8 +120,6 @@ module Issuable
when 'milestone_due_desc' then order_milestone_due_desc when 'milestone_due_desc' then order_milestone_due_desc
when 'downvotes_desc' then order_downvotes_desc when 'downvotes_desc' then order_downvotes_desc
when 'upvotes_desc' then order_upvotes_desc when 'upvotes_desc' then order_upvotes_desc
when 'weight_desc' then order_weight_desc
when 'weight_asc' then order_weight_asc
when 'priority' then order_labels_priority(excluded_labels: excluded_labels) when 'priority' then order_labels_priority(excluded_labels: excluded_labels)
else else
order_by(method) order_by(method)
......
...@@ -37,6 +37,8 @@ class Issue < ActiveRecord::Base ...@@ -37,6 +37,8 @@ class Issue < ActiveRecord::Base
scope :order_due_date_asc, -> { reorder('issues.due_date IS NULL, issues.due_date ASC') } scope :order_due_date_asc, -> { reorder('issues.due_date IS NULL, issues.due_date ASC') }
scope :order_due_date_desc, -> { reorder('issues.due_date IS NULL, issues.due_date DESC') } scope :order_due_date_desc, -> { reorder('issues.due_date IS NULL, issues.due_date DESC') }
scope :order_weight_desc, -> { reorder('weight IS NOT NULL, weight DESC') }
scope :order_weight_asc, -> { reorder('weight ASC') }
state_machine :state, initial: :opened do state_machine :state, initial: :opened do
event :close do event :close do
...@@ -93,6 +95,8 @@ class Issue < ActiveRecord::Base ...@@ -93,6 +95,8 @@ class Issue < ActiveRecord::Base
case method.to_s case method.to_s
when 'due_date_asc' then order_due_date_asc 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
when 'weight_desc' then order_weight_desc
when 'weight_asc' then order_weight_asc
else else
super super
end end
......
...@@ -18,10 +18,11 @@ ...@@ -18,10 +18,11 @@
= sort_title_recently_updated = sort_title_recently_updated
= link_to page_filter_path(sort: sort_value_oldest_updated) do = link_to page_filter_path(sort: sort_value_oldest_updated) do
= sort_title_oldest_updated = sort_title_oldest_updated
= link_to page_filter_path(sort: sort_value_more_weight) do - if local_assigns[:type] == :issues
= sort_title_more_weight = link_to page_filter_path(sort: sort_value_more_weight) do
= link_to page_filter_path(sort: sort_value_less_weight) do = sort_title_more_weight
= sort_title_less_weight = link_to page_filter_path(sort: sort_value_less_weight) do
= sort_title_less_weight
= link_to page_filter_path(sort: sort_value_milestone_soon) do = link_to page_filter_path(sort: sort_value_milestone_soon) do
= sort_title_milestone_soon = sort_title_milestone_soon
= link_to page_filter_path(sort: sort_value_milestone_later) do = link_to page_filter_path(sort: sort_value_milestone_later) do
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
.filter-item.inline.labels-filter .filter-item.inline.labels-filter
= render "shared/issuable/label_dropdown" = render "shared/issuable/label_dropdown"
- if controller.controller_name == 'issues' - if local_assigns[:type] == :issues
.filter-item.inline.weight-filter .filter-item.inline.weight-filter
- if params[:weight] - if params[:weight]
= hidden_field_tag(:weight, params[:weight]) = hidden_field_tag(:weight, params[:weight])
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
= weight = weight
.pull-right .pull-right
= render 'shared/sort_dropdown' = render 'shared/sort_dropdown', type: local_assigns[:type]
- if controller.controller_name == 'issues' - if controller.controller_name == 'issues'
.issues_bulk_update.hide .issues_bulk_update.hide
......
require 'spec_helper'
describe 'Issue sorting by Weight', feature: true do
include SortingHelper
let(:project) { create(:project, :public) }
let(:foo) { create(:issue, title: 'foo', project: project) }
let(:bar) { create(:issue, title: 'bar', project: project) }
before do
login_as :user
end
describe 'sorting by weight' do
before do
foo.update(weight: 5)
bar.update(weight: 10)
end
it 'sorts by more weight' do
visit namespace_project_issues_path(project.namespace, project, sort: sort_value_more_weight)
expect(first_issue).to include('bar')
end
it 'sorts by less weight' do
visit namespace_project_issues_path(project.namespace, project, sort: sort_value_less_weight)
expect(first_issue).to include('foo')
end
end
def first_issue
page.all('ul.issues-list > li').first.text
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment