Sha256: 91baeca6de621fb82be16bb80f09c53006797bc46cfe8d7b137ce81bc44e8f6d

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

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

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

    # Custom validations
    validate :ensure_valid_emotion_name
    validate { ensure_valid_associated_record :emotional }
    validate { ensure_valid_associated_record :emotive }

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

    # Callbacks
    after_create :update_emotion_counter
    after_destroy :update_emotion_counter

  protected

    # Update the `<emotion>_emotions_counter` for the emotive record
    def update_emotion_counter
      self.emotive.update_emotion_counter(self.emotion)
      self.emotional.update_emotion_counter(self.emotion)
    end

    # Make sure we're using an allowed emotion name
    def ensure_valid_emotion_name
      unless Emotions.emotions.include?(self.emotion.try(:to_sym))
        errors.add :emotion, I18n.t(:invalid, scope: [:errors, :messages])
      end
    end

    # Make sure that both emotive and emotional records are actually able to
    # express and/or receive emotions
    def ensure_valid_associated_record(association)
      value = send(association)
      predicate = :"#{association}?"

      if !value.class.respond_to?(predicate) || !value.class.send(predicate)
        errors.add association, I18n.t(:invalid, scope: [:errors, :messages])
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
emotions-0.2.2 lib/emotions/emotion.rb