From 09a434e2d80f99f0051cff66aec3d647d2318303 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Wed, 14 Sep 2022 13:10:48 +0200 Subject: [PATCH] DEV: emoji model should set the group of the emoji (#18237) We do it for custom emojis but not for standard emojis which is quite unexpected and limit the use cases we can have for it. --- app/models/emoji.rb | 19 +++++++++++++++++++ spec/models/emoji_spec.rb | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/models/emoji.rb b/app/models/emoji.rb index c940f317964..2333a913f44 100644 --- a/app/models/emoji.rb +++ b/app/models/emoji.rb @@ -94,6 +94,7 @@ class Emoji e.name = name e.tonable = Emoji.tonable_emojis.include?(name) e.url = Emoji.url_for(filename) + e.group = groups[name] || DEFAULT_GROUP end end @@ -122,6 +123,24 @@ class Emoji site_emoji_cache.clear end + def self.groups_file + @groups_file ||= "#{Rails.root}/lib/emoji/groups.json" + end + + def self.groups + @groups ||= begin + groups = {} + + File.open(groups_file, "r:UTF-8") { |f| JSON.parse(f.read) }.each do |group| + group["icons"].each do |icon| + groups[icon["name"]] = group["name"] + end + end + + groups + end + end + def self.db_file @db_file ||= "#{Rails.root}/lib/emoji/db.json" end diff --git a/spec/models/emoji_spec.rb b/spec/models/emoji_spec.rb index f6abbcd8573..0cdbcb9df8f 100644 --- a/spec/models/emoji_spec.rb +++ b/spec/models/emoji_spec.rb @@ -136,4 +136,17 @@ RSpec.describe Emoji do expect(replaced_str).to eq("This is a good day \"woman\" :foo: :bar:t4: :man:t8:") end end + + describe ".groups" do + it "returns an optimized emoji name -> group name datastructure" do + expect(Emoji.groups["scotland"]).to eq("flags") + end + end + + describe "#create_from_db_item" do + it "sets the group of the emoji" do + emoji = Emoji.create_from_db_item("name" => "scotland") + expect(emoji.group).to eq("flags") + end + end end