diff options
author | Robert Speicher <rspeicher@gmail.com> | 2016-10-06 20:00:51 +0200 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2016-10-07 15:39:28 +0200 |
commit | cd028da88665caa1bdf1ac117f2dfd26d5a6009b (patch) | |
tree | e07c810d7f4e203d4c260b2dddbb067b72c0a096 | |
parent | 9a13f885a9dc7b072d41160a6d3db965c9114b4b (diff) | |
download | gitlab-ce-rs-optimize-award_user_list-spec.tar.gz |
Optimize the `award_user_list` helper specrs-optimize-award_user_list-spec
According to
https://gitlab.com/gitlab-org/gitlab-ce/issues/23034#note_16586657, each
test for this helper generated 1,833 queries.
Now we only generate stubbed records, and only as many as we need for
each test.
This also corrects a slight logic bug in the helper itself. When the
number of awards was greater than the limit (9 by default), _and_ the
current user was one of them, we actually included 10 names, including
"You", plus the remaining count. Now we return the correct number
regardless.
-rw-r--r-- | app/helpers/issues_helper.rb | 5 | ||||
-rw-r--r-- | spec/helpers/issues_helper_spec.rb | 38 |
2 files changed, 27 insertions, 16 deletions
diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index 8b212b0327a..8b80f7d1192 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -113,14 +113,13 @@ module IssuesHelper end end - def award_user_list(awards, current_user) + def award_user_list(awards, current_user, limit: 9) names = awards.map do |award| award.user == current_user ? 'You' : award.user.name end - # Take first 9 OR current user + first 9 current_user_name = names.delete('You') - names = names.first(9).insert(0, current_user_name).compact + names = names.insert(0, current_user_name).compact.first(limit) names << "#{awards.size - names.size} more." if awards.size > names.size diff --git a/spec/helpers/issues_helper_spec.rb b/spec/helpers/issues_helper_spec.rb index 67bac782591..2d65ed2a4ed 100644 --- a/spec/helpers/issues_helper_spec.rb +++ b/spec/helpers/issues_helper_spec.rb @@ -63,28 +63,40 @@ describe IssuesHelper do end describe '#award_user_list' do - let!(:awards) { build_list(:award_emoji, 15) } + it "returns a comma-separated list of the first X users" do + user = build_stubbed(:user, name: 'Joe') + award = build_stubbed(:award_emoji, user: user) + awards = Array.new(2, award) - it "returns a comma seperated list of 1-9 users" do - expect(award_user_list(awards.first(9), nil)).to eq(awards.first(9).map { |a| a.user.name }.to_sentence) + expect(award_user_list(awards, nil, limit: 2)) + .to eq('Joe and Joe') end it "displays the current user's name as 'You'" do - expect(award_user_list(awards.first(1), awards[0].user)).to eq('You') - end + user = build_stubbed(:user, name: 'Joe') + award = build_stubbed(:award_emoji, user: user) - it "truncates lists of larger than 9 users" do - expect(award_user_list(awards, nil)).to eq(awards.first(9).map { |a| a.user.name }.join(', ') + ", and 6 more.") + expect(award_user_list([award], user)).to eq('You') + expect(award_user_list([award], nil)).to eq 'Joe' end - it "displays the current user in front of 0-9 other users" do - expect(award_user_list(awards, awards[0].user)). - to eq("You, " + awards[1..9].map { |a| a.user.name }.join(', ') + ", and 5 more.") + it "truncates lists" do + user = build_stubbed(:user, name: 'Jane') + award = build_stubbed(:award_emoji, user: user) + awards = Array.new(5, award) + + expect(award_user_list(awards, nil, limit: 3)) + .to eq('Jane, Jane, Jane, and 2 more.') end - it "displays the current user in front regardless of position in the list" do - expect(award_user_list(awards, awards[12].user)). - to eq("You, " + awards[0..8].map { |a| a.user.name }.join(', ') + ", and 5 more.") + it "displays the current user in front of other users" do + current_user = build_stubbed(:user) + my_award = build_stubbed(:award_emoji, user: current_user) + award = build_stubbed(:award_emoji, user: build_stubbed(:user, name: 'Jane')) + awards = Array.new(5, award).push(my_award) + + expect(award_user_list(awards, current_user, limit: 2)). + to eq("You, Jane, and 4 more.") end end |