module Emotions::Emotional

Public Instance Methods

_emotions_about(emotive) click to toggle source

@private

# File lib/emotions/emotional.rb, line 14
def _emotions_about(emotive)
  emotions.where(emotive_id: emotive.id, emotive_type: emotive.class.name)
end
emotions_about(emotive) click to toggle source

Return all emotions expressed by the emotional record towards another emotive record

@example

user = User.first
picture = Picture.first
user.express! :happy, picture
user.emotions_about(picture)
# => [:happy]
# File lib/emotions/emotional.rb, line 27
def emotions_about(emotive)
  _emotions_about(emotive).pluck(:emotion).map(&:to_sym)
end
express!(emotion, emotive) click to toggle source

Express an emotion towards another record

@example

user = User.first
picture = Picture.first
user.express! :happy, picture
# File lib/emotions/emotional.rb, line 37
def express!(emotion, emotive)
  emotion = _emotions_about(emotive).where(emotion: emotion).first_or_initialize

  begin
    emotion.tap(&:save!)
  rescue ActiveRecord::RecordInvalid => e
    raise InvalidEmotion.new(e.record)
  end
end
express?(emotion, emotive) click to toggle source

Find if an emotion is expressed towards another record

@example

user = User.first
picture = Picture.first
user.express? :happy, picture
# File lib/emotions/emotional.rb, line 72
def express?(emotion, emotive)
  _emotions_about(emotive).where(emotion: emotion).any?
end
no_longer_express!(emotion, emotive) click to toggle source

No longer express an emotion towards another record

@example

user = User.first
picture = Picture.first
user.no_longer_express! :happy, picture
# File lib/emotions/emotional.rb, line 53
def no_longer_express!(emotion, emotive)
  _emotions_about(emotive).where(emotion: emotion).first.tap { |e| e.try(:destroy) }
end
update_emotion_counter(emotion) click to toggle source

@private

# File lib/emotions/emotional.rb, line 58
def update_emotion_counter(emotion)
  attribute = "#{emotion}_emotions_count"

  if respond_to?(attribute)
    update_attribute(attribute, send("#{emotion}_about").count)
  end
end