Commit 853408a1 authored by Toon Claes's avatar Toon Claes

AwardEmoji: Don't look up url for built-in emoji

When we serialize the attributes of an awarded emoji, we ask the url of
the emoji. This will only be set for Custom Emoji, and to find it, we
need to look it up in the database.

Now for built-in emoji, we know all the names of those, and we know they
don't have an url set. So if the awarded emoji is one of the built-in,
early return without ever querying the database.

To ensure this behavior, and the method will not error-out, test
coverage is added for AwardEmoji#url.

Changelog: fixed
parent 60afa0a4
...@@ -61,6 +61,8 @@ class AwardEmoji < ApplicationRecord ...@@ -61,6 +61,8 @@ class AwardEmoji < ApplicationRecord
end end
def url def url
return if TanukiEmoji.find_by_alpha_code(name)
awardable.try(:namespace)&.custom_emoji&.by_name(name)&.first&.url awardable.try(:namespace)&.custom_emoji&.by_name(name)&.first&.url
end end
......
...@@ -223,4 +223,35 @@ RSpec.describe AwardEmoji do ...@@ -223,4 +223,35 @@ RSpec.describe AwardEmoji do
end end
end end
end end
describe '#url' do
let_it_be(:custom_emoji) { create(:custom_emoji) }
let_it_be(:project) { create(:project, namespace: custom_emoji.group) }
let_it_be(:issue) { create(:issue, project: project) }
def build_award(name)
build(:award_emoji, awardable: issue, name: name)
end
it 'is nil for built-in emoji' do
new_award = build_award('tada')
count = ActiveRecord::QueryRecorder.new do
expect(new_award.url).to be_nil
end.count
expect(count).to be_zero
end
it 'is nil for unrecognized emoji' do
new_award = build_award('null')
expect(new_award.url).to be_nil
end
it 'is set for custom emoji' do
new_award = build_award(custom_emoji.name)
expect(new_award.url).to eq(custom_emoji.url)
end
end
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