Module: Emotions::Emotional
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/emotions/emotional.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #_emotions_about(emotive) ⇒ Object
-
#emotions_about(emotive) ⇒ Object
Return all emotions expressed by the emotional record towards another emotive record.
-
#express!(emotion, emotive) ⇒ Object
Express an emotion towards another record.
-
#express?(emotion, emotive) ⇒ Boolean
Find if an emotion is expressed towards another record.
-
#no_longer_express!(emotion, emotive) ⇒ Object
No longer express an emotion towards another record.
- #update_emotion_counter(emotion) ⇒ Object
Instance Method Details
#_emotions_about(emotive) ⇒ Object
14 15 16 |
# 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) ⇒ Object
Return all emotions expressed by the emotional record towards another emotive record
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
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 |
#express?(emotion, emotive) ⇒ Boolean
Find if an emotion is expressed towards another record
72 73 74 |
# File 'lib/emotions/emotional.rb', line 72 def express?(emotion, emotive) _emotions_about(emotive).where(emotion: emotion).any? end |
#no_longer_express!(emotion, emotive) ⇒ Object
No longer express an emotion towards another record
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 |
#update_emotion_counter(emotion) ⇒ Object
58 59 60 61 62 63 64 |
# 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 |