Commit 2ea026bd authored by Gabriel Mazetto's avatar Gabriel Mazetto

Refactored Emoji methods to rely on TanukiEmoji and removed unused ones

parent 52be0404
......@@ -31,7 +31,7 @@ class CustomEmoji < ApplicationRecord
private
def valid_emoji_name
if Gitlab::Emoji.emoji_exists?(name)
if TanukiEmoji.find_by_alpha_code(name)
errors.add(:name, _('%{name} is already being used for another emoji') % { name: self.name })
end
end
......
......@@ -14,7 +14,7 @@ module AwardEmojis
private
def normalize_name(name)
Gitlab::Emoji.normalize_emoji_name(name)
TanukiEmoji.find_by_alpha_code(name)&.name || name
end
# Provide more error state data than what BaseService allows.
......
......@@ -4,30 +4,6 @@ module Gitlab
module Emoji
extend self
def emojis_by_moji
TanukiEmoji.index.instance_variable_get(:@codepoints_index)
end
def emojis_unicodes
emojis_by_moji.keys
end
def emojis_aliases
@emoji_aliases ||= Gitlab::Json.parse(File.read(Rails.root.join('fixtures', 'emojis', 'aliases.json')))
end
def emoji_filename(name)
TanukiEmoji.find_by_alpha_code(name).image_name
end
def emoji_unicode_filename(moji)
TanukiEmoji.find_by_codepoints(moji).image_name
end
def normalize_emoji_name(name)
emojis_aliases[name] || name
end
def emoji_image_tag(name, src)
image_options = {
class: 'emoji',
......@@ -42,10 +18,6 @@ module Gitlab
ActionController::Base.helpers.tag(:img, image_options)
end
def emoji_exists?(name)
TanukiEmoji.find_by_alpha_code(name)
end
# CSS sprite fallback takes precedence over image fallback
# @param [TanukiEmoji::Character] emoji
# @param [Hash] options
......
......@@ -3,48 +3,6 @@
require 'spec_helper'
RSpec.describe Gitlab::Emoji do
let_it_be(:emojis_by_moji) { TanukiEmoji.index.all.index_by(&:codepoints) }
let_it_be(:emojis_aliases) { Gitlab::Json.parse(File.read(Rails.root.join('fixtures', 'emojis', 'aliases.json'))) }
describe '.emojis_aliases' do
it 'returns emoji aliases' do
emoji_aliases = described_class.emojis_aliases
expect(emoji_aliases).to eq(emojis_aliases)
end
end
describe '.emoji_filename' do
it 'returns emoji filename' do
# "100" => {"unicode"=>"1F4AF"...}
emoji_filename = described_class.emoji_filename('100')
expect(emoji_filename).to eq("emoji_u#{TanukiEmoji.find_by_alpha_code('100').hex}.png")
end
end
describe '.emoji_unicode_filename' do
it 'returns emoji unicode filename' do
emoji_unicode_filename = described_class.emoji_unicode_filename('💯')
expect(emoji_unicode_filename).to eq("emoji_u#{TanukiEmoji.find_by_codepoints('💯').hex}.png")
end
end
describe '.normalize_emoji_name' do
it 'returns same name if not found in aliases' do
emoji_name = described_class.normalize_emoji_name('random')
expect(emoji_name).to eq('random')
end
it 'returns name if name found in aliases' do
emoji_name = described_class.normalize_emoji_name('small_airplane')
expect(emoji_name).to eq(emojis_aliases['small_airplane'])
end
end
describe '.emoji_image_tag' do
it 'returns emoji image tag' do
emoji_image = described_class.emoji_image_tag('emoji_one', 'src_url')
......@@ -62,20 +20,6 @@ RSpec.describe Gitlab::Emoji do
end
end
describe '.emoji_exists?' do
it 'returns true if the name exists' do
emoji_exists = described_class.emoji_exists?('100')
expect(emoji_exists).to be_truthy
end
it 'returns false if the name does not exist' do
emoji_exists = described_class.emoji_exists?('random')
expect(emoji_exists).to be_falsey
end
end
describe '.gl_emoji_tag' do
it 'returns gl emoji tag if emoji is found' do
emoji = TanukiEmoji.find_by_alpha_code('small_airplane')
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe AwardEmojis::BaseService do
let(:awardable) { build(:note) }
let(:current_user) { build(:user) }
describe '.initialize' do
subject { described_class }
it 'uses same emoji name if not an alias' do
emoji_name = 'horse'
expect(subject.new(awardable, emoji_name, current_user).name).to eq(emoji_name)
end
it 'uses emoji original name if its an alias' do
emoji_alias = 'small_airplane'
emoji_name = 'airplane_small'
expect(subject.new(awardable, emoji_alias, current_user).name).to eq(emoji_name)
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