|
|
|
class Discuss < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
attr_accessible :user_id, :content, :dis_id, :dis_type, :parent_id, :praise_count, :root_id, :challenge_id,
|
|
|
|
:position, :reward, :hidden
|
|
|
|
default_scope :order => 'created_at desc'
|
|
|
|
|
|
|
|
has_many :praise_tread, as: :praise_tread_object, dependent: :destroy
|
|
|
|
has_one :praise_tread_cache, as: :object, dependent: :destroy
|
|
|
|
acts_as_tree :foreign_key => 'parent_id'
|
|
|
|
belongs_to :dis, :polymorphic => true
|
|
|
|
belongs_to :last_reply, :class_name => 'Discuss', :foreign_key => 'last_reply_id'
|
|
|
|
has_many :tidings, :class_name => 'Tiding', :as => :container , :dependent => :destroy
|
|
|
|
after_create :send_tiding
|
|
|
|
validates_presence_of :dis, :user, :content
|
|
|
|
after_create :reset_counters!
|
|
|
|
|
|
|
|
scope :visible, lambda { where(:hidden => false)}
|
|
|
|
scope :field_for_list, lambda{
|
|
|
|
select([:id, :content, :user_id, :updated_at, :dis_id, :dis_type, :reward, :root_id, :hidden])
|
|
|
|
}
|
|
|
|
scope :posts, lambda { where(:root_id => nil, :hidden => false)}
|
|
|
|
|
|
|
|
def creator_user
|
|
|
|
User.find(self.user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# 用户评论的game
|
|
|
|
def user_game
|
|
|
|
myshixun = Myshixun.where(:user_id => self.user_id, :shixun_id => self.dis).first
|
|
|
|
challenge_id = self.try(:challenge_id).nil? ? self.dis.challenges.first.try(:id) : self.try(:challenge_id)
|
|
|
|
game = Game.where(:myshixun_id => myshixun.try(:id), :challenge_id => challenge_id).first
|
|
|
|
end
|
|
|
|
|
|
|
|
# 用户评论的 myshixun
|
|
|
|
def user_myshixun
|
|
|
|
myshixun = Myshixun.where(:user_id => self.user_id, :shixun_id => self.dis).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_detail
|
|
|
|
self.content
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_counters!
|
|
|
|
if parent && parent.id
|
|
|
|
parent.update_attribute(:last_reply_id, parent.children.maximum(:id))
|
|
|
|
end
|
|
|
|
if root
|
|
|
|
root.update_attribute(:last_reply_id, Discuss.where(:root_id => root.id).maximum(:id)) unless root.destroyed?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_tiding
|
|
|
|
if self.parent_id.present?
|
|
|
|
self.tidings << Tiding.new(:user_id => self.parent.user_id, :trigger_user_id=> self.user_id, :parent_container_id => self.challenge_id, :parent_container_type => "Challenge", :belong_container_id => self.dis_id, :belong_container_type => "Shixun", :viewed => 0, :tiding_type => "Comment")
|
|
|
|
else
|
|
|
|
self.tidings << Tiding.new(:user_id => Challenge.find(self.challenge_id).user_id, :trigger_user_id=> self.user_id, :parent_container_id => self.challenge_id, :parent_container_type => "Challenge", :belong_container_id => self.dis_id, :belong_container_type => "Shixun", :viewed => 0, :tiding_type => "Comment")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
self.user.show_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_api(current_user, user_id)
|
|
|
|
(current_user.admin? || current_user.id == user_id) ? true : false
|
|
|
|
end
|
|
|
|
end
|