Module: Emotions::Emotional::ClassMethods

Defined in:
lib/emotions/emotional.rb

Instance Method Summary collapse

Instance Method Details

#define_emotion_methods(emotion) ⇒ Object

rubocop:disable MethodLength



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/emotions/emotional.rb', line 97

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 = nil)
      relation = emotive.nil? ? emotions : _emotions_about(emotive)
      relation.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>]>


86
87
88
89
90
91
92
93
94
# File 'lib/emotions/emotional.rb', line 86

def emotional_about(emotion, emotive)
  if emotive.class.emotive?
    emotional_ids = emotive.emotions.where(emotion: emotion).where(emotional_type: 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