Module: Emotions::Emotional::ClassMethods

Defined in:
lib/emotions/emotional.rb

Instance Method Summary collapse

Instance Method Details

#define_emotion_methods(emotion) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/emotions/emotional.rb', line 77

def define_emotion_methods(emotion)
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{emotion}_about?(emotive)
      !!#{emotion}_about(emotive).exists?
    end

    def #{emotion}_about!(emotive)
      express! #{emotion.inspect}, emotive
    end

    def no_longer_#{emotion}_about!(emotive)
      no_longer_express! #{emotion.inspect}, emotive
    end

    def #{emotion}_about(emotive)
      _emotions_about(emotive).where(emotion: #{emotion.to_s.inspect})
    end

    alias #{emotion}? #{emotion}_about?
    alias #{emotion}_with? #{emotion}_about?
    alias #{emotion}_over? #{emotion}_about?

    alias #{emotion}_with! #{emotion}_about!
    alias #{emotion}_over! #{emotion}_about!

    alias no_longer_#{emotion}_with! no_longer_#{emotion}_about!
    alias no_longer_#{emotion}_over! no_longer_#{emotion}_about!

    alias #{emotion}_with #{emotion}_about
    alias #{emotion}_over #{emotion}_about
  RUBY

  instance_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{emotion}_about(emotive)
      emotional_about(#{emotion.inspect}, emotive)
    end

    alias #{emotion}_with #{emotion}_about
    alias #{emotion}_over #{emotion}_about
  RUBY
end

#emotional_about(emotion, emotive) ⇒ Object

Return an ‘ActiveRecord::Relation` containing the emotional records that expressed a specific emotion towards an emotive record

Examples:

user = User.first
picture = Picture.first
user.express! :happy, picture
User.emotional_about(:happy, picture)
# => #<ActiveRecord::Relation [#<User id=1>]>


67
68
69
70
71
72
73
74
75
# File 'lib/emotions/emotional.rb', line 67

def emotional_about(emotion, emotive)
  if emotive.class.emotive?
    emotional_ids = emotive.emotions.where(emotion: emotion).where(emotional_type: self.name).pluck(:emotional_id)
    where(id: emotional_ids)
  else
    # ActiveRecord 4 supports `.none`, not ActiveRecord 3
    respond_to?(:none) ? none : where('1 = 0')
  end
end