ruling_timer/app/models/ruling_timer_project.rb

84 lines
3.4 KiB
Ruby

class RulingTimerProject
include Mongoid::Document
include Mongoid::Timestamps
include OrbitTag::Taggable
include OrbitCategory::Categorizable
field :project_no, type: String, default: ""
field :project_name, type: String, default: ""
field :objectives, type: String, default: "" #專案⽬標
field :status, type: Integer, default: 0 # 0 =>規劃中, 1 => 已啟動, 2 => 已結案
has_many :ruling_timer_tasks, :autosave => true
field :is_finished, type: Boolean, default: false
field :time_offset, type: String, default: "+8"
field :privacy, type: Integer, default: 0 #私密等級 0 => 所有團隊成員Open, 1 => 專案成員Project, 2 => 優先, 3 => 任務成員 Task
field :priority, type: Integer, default: 0 #優先度 0 => 無, 1 => 普通, 2 => 優先, 3 => 緊急
belongs_to :creator , :class_name=>"User", :foreign_key => :creator_id #建立者
belongs_to :project_manager , :class_name=>"User", :foreign_key => :project_manager_id #專案經理
field :all_user_ids, type: Array, default: [] #專案成員
field :is_hidden, type: Boolean, default: true
field :is_private, type: Boolean, default: true
field :task_count, type: Integer, default: 0
field :sort_number, type: Integer, default: 0
has_many :ruling_timer_sections, :autosave => true, :dependent => :destroy #任務列表
after_initialize do |record|
unless record.new_record?
save_flag = false
if record.category_id.nil?
record.category_id = RulingTimerSetting.first.personal_category_id
save_flag = true
end
if record.project_manager_id.nil?
record.project_manager_id = record.creator_id
save_flag = true
end
record.save if save_flag
end
end
before_create do |record|
time_now = record.created_at || DateTime.now.utc.new_offset(record.time_offset)
today_start = DateTime.parse("#{time_now.strftime("%Y/%m/%d")} 00:00:00#{record.time_offset}").utc
today_end = DateTime.parse("#{(time_now + 1.day).strftime("%Y/%m/%d")} 00:00:00#{record.time_offset}").utc
today_project_count = record.class.where(:created_at.gte=>today_start,:created_at.lt=>today_end).count
record.project_no = "P"+time_now.strftime("%Y%m%d") + ("0000" + (today_project_count + 1).to_s)[-4..-1]
end
before_save do |record|
end
def all_users
User.where(:id.in=>self.all_user_ids)
end
def convert_datetime(time)
if time.class == Time
return time.to_datetime
elsif time.class == DateTime
return time
elsif time.class == String
return DateTime.parse(time)
else
return Time.at(time).to_datetime #time is seconds
end
end
def create_time
convert_datetime(self.created_at.utc).new_offset(self.time_offset).strftime("%Y/%m/%d %H:%m")
end
def users
User.where(:id.in=>self.user_ids)
end
def get_project_name
self.is_private ? I18n.t("ruling_timer.private_tasks") : self.project_name
end
def get_status
I18n.t("ruling_timer.status_opts.#{self.status}")
end
after_save do |record|
# if record.user_ids_changed?
# delete_user_ids = record.user_ids_was.to_a - record.user_ids.to_a
# add_user_ids = record.user_ids.to_a - record.user_ids_was.to_a
# delete_user_ids.each do |user|
# self.ruling_timer_sub_tasks.where(:user=>user).destroy
# end
# add_user_ids.each do |user|
# self.ruling_timer_sub_tasks.create(:user=>user,:task_name=>task_name)
# end
# end
end
end