module Emotions::Emotional::ClassMethods

Public Instance Methods

define_emotion_methods(emotion) click to toggle source

rubocop:disable MethodLength

# 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) click to toggle source

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

@example

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