class ForgeMessage < ActiveRecord::Base
  # status在不同的类中，作用不同
  # Isseu： satus nil：发布了缺陷；：1：缺陷计划完成日志到了提醒
  attr_accessible :forge_message_id, :forge_message_type, :project_id, :user_id, :viewed, :secret_key, :status

  belongs_to :forge_message ,:polymorphic => true
  belongs_to :project
  belongs_to :user
  has_many :message_alls, :class_name => 'MessageAll',:as =>:message, :dependent => :destroy

  validates :user_id,presence: true
  validates :project_id,presence: true
  validates :forge_message_id,presence: true
  validates :forge_message_type, presence: true
  after_create :add_user_message

  def add_user_message
    if MessageAll.where("message_type = '#{self.class.to_s}' and message_id = '#{self.id}'").first.nil?
      self.message_alls << MessageAll.new(:user_id => self.user_id)
    end
  end
end
