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