socialforge/app/models/work.rb

72 lines
2.3 KiB
Ruby

class Work < ActiveRecord::Base
belongs_to :user
belongs_to :contest
attr_accessible :description, :end_time, :is_delete, :is_open, :name, :publish_time, :score_open, :work_status, :work_type, :contest_id, :user_id
include ApplicationHelper
has_one :work_detail_group, :dependent => :destroy
has_many :contestant_work_projects, :dependent => :destroy
has_many :contestant_works, :dependent => :destroy, :conditions => "is_delete != 1"
has_many :journals_for_messages, :as => :jour, :dependent => :destroy
has_many :praise_tread, as: :praise_tread_object, dependent: :destroy
has_one :praise_tread_cache, as: :object, dependent: :destroy
# 竞赛动态
has_many :contest_acts, :class_name => 'ContestActivity',:as =>:contest_act ,:dependent => :destroy
# 竞赛消息
has_many :contest_messages, :class_name =>'ContestMessage', :as => :contest_message, :dependent => :destroy
acts_as_attachable
after_create :act_as_contest_message
after_update :update_activity
after_save :act_as_contest_activity
#after_destroy :delete_kindeditor_assets
def is_group_work?
self.work_type == 3 && self.work_detail_group
end
###添加回复
def self.add_work_jour(user, notes, id, root_id, options = {})
homework = Work.find(id)
if options.count == 0
jfm = homework.journals_for_messages.build(:user_id => user.id, :notes => notes, :reply_id => 0, :root_id => root_id)
else
jfm = homework.journals_for_messages.build(options)
end
jfm.save
jfm
end
def act_as_contest_activity
if self.contest
if self.work_status == 0
self.contest_acts.destroy_all
else
if self.contest_acts.size == 0
self.contest_acts << ContestActivity.new(:user_id => self.user_id,:contest_id => self.contest_id)
end
end
end
end
#动态的更新
def update_activity
update_contest_activity(self.class, self.id)
update_user_activity(self.class, self.id)
end
def act_as_contest_message
if self.contest
if self.work_status == 0
self.contest_messages.destroy_all
else
self.contest.contest_members.each do |m|
if m.user_id != self.user_id
self.contest_messages << ContestMessage.new(:user_id => m.user_id, :contest_id => self.contest_id, :viewed => false)
end
end
end
end
end
end