In Rails 4.2 `#asset_path` has changed. Prior to Rails 4.2,
`#asset_path` would search all asset directories, and prepend the
appropriate asset type directory name to the result:
asset_path("emoji/unicode/<id>.png")
#=> "/images/emoji/unicode/<id>.png"
However, In Rails 4.2 `#asset_path` doesn't do that anymore, and assumes
that you've specified the correct folder. Since Emoji images are copied
to public/images/emoji/unicode, `#asset_path` would then generate the
wrong URL path:
asset_path("emoji/unicode/<id>.png")
#=> "/emoji/unicode/<id>.png"
Using `#image_path` fixes that, because that method looks specifically
for images/ folder, so it will find the Emoji images.
gemoji
Emoji images and names. See the LICENSE for copyright information.
Installation
Add gemoji to your Gemfile.
gem 'gemoji'
Sync images
Images can be copied to your public directory with rake emoji in your app. This is the recommended approach since the images will be available at a consistent location. This works best with cached formatted user content generated by tools like html-pipeline.
# Rakefile
load 'tasks/emoji.rake'
$ rake emoji
Assets Precompiling
If you must, you can manually add all the images to your asset load path.
# config/application.rb
config.assets.paths << Emoji.images_path
Then have them compiled to public on deploy.
# config/application.rb
config.assets.precompile << "emoji/**/*.png"
WARNING Since there are a ton of images, just adding the path may slow down other lookups if you aren't using it. Compiling all the emojis on deploy will add overhead to your deploy if even the images haven't changed. Theres just so many more superfluous files to iterate over. Also, the urls will be fingerprinted which may not be ideal for referencing from cached content.
Example Rails Helper
This would allow emojifying content such as: it's raining :cat:s and :dog:s!
See the Emoji cheat sheet for more examples.
module EmojiHelper
def emojify(content)
h(content).to_str.gsub(/:([\w+-]+):/) do |match|
if emoji = Emoji.find_by_alias($1)
%(<img alt="#$1" src="#{image_path("emoji/#{emoji.image_filename}")}" style="vertical-align:middle" width="20" height="20" />)
else
match
end
end.html_safe if content.present?
end
end
Unicode mapping
Translate emoji names to unicode and vice versa.
>> Emoji.find_by_alias("cat").raw
=> "🐱" # Don't see a cat? That's U+1F431.
>> Emoji.find_by_unicode("\u{1f431}").name
=> "cat"
Adding new emoji
You can add new emoji characters to the Emoji.all list:
emoji = Emoji.create("music") do |char|
char.add_alias "song"
char.add_unicode_alias "\u{266b}"
char.add_tag "notes"
end
emoji.name #=> "music"
emoji.raw #=> "♫"
emoji.image_filename #=> "unicode/266b.png"
# Creating custom emoji (no Unicode aliases):
emoji = Emoji.create("music") do |char|
char.add_tag "notes"
end
emoji.custom? #=> true
emoji.image_filename #=> "music.png"
As you create new emoji, you must ensure that you also create and put the images
they reference by their image_filename to your assets directory.
You can customize image_filename with:
emoji = Emoji.create("music") do |char|
char.image_filename = "subdirectory/my_emoji.gif"
end
For existing emojis, you can edit the list of aliases or add new tags in an edit block:
emoji = Emoji.find_by_alias "musical_note"
Emoji.edit_emoji(emoji) do |char|
char.add_alias "music"
char.add_unicode_alias "\u{266b}"
char.add_tag "notes"
end
Emoji.find_by_alias "music" #=> emoji
Emoji.find_by_unicode "\u{266b}" #=> emoji
octocat, squirrel, shipit Copyright (c) 2013 GitHub Inc. All rights reserved. bowtie, neckbeard, fu Copyright (c) 2013 37signals, LLC. All rights reserved. feelsgood, finnadie, goberserk, godmode, hurtrealbad, rage 1-4, suspect Copyright (c) 2013 id Software. All rights reserved. trollface Copyright (c) 2013 whynne@deviantart. All rights reserved. All other images Copyright (c) 2013 Apple Inc. All rights reserved. Source code: Copyright (c) 2013 GitHub, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.