Sha256: c2e7f88b0d0e38a60c52bc74d162759348146a105793a358c761e2850401b924

Contents?: true

Size: 897 Bytes

Versions: 3

Compression:

Stored size: 897 Bytes

Contents

module Emotions
  module Emotive
    extend ActiveSupport::Concern

    included do
      has_many :emotions, as: :emotive, class_name: 'Emotions::Emotion', dependent: :destroy

      Emotions.emotions.each do |emotion|
        define_emotion_methods(emotion)
      end
    end

    # @private
    def update_emotion_counter(emotion)
      attribute = "#{emotion}_emotions_count"

      if self.respond_to?(attribute)
        self.update_attribute(attribute, self.send("#{emotion}_about").count)
      end
    end

    # @private
    module ClassMethods
      def define_emotion_methods(emotion)
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{emotion}_about
            self.emotions.where(emotion: #{emotion.to_s.inspect})
          end
          alias #{emotion}_with #{emotion}_about
          alias #{emotion}_over #{emotion}_about
        RUBY
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
emotions-0.2.2 lib/emotions/emotive.rb
emotions-0.2.1 lib/emotions/emotive.rb
emotions-0.2 lib/emotions/emotive.rb