Module: Emotions::Emotional

Extended by:
ActiveSupport::Concern
Defined in:
lib/emotions/emotional.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_emotions_about(emotive) ⇒ Object



14
15
16
# File 'lib/emotions/emotional.rb', line 14

def _emotions_about(emotive)
  self.emotions.where(emotive_id: emotive.id, emotive_type: emotive.class.name)
end

#emotions_about(emotive) ⇒ Object

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

Examples:

user = User.first
picture = Picture.first
user.express! :happy, picture
user.emotions_about(picture)
# => [:happy]


27
28
29
# File 'lib/emotions/emotional.rb', line 27

def emotions_about(emotive)
  _emotions_about(emotive).pluck(:emotion).map(&:to_sym)
end

#express!(emotion, emotive) ⇒ Object

Express an emotion towards another record

Examples:

user = User.first
picture = Picture.first
user.express! :happy, picture


37
38
39
40
41
42
43
44
45
# 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

#no_longer_express!(emotion, emotive) ⇒ Object

No longer express an emotion towards another record

Examples:

user = User.first
picture = Picture.first
user.no_longer_express! :happy, picture


53
54
55
# 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