Emotions is a Ruby library that allows ActiveRecord records to
express (and hopefully store) emotions about other records.
Installation
Add this line to your application’s Gemfile:
gem 'emotions'
And then execute:
$ bundle
Run the migration to add the emotions
table and the Emotion
model:
$ rails generate emotions:install
Usage
Configure the allowed emotions.
Emotions.configure do |config|
config.emotions = [:happy, :sad]
end
Create a couple of models.
class Picture < ActiveRecord::Base
acts_as_emotive
end
class User < ActiveRecord::Base
acts_as_emotional
end
Express emotions towards other records.
user = User.find(1)
picture = Picture.find(2)
user.happy_about!(picture)
user.sad_about!(picture)
user.emotions_about(picture)
# => [:happy, :sad]
user.happy_about?(picture)
# => true
user.no_longer_sad_about!(picture)
user.sad_about?(picture)
# => false
User.happy_about(picture)
# => #<ActiveRecord::Relation [#<User id=1>]>
user.express!(:sad, picture)
user.sad_about?(picure)
# => true
Cache counters
Most of the times, you would want to get a quick look at about how many users expressed a certain emotion towards a certain picture. That could be an expensive operation.
However, if the emotive record has an <emotion>_emotions_count
column, Emotions will populate its value with how many emotional records expressed that emotion towards it.
user.happy_about!(picture)
picture.happy_about.count
# SQL query that counts records and returns `1`
picture.happy_emotions_count
# Quick lookup into the column and returns `1`
Same thing for emotional records. If there’s a <emotion>_emotions_count
column in the emotional model, Emotions will update it each time one of its records expresses that emotion towards another record.
user.happy_about!(picture)
user.happy_about.count
# SQL query that counts records and returns `1`
user.happy_emotions_count
# Quick lookup into the column and returns `1`
License
Emotions
is © 2013-2015 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md
file.
About Mirego
Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We're a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.
We also love open-source software and we try to give back to the community as much as we can.