Sha256: 38a15cc77abab9c7ee1d35bef22a66599755f8fad16df6e3e5af21e3f1b42944

Contents?: true

Size: 846 Bytes

Versions: 3

Compression:

Stored size: 846 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

    def emotional_about
      self.emotions.includes(:emotional)
    end

    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

    module ClassMethods
      def define_emotion_methods(emotion)
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{emotion}_about
            emotional_about.where(emotion: #{emotion.to_s.inspect})
          end
        RUBY
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
emotions-0.1.6 lib/emotions/emotive.rb
emotions-0.1.5 lib/emotions/emotive.rb
emotions-0.1.4 lib/emotions/emotive.rb