Commit baf5363d authored by Tiger Watson's avatar Tiger Watson

Merge branch 'improve-sort-by-popolarity-issues' into 'master'

Improve sort by popularity for issues board

See merge request gitlab-org/gitlab!66140
parents 0a8c6177 e87edb6b
...@@ -196,11 +196,23 @@ class Issue < ApplicationRecord ...@@ -196,11 +196,23 @@ class Issue < ApplicationRecord
end end
end end
# Alias to state machine .with_state_id method
# This needs to be defined after the state machine block to avoid errors
class << self class << self
extend ::Gitlab::Utils::Override
# Alias to state machine .with_state_id method
# This needs to be defined after the state machine block to avoid errors
alias_method :with_state, :with_state_id alias_method :with_state, :with_state_id
alias_method :with_states, :with_state_ids alias_method :with_states, :with_state_ids
override :order_upvotes_desc
def order_upvotes_desc
reorder(upvotes_count: :desc)
end
override :order_upvotes_asc
def order_upvotes_asc
reorder(upvotes_count: :asc)
end
end end
def self.relative_positioning_query_base(issue) def self.relative_positioning_query_base(issue)
......
...@@ -3,64 +3,64 @@ ...@@ -3,64 +3,64 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Awardable do RSpec.describe Awardable do
let!(:issue) { create(:issue) } let!(:note) { create(:note) }
let!(:award_emoji) { create(:award_emoji, :downvote, awardable: issue) } let!(:award_emoji) { create(:award_emoji, :downvote, awardable: note) }
describe "Associations" do describe "Associations" do
subject { build(:issue) } subject { build(:note) }
it { is_expected.to have_many(:award_emoji).dependent(:destroy) } it { is_expected.to have_many(:award_emoji).dependent(:destroy) }
end end
describe "ClassMethods" do describe "ClassMethods" do
let!(:issue2) { create(:issue) } let!(:note2) { create(:note) }
let!(:award_emoji2) { create(:award_emoji, awardable: issue2) } let!(:award_emoji2) { create(:award_emoji, awardable: note2) }
describe "orders" do describe "orders" do
it "orders on upvotes" do it "orders on upvotes" do
expect(Issue.order_upvotes_desc.to_a).to eq [issue2, issue] expect(Note.order_upvotes_desc.to_a).to eq [note2, note]
end end
it "orders on downvotes" do it "orders on downvotes" do
expect(Issue.order_downvotes_desc.to_a).to eq [issue, issue2] expect(Note.order_downvotes_desc.to_a).to eq [note, note2]
end end
end end
describe "#awarded" do describe "#awarded" do
it "filters by user and emoji name" do it "filters by user and emoji name" do
expect(Issue.awarded(award_emoji.user, "thumbsup")).to be_empty expect(Note.awarded(award_emoji.user, "thumbsup")).to be_empty
expect(Issue.awarded(award_emoji.user, "thumbsdown")).to eq [issue] expect(Note.awarded(award_emoji.user, "thumbsdown")).to eq [note]
expect(Issue.awarded(award_emoji2.user, "thumbsup")).to eq [issue2] expect(Note.awarded(award_emoji2.user, "thumbsup")).to eq [note2]
expect(Issue.awarded(award_emoji2.user, "thumbsdown")).to be_empty expect(Note.awarded(award_emoji2.user, "thumbsdown")).to be_empty
end end
it "filters by user and any emoji" do it "filters by user and any emoji" do
issue3 = create(:issue) note3 = create(:note)
create(:award_emoji, awardable: issue3, name: "star", user: award_emoji.user) create(:award_emoji, awardable: note3, name: "star", user: award_emoji.user)
create(:award_emoji, awardable: issue3, name: "star", user: award_emoji2.user) create(:award_emoji, awardable: note3, name: "star", user: award_emoji2.user)
expect(Issue.awarded(award_emoji.user)).to contain_exactly(issue, issue3) expect(Note.awarded(award_emoji.user)).to contain_exactly(note, note3)
expect(Issue.awarded(award_emoji2.user)).to contain_exactly(issue2, issue3) expect(Note.awarded(award_emoji2.user)).to contain_exactly(note2, note3)
end end
end end
describe "#not_awarded" do describe "#not_awarded" do
it "returns issues not awarded by user" do it "returns notes not awarded by user" do
expect(Issue.not_awarded(award_emoji.user)).to eq [issue2] expect(Note.not_awarded(award_emoji.user)).to eq [note2]
expect(Issue.not_awarded(award_emoji2.user)).to eq [issue] expect(Note.not_awarded(award_emoji2.user)).to eq [note]
end end
end end
end end
describe "#upvotes" do describe "#upvotes" do
it "counts the number of upvotes" do it "counts the number of upvotes" do
expect(issue.upvotes).to be 0 expect(note.upvotes).to be 0
end end
end end
describe "#downvotes" do describe "#downvotes" do
it "counts the number of downvotes" do it "counts the number of downvotes" do
expect(issue.downvotes).to be 1 expect(note.downvotes).to be 1
end end
end end
...@@ -68,67 +68,67 @@ RSpec.describe Awardable do ...@@ -68,67 +68,67 @@ RSpec.describe Awardable do
let(:user) { create(:user) } let(:user) { create(:user) }
before do before do
issue.project.add_guest(user) note.project.add_guest(user)
end end
it 'is truthy when the user is allowed to award emoji' do it 'is truthy when the user is allowed to award emoji' do
expect(issue.user_can_award?(user)).to be_truthy expect(note.user_can_award?(user)).to be_truthy
end end
it 'is falsy when the project is archived' do it 'is falsy when the project is archived' do
issue.project.update!(archived: true) note.project.update!(archived: true)
expect(issue.user_can_award?(user)).to be_falsy expect(note.user_can_award?(user)).to be_falsy
end end
end end
describe 'querying award_emoji on an Awardable' do describe 'querying award_emoji on an Awardable' do
let(:issue) { create(:issue) } let(:note) { create(:note) }
it 'sorts in ascending fashion' do it 'sorts in ascending fashion' do
create_list(:award_emoji, 3, awardable: issue) create_list(:award_emoji, 3, awardable: note)
expect(issue.award_emoji).to eq issue.award_emoji.sort_by(&:id) expect(note.award_emoji).to eq note.award_emoji.sort_by(&:id)
end end
end end
describe "#grouped_awards" do describe "#grouped_awards" do
context 'default award emojis' do context 'default award emojis' do
let(:issue_without_downvote) { create(:issue) } let(:note_without_downvote) { create(:note) }
let(:issue_with_downvote) do let(:note_with_downvote) do
issue_with_downvote = create(:issue) note_with_downvote = create(:note)
create(:award_emoji, :downvote, awardable: issue_with_downvote) create(:award_emoji, :downvote, awardable: note_with_downvote)
issue_with_downvote note_with_downvote
end end
it "includes unused thumbs buttons by default" do it "includes unused thumbs buttons by default" do
expect(issue_without_downvote.grouped_awards.keys.sort).to eq %w(thumbsdown thumbsup) expect(note_without_downvote.grouped_awards.keys.sort).to eq %w(thumbsdown thumbsup)
end end
it "doesn't include unused thumbs buttons when disabled in project" do it "doesn't include unused thumbs buttons when disabled in project" do
issue_without_downvote.project.show_default_award_emojis = false note_without_downvote.project.show_default_award_emojis = false
expect(issue_without_downvote.grouped_awards.keys.sort).to be_empty expect(note_without_downvote.grouped_awards.keys.sort).to be_empty
end end
it "includes unused thumbs buttons when enabled in project" do it "includes unused thumbs buttons when enabled in project" do
issue_without_downvote.project.show_default_award_emojis = true note_without_downvote.project.show_default_award_emojis = true
expect(issue_without_downvote.grouped_awards.keys.sort).to eq %w(thumbsdown thumbsup) expect(note_without_downvote.grouped_awards.keys.sort).to eq %w(thumbsdown thumbsup)
end end
it "doesn't include unused thumbs buttons in summary" do it "doesn't include unused thumbs buttons in summary" do
expect(issue_without_downvote.grouped_awards(with_thumbs: false).keys).to be_empty expect(note_without_downvote.grouped_awards(with_thumbs: false).keys).to be_empty
end end
it "includes used thumbs buttons when disabled in project" do it "includes used thumbs buttons when disabled in project" do
issue_with_downvote.project.show_default_award_emojis = false note_with_downvote.project.show_default_award_emojis = false
expect(issue_with_downvote.grouped_awards.keys).to eq %w(thumbsdown) expect(note_with_downvote.grouped_awards.keys).to eq %w(thumbsdown)
end end
it "includes used thumbs buttons in summary" do it "includes used thumbs buttons in summary" do
expect(issue_with_downvote.grouped_awards(with_thumbs: false).keys).to eq %w(thumbsdown) expect(note_with_downvote.grouped_awards(with_thumbs: false).keys).to eq %w(thumbsdown)
end end
end end
end end
......
...@@ -128,6 +128,24 @@ RSpec.describe Issue do ...@@ -128,6 +128,24 @@ RSpec.describe Issue do
end end
end end
context 'order by upvotes' do
let!(:issue) { create(:issue) }
let!(:issue2) { create(:issue) }
let!(:award_emoji) { create(:award_emoji, :upvote, awardable: issue2) }
describe '.order_upvotes_desc' do
it 'orders on upvotes' do
expect(described_class.order_upvotes_desc.to_a).to eq [issue2, issue]
end
end
describe '.order_upvotes_asc' do
it 'orders on upvotes' do
expect(described_class.order_upvotes_asc.to_a).to eq [issue, issue2]
end
end
end
describe '.with_alert_management_alerts' do describe '.with_alert_management_alerts' do
subject { described_class.with_alert_management_alerts } subject { described_class.with_alert_management_alerts }
......
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