class Journal < ApplicationRecord belongs_to :user belongs_to :issue, foreign_key: :journalized_id, :touch => true has_many :journal_details, :dependent => :delete_all has_many :attachments, as: :container, 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)} def is_journal_detail? self.notes.blank? && self.journal_details.present? 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 "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) old_value = u.find(de[2]).try(:show_real_name) value = u.find(de[3]).try(:show_real_name) when "tracker_id" t = Tracker.select(:id, :name) old_value = t.find(de[2]).try(:name) value = t.find(de[3]).try(:name) when "status_id" t = IssueStatus.select(:id, :name) old_value = t.find(de[2]).try(:name) value = t.find(de[3]).try(:name) when "priority_id" t = IssuePriority.select(:id, :name) old_value = t.find(de[2]).try(:name) value = t.find(de[3]).try(:name) when "issue_tags_value" t = IssueTag.select(:id, :name) old_value = t.where(id: de[2].split(",")).select(:id,:name,:color).as_json value = t.where(id: de[3].split(",")).select(:id,:name,:color).as_json when "fixed_version_id" t = Version.select(:id, :title) old_value = t.find(de[2]).try(:title) value = t.find(de[3]).try(:title) 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 end