Sha256: e0ec2560387e04c7d775d36060e63ab6f4116f4ddab98f20ccad05182516d6be

Contents?: true

Size: 825 Bytes

Versions: 2

Compression:

Stored size: 825 Bytes

Contents

module Emotions
  module Emotive
    extend ActiveSupport::Concern

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

      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

2 entries across 2 versions & 1 rubygems

Version Path
emotions-0.1.3 lib/emotions/emotive.rb
emotions-0.1.2 lib/emotions/emotive.rb