Install the gem and add to the application's Gemfile by executing:
$ bundle add acts_as_reactable
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install acts_as_reactable
# rails g migration create_reactions
# with default id type
create_table :reactions do |t|
t.references :reactable, polymorphic: true, null: false
t.references :reactor, polymorphic: true, null: false
t.string :emoji, null: false, index: true
t.timestamps
t.index [:reactable_type, :reactable_id, :reactor_type, :reactor_id, :emoji], unique: true, name: 'index_reactions_on_reactable_and_reactor_and_emoji'
end
# with uuid id
create_table :reactions, id: :uuid do |t|
t.references :reactable, polymorphic: true, type: :uuid, null: false
t.references :reactor, polymorphic: true, type: :uuid, null: false
...
end
# reactable
class Post < ApplicationRecord
acts_as_reactable
end
# reactor
class User < ApplicationRecord
acts_as_reactor
end
post.add_reactions(user, "π")
post.add_reactions(user, ["π", "π"])
post.remove_reactions(user, "π")
post.remove_reactions(user, ["π", "π"])
reactions = ActsAsReactable::Reaction.where(reactable: post, reactor: user)
ActsAsReactable::Reaction.where(reactable: reactor).group(:emoji).order('count_id DESC').count(:id)
# { "π": 10, "π’": 5, "π£": 1 }
- Technically, there's no concrete name/key/id for emoji (and modifiers like skin tone). The CLDR short names "vary by language" and "may change", besides, are those names case sensitive? Should we use
-
,_
or - It's easier to store since all modern database supports encodings (e.g. UTF-8) for unicode characters.
- It's easy to validate with libs/regex (e.g. unicode-emoji).
- It takes less size on disk to store (and presumably less time to index/sort/match) one unicode character π (4 bytes) than
face with tears of joy
(22 bytes). This is a great article to explain how utf-8 works
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
We use release-please and the action.
In order for the flow to work, commits need to follow the convention.
Typical workflow:
- commit (following the convention) and push to
main
- release-please creates PR for release new version
- merge the PR
- automatically publish new release to rubygems by github action
Bug reports and pull requests are welcome on GitHub at https://github.com/public-reactions/acts_as_reactable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the ActsAsReactable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.