Sha256: 11d52a84d42feb9ca8e3bffa18492fcca763db4a78fbe98297d87f636c96bff1

Contents?: true

Size: 889 Bytes

Versions: 1

Compression:

Stored size: 889 Bytes

Contents

module Emotions
  module Emotive
    extend ActiveSupport::Concern

    included do
      has_many :emotions, as: :emotive, class_name: '::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

1 entries across 1 versions & 1 rubygems

Version Path
emotions-0.3 lib/emotions/emotive.rb