class OrgDocumentComment < ActiveRecord::Base
  attr_accessible :content, :creator_id, :organization_id, :parent_id, :reply_id, :title,:sticky,:locked
  include Redmine::SafeAttributes
  include ApplicationHelper
  belongs_to :organization
  belongs_to :creator, :class_name => 'User', :foreign_key => 'creator_id'
  has_many :editor_of_documents, :dependent => :destroy
  acts_as_tree :order => "#{OrgDocumentComment.table_name}.sticky asc, #{OrgDocumentComment.table_name}.created_at desc"
  acts_as_attachable
  has_many :org_acts, :class_name => 'OrgActivity',:as =>:org_act ,:dependent => :destroy
  after_create :document_save_as_org_activity
  after_update :update_activity

  #动态的更新
  def update_activity
    org_activity = OrgActivity.where("org_act_type='OrgDocumentComment' and org_act_id =?", id).first
    if org_activity
      org_activity.updated_at = Time.now
      org_activity.save
    end
  end

  def document_save_as_org_activity
    if(self.parent().nil?)
      self.org_acts << OrgActivity.new(:user_id => User.current.id, :container_id => self.organization.id, :container_type => 'Organization')
    else
      act = OrgActivity.where("org_act_type='OrgDocumentComment' and org_act_id =?", self.root.id).first
      act.update_attributes(:updated_at => self.updated_at)
    end
  end


  def project

  end

end
