EmojiReplace

A library to work around the special UTF8MB4 encoding in Ruby and Ruby on Rails.

Uses the “emoji” gem here: github.com/wpeterson/emoji

Install

Add this to your Gemfile and bundle:

gem "emoji_replace"

Seamless usage

Modify your model like this:

class Message < ActiveRecord::Base
  include EmojiReplace::Seamless
  replace_emoji_seamlessly :message
end

You can now call the attributes like normal:

message.message = "I ❤ you"
message.message #=> "I ❤ you"
message.read_attribute(:message) #=> "I %{emoji:heart} you"
message.message(html: true) #=> "I <img alt=\"heart\" class=\"emoji\" src=\"http://localhost:3000/heart.png\"> you"

Manual usage

If you don’t want the seamless functionality, you can also perform the replace manually like this:

Replace emoji’s to the code format:

replacer = EmojiReplace::Replacer.new(text: "I ❤ you")
replacer.replace
replacer.text #=> "I %{emoji:heart} you"

Replace the code format back to emoji’s:

replacer = EmojiReplace::Replacer.new(text: "I %{emoji:heart} you")
replacer.back
replacer.text #=> "I ❤ you"

Replace the code format back to HTML:

replacer = EmojiReplace::Replacer.new(text: "I %{emoji:heart} you")
replacer.back(html: true)
replacer.text #=> "I <img alt=\"heart\" class=\"emoji\" src=\"http://localhost:3000/heart.png\"> you"

Contributing to emoji_replace

Copyright © 2015 kaspernj. See LICENSE.txt for further details.