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 "    def \#{emotion}_about?(emotive)\n      !!\#{emotion}_about(emotive).exists?\n    end\n\n    def \#{emotion}_about!(emotive)\n      express! \#{emotion.inspect}, emotive\n    end\n\n    def no_longer_\#{emotion}_about!(emotive)\n      no_longer_express! \#{emotion.inspect}, emotive\n    end\n\n    def \#{emotion}_about(emotive = nil)\n      relation = emotive.nil? ? emotions : _emotions_about(emotive)\n      relation.where(emotion: \#{emotion.to_s.inspect})\n    end\n\n    alias \#{emotion}? \#{emotion}_about?\n    alias \#{emotion}_with? \#{emotion}_about?\n    alias \#{emotion}_over? \#{emotion}_about?\n\n    alias \#{emotion}_with! \#{emotion}_about!\n    alias \#{emotion}_over! \#{emotion}_about!\n\n    alias no_longer_\#{emotion}_with! no_longer_\#{emotion}_about!\n    alias no_longer_\#{emotion}_over! no_longer_\#{emotion}_about!\n\n    alias \#{emotion}_with \#{emotion}_about\n    alias \#{emotion}_over \#{emotion}_about\n  RUBY\n\n  instance_eval <<-RUBY, __FILE__, __LINE__ + 1\n    def \#{emotion}_about(emotive)\n      emotional_about(\#{emotion.inspect}, emotive)\n    end\n\n    alias \#{emotion}_with \#{emotion}_about\n    alias \#{emotion}_over \#{emotion}_about\n  RUBY\nend\n", __FILE__, __LINE__ + 1

#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