OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/app/models/journal.rb

175 lines
5.3 KiB

#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
#
# Copyright (C) 2012-2013 the OpenProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
class Journal < ActiveRecord::Base
self.table_name = "journals"
include JournalFormatter
include FormatHooks
register_journal_formatter :diff, OpenProject::JournalFormatter::Diff
register_journal_formatter :attachment, OpenProject::JournalFormatter::Attachment
register_journal_formatter :custom_field, OpenProject::JournalFormatter::CustomField
register_journal_formatter :scenario_date, OpenProject::JournalFormatter::ScenarioDate
attr_accessible :journaled_type, :journaled_id, :activity_type, :version, :notes, :user_id
# Make sure each journaled model instance only has unique version ids
validates_uniqueness_of :version, :scope => [:journaled_id, :journaled_type]
belongs_to :user
has_many :attachable_journals, class_name: Journal::AttachableJournal
has_many :customizable_journals, class_name: Journal::CustomizableJournal
after_save :save_data
# Scopes to all journals excluding the initial journal - useful for change
# logs like the history on issue#show
scope "changing", :conditions => ["version > 1"]
def journaled
@journaled ||= journalized_object_type.find(journaled_id)
end
def changed_data=(changed_attributes)
attributes = changed_attributes
if attributes.kind_of? Hash and attributes.values.first.kind_of? Array
attributes.each {|k,v| attributes[k] = v[1]}
end
data.update_attributes attributes
end
# In conjunction with the included Comparable module, allows comparison of journal records
# based on their corresponding version numbers, creation timestamps and IDs.
def <=>(other)
[version, created_at, id].map(&:to_i) <=> [other.version, other.created_at, other.id].map(&:to_i)
end
# Returns whether the version has a version number of 1. Useful when deciding whether to ignore
# the version during reversion, as initial versions have no serialized changes attached. Helps
# maintain backwards compatibility.
def initial?
version < 2
end
# The anchor number for html output
def anchor
version - 1
end
# Possible shortcut to the associated project
def project
if journaled.respond_to?(:project)
journaled.project
elsif journaled.is_a? Project
journaled
else
nil
end
end
def editable_by?(user)
journaled.journal_editable_by?(user)
end
def details
get_changes
end
alias_method :changed_data, :details
def new_value_for(prop)
details[prop.to_s].last if details.keys.include? prop.to_s
end
def old_value_for(prop)
details[prop.to_s].first if details.keys.include? prop.to_s
end
def data
@data ||= "Journal::#{journaled_type}".constantize.find_by_journal_id(id)
end
def data=(data)
@data = data
end
private
def save_data
data.journal_id = id if data.new_record?
data.save!
end
def get_changes
return {} if data.nil?
if @changes.nil?
@changes = {}
if predecessor.nil?
@changes = data.journaled_attributes.select{|_,v| !v.nil?}
.inject({}) { |h, (k, v)| h[k] = [nil, v]; h }
else
predecessor_data = predecessor.data.journaled_attributes
data.journaled_attributes.select{|k,v| v != predecessor_data[k]}.each do |k, v|
@changes[k] = [predecessor_data[k], v]
end
end
@changes.merge!(get_attachment_changes predecessor)
end
@changes
end
def get_attachment_changes(predecessor)
changes = {}
if predecessor.nil?
attachable_journals.each_with_object(changes) {|a, h| h["attachments_#{a.attachment_id}".to_sym] = [nil, a.filename] }
else
all_attachable_journal_ids = attachable_journals.map(&:attachment_id) | predecessor.attachable_journals.map(&:attachment_id)
all_attachable_journals = all_attachable_journal_ids.each_with_object({}) { |i, h| h[i] = [predecessor.attachable_journals.find_by_attachment_id(i), attachable_journals.find_by_attachment_id(i)] }
# find new attachments
all_attachable_journals.select {|_, v| v[0].nil? and not v[1].nil?}
.each_with_object(changes) { |k,h| h["attachments_#{k[0]}".to_sym] = [nil, k[1][1].filename] }
# find removed attachments
all_attachable_journals.select {|_, v| not v[0].nil? and v[1].nil?}
.each_with_object(changes) { |k,h| h["attachments_#{k[0]}".to_sym] = [k[1][0].filename, nil] }
# find changed attachments
all_attachable_journals.select {|_, v| not v[0].nil? and not v[1].nil? and v[0].filename != v[1].filename}
.each_with_object(changes) { |k,h| h["attachments_#{k[0]}".to_sym] = [k[1][0].filename, k[1][1].filename] }
end
changes
end
def predecessor
@predecessor ||= Journal.where("journaled_type = ? AND journaled_id = ? AND id < ?",
journaled_type, journaled_id, id)
.order("version DESC")
.first
end
def journalized_object_type
"#{journaled_type.gsub('Journal', '')}".constantize
end
end