forgeplus/app/models/journal.rb

287 lines
11 KiB
Ruby

# == Schema Information
#
# Table name: journals
#
# id :integer not null, primary key
# journalized_id :integer default("0"), not null
# journalized_type :string(30) default(""), not null
# user_id :integer default("0"), not null
# notes :text(4294967295)
# created_on :datetime not null
# private_notes :boolean default("0"), not null
# parent_id :integer
# comments_count :integer default("0")
# reply_id :integer
# review_id :integer
# commit_id :string(255)
# diff :text(4294967295)
# line_code :string(255)
# path :string(255)
# state :integer default("0")
# resolve_at :datetime
# resolveer_id :integer
# need_respond :boolean default("0")
# updated_on :datetime
#
# Indexes
#
# index_journals_on_created_on (created_on)
# index_journals_on_journalized_id (journalized_id)
# index_journals_on_review_id (review_id)
# index_journals_on_user_id (user_id)
# journals_journalized_id (journalized_id,journalized_type)
#
class Journal < ApplicationRecord
serialize :diff, JSON
alias_attribute :note, :notes
belongs_to :user
belongs_to :issue, foreign_key: :journalized_id, :touch => true, optional: true
belongs_to :journalized, polymorphic: true
belongs_to :review, optional: true
belongs_to :resolveer, class_name: 'User', foreign_key: :resolveer_id, optional: true
belongs_to :parent_journal, class_name: 'Journal', foreign_key: :parent_id, optional: true, counter_cache: :comments_count
belongs_to :reply_journal, class_name: 'Journal', foreign_key: :reply_id, optional: true
has_many :journal_details, :dependent => :delete_all
has_many :attachments, as: :container, dependent: :destroy
has_many :first_ten_children_journals, -> { order(created_on: :asc).limit(10)}, class_name: 'Journal', foreign_key: :parent_id
has_many :children_journals, class_name: 'Journal', foreign_key: :parent_id, dependent: :destroy
scope :journal_includes, ->{includes(:user, :journal_details, :attachments)}
scope :parent_journals, ->{where(parent_id: nil)}
scope :children_journals, lambda{|journal_id| where(parent_id: journal_id)}
enum state: {opened: 0, resolved: 1, disabled: 2}
def is_journal_detail?
self.notes.blank? && self.journal_details.present?
end
def operate_content
content = ""
detail = self.journal_details.take
case detail.property
when 'issue'
return "创建了<b>疑修</b>"
when 'attachment'
old_value = Attachment.where(id: detail.old_value.split(",")).pluck(:filename).join("")
new_value = Attachment.where(id: detail.value.split(",")).pluck(:filename).join("")
if old_value.nil? || old_value.blank?
content += "添加了<b>#{new_value}</b>附件"
else
new_value = "" if new_value.blank?
content += "将附件由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
end
when 'issue_tag'
old_value = IssueTag.where(id: detail.old_value.split(",")).pluck(:name).join("")
new_value = IssueTag.where(id: detail.value.split(",")).pluck(:name).join("")
if old_value.nil? || old_value.blank?
content += "添加了<b>#{new_value}</b>标记"
else
new_value = "" if new_value.blank?
content += "将标记由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
end
when 'assigner'
old_value = User.where(id: detail.old_value.split(",")).map{|u| u.real_name}.join("")
new_value = User.where(id: detail.value.split(",")).map{|u| u.real_name}.join("")
if old_value.nil? || old_value.blank?
content += "添加负责人<b>#{new_value}</b>"
else
new_value = "" if new_value.blank?
content += "将负责人由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
end
when 'attr'
content = ""
case detail.prop_key
when 'subject'
return "修改了<b>标题</b>"
when 'description'
return "修改了<b>描述</b>"
when 'status_id'
old_value = IssueStatus.find_by_id(detail.old_value)&.name
new_value = IssueStatus.find_by_id(detail.value)&.name
content += "状态"
when 'priority_id'
old_value = IssuePriority.find_by_id(detail.old_value)&.name
new_value = IssuePriority.find_by_id(detail.value)&.name
content += "优先级"
when 'fixed_version_id'
old_value = Version.find_by_id(detail.old_value)&.name
new_value = Version.find_by_id(detail.value)&.name
content += "里程碑"
when 'branch_name'
old_value = detail.old_value
new_value = detail.value
content += "关联分支"
when 'start_date'
old_value = detail.old_value
new_value = detail.value
content += "开始日期"
when 'due_date'
old_value = detail.old_value
new_value = detail.value
content += "结束日期"
when 'assigned_to_id'
old_value = User.find_by_id(detail.old_value)&.real_name
new_value = User.find_by_id(detail.value)&.real_name
content += "负责人"
end
if old_value.nil? || old_value.blank?
content += "设置为<b>#{new_value}</b>"
else
new_value = "" if new_value.blank?
content += "由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
end
end
end
def journal_content
send_details = []
if self.is_journal_detail?
details = self.journal_details.select(:property, :prop_key, :old_value, :value).pluck(:property, :prop_key, :old_value, :value)
if details.size > 0
details.each do |de|
if de[0] == "attr"
content = ""
else
content = "附件"
end
old_value = de[2]
value = de[3]
if de[1].to_i > 0
prop_name = ""
else
prop_name = I18n.t("journal_detail.#{de[1]}")
case de[1]
when "description"
old_value = "描述已更新"
value = "修改了描述"
when "is_private"
old_value = I18n.t("journal_detail.#{de[2]}")
value = I18n.t("journal_detail.#{de[3]}")
when "assigned_to_id"
u = User.select(:id, :login, :lastname, :firstname, :nickname)
old_value = de[2].to_i > 0 ? u.find_by_id(de[2]).try(:show_real_name) : ""
assign_user = de[3].to_i > 0 ? u.find_by_id(de[3]) : ""
if assign_user.present?
value = assign_user.try(:show_real_name)
else
value = ""
end
when "tracker_id"
t = Tracker.select(:id, :name)
old_value = de[2].to_i > 0 ? t.find_by_id(de[2]).try(:name) : ""
tracker_name = de[3].to_i > 0 ? t.find_by_id(de[3]) : ""
if tracker_name
value = tracker_name.try(:name)
else
value = ""
end
when "status_id"
t = IssueStatus.select(:id, :name)
old_value = de[2].to_i > 0 ? t.find_by_id(de[2]).try(:name) : ""
type_name = de[3].to_i > 0 ? t.find_by_id(de[3]) : ""
if type_name
value = type_name.try(:name)
else
value = ""
end
when "priority_id"
t = IssuePriority.select(:id, :name)
old_value = de[2].to_i > 0 ? t.find_by_id(de[2]).try(:name): ""
type_name = de[3].to_i > 0 ? t.find_by_id(de[3]) : ""
if type_name
value = type_name.try(:name)
else
value = ""
end
when "issue_tags_value"
t = IssueTag.select(:id, :name)
old_value = de[2].to_i > 0 ? t.where(id: de[2].split(",")).select(:id,:name,:color).as_json : ""
if de[3].present?
value = t.where(id: de[3].split(",")).select(:id,:name,:color).as_json
else
value = ""
end
when "fixed_version_id"
t = Version.select(:id, :name)
old_value = de[2].to_i > 0 ? t.find_by_id(de[2]).try(:name) : ""
type_name = de[3].to_i > 0 ? t.find_by_id(de[3]) : ""
if type_name
value = type_name.try(:name)
else
value = ""
end
when "end_time"
t = IssueTime.select(:id, :start_time, :end_time)
type_name = de[2].to_i > 0 ? t.find_by_id(de[2]) : ""
if type_name.present?
old_value = "停止工作"
d_value = type_name.end_time.to_i - type_name.start_time.to_i
value = "#{Time.at(d_value).utc.strftime('%H h %M min %S s')}"
else
old_value = "停止工作"
value = ""
end
when "issue_depend"
t = Issue.select(:id,:subject )
type_name = de[3].present? ? t&.find_by_id(de[3]) : ""
if type_name.present?
old_value = "增加依赖"
value = {
id: de[3],
name: type_name.try(:subject)
}
else
old_value = "增加依赖"
value = ""
end
when "destroy_issue_depend"
t = Issue.select(:id,:subject )
type_name = de[3].present? ? t&.find_by_id(de[3]) : ""
if type_name.present?
old_value = "删除依赖"
value = {
id: de[3],
name: type_name.try(:subject)
}
else
old_value = "删除依赖"
value = ""
end
when "done_ratio"
old_value = "#{de[2]}%"
value = "#{de[3]}%"
else
old_value = de[2]
value = de[3]
end
end
prop_hash = {
detail: (content + prop_name),
old_value: old_value,
value: value
}
send_details.push(prop_hash)
end
end
end
send_details
end
def to_builder
Jbuilder.new do |journal|
journal.(self, :id, :notes, :comments_count)
if self.parent_journal.present?
journal.parent_journal self.parent_journal.to_builder
end
if self.reply_journal.present?
journal.reply_journal self.reply_journal.to_builder
end
end
end
end