Sha256: d851ff84e148f704edde60eb02b2b0d19460fb93aa45ded560df5da66f06cb59

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module Emotions
  class Emotion < ActiveRecord::Base
    self.table_name = 'emotions'

    # Validations
    validates :emotional, presence: true
    validates :emotive, presence: true

    validates_each :emotion do |record, attr, value|
      unless Emotions.emotions.include?(value.try(:to_sym))
        record.errors.add attr, I18n.t(:invalid, scope: [:errors, :messages])
      end
    end

    validates_each :emotional do |record, attr, value|
      if value.blank? || !value.class.try(:emotional?)
        record.errors.add attr, I18n.t(:invalid, scope: [:errors, :messages])
      end
    end

    validates_each :emotive do |record, attr, value|
      if value.blank? || !value.class.try(:emotive?)
        record.errors.add attr, I18n.t(:invalid, scope: [:errors, :messages])
      end
    end

    # Associations
    belongs_to :emotional, polymorphic: true
    belongs_to :emotive, polymorphic: true

    # Callbacks
    after_create :update_emotion_counter
    after_destroy :update_emotion_counter

  protected

    def update_emotion_counter
      self.emotive.update_emotion_counter(self.emotion)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
emotions-0.2.1 lib/emotions/emotion.rb
emotions-0.2 lib/emotions/emotion.rb
emotions-0.1.7 lib/emotions/emotion.rb