OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
introduce changed_at_timestamp on WithHistoricAttributes
ulferts3d033a50da68
diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rbindex 2e8231d7904..f22f08a2856 100644--- a/app/models/journable/with_historic_attributes.rb+++ b/app/models/journable/with_historic_attributes.rb@@ -211,6 +211,14 @@ class Journable::WithHistoricAttributes < SimpleDelegatorquery && matches_query_filters_at_timestamps.include?(timestamp)end+ def changed_at_timestamp(timestamp)+ return [] unless journables_by_timestamp[timestamp.to_s]++ ::Acts::Journalized::JournableDiffer+ .changes(__getobj__, journables_by_timestamp[timestamp.to_s])+ .keys+ end+def self.query_work_packages(query:, timestamp: nil)query = query.dupquery.timestamps = [timestamp] if timestampdiff --git a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb b/lib/api/v3/work_packages/eager_loading/historic_attributes.rbindex 8a816ced343..602dcf4eddf 100644--- a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb+++ b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb@@ -60,11 +60,10 @@ module API::V3::WorkPackages::EagerLoadingdef set_non_delegated_properties(work_package, source, timestamp)work_package.matches_filters_at_timestamp = source.matches_query_filters_at_timestamps.include?(timestamp)work_package.exists_at_timestamp = source.exists_at_timestamps.include?(timestamp)- work_package.attributes_changed_to_baseline = (source.attributes_by_timestamp[timestamp.to_s]&.to_h || {}).keys.map(&:to_s)+ work_package.attributes_changed_to_baseline = source.changed_at_timestamp(timestamp)work_package.with_query = source.query.present?end- # TODO: prepare by indexing by iddef work_packages_with_historic_attributes@work_packages_with_historic_attributes ||= begin@timestamps ||= @query.try(:timestamps) || []diff --git a/lib_static/plugins/acts_as_journalized/lib/acts/journalized/journable_differ.rb b/lib_static/plugins/acts_as_journalized/lib/acts/journalized/journable_differ.rbnew file mode 100644index 00000000000..a078e4042e4--- /dev/null+++ b/lib_static/plugins/acts_as_journalized/lib/acts/journalized/journable_differ.rb@@ -0,0 +1,63 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# ++++module Acts::Journalized+ class JournableDiffer+ class << self+ def changes(original, changed)+ original_data = normalize_newlines(journaled_attributes(original))++ normalize_newlines(journaled_attributes(changed))+ .select { |attribute, new_value| no_nil_to_empty_strings?(original_data, attribute, new_value) }+ .to_h { |attribute, new_value| [attribute, [original_data[attribute], new_value]] }+ .with_indifferent_access+ end++ private++ def normalize_newlines(data)+ data.each_with_object({}) do |e, h|+ h[e[0]] = (e[1].is_a?(String) ? e[1].gsub(/\r\n/, "\n") : e[1])+ end+ end++ def no_nil_to_empty_strings?(normalized_old_data, attribute, new_value)+ old_value = normalized_old_data[attribute]+ new_value != old_value && (new_value.present? || old_value.present?)+ end++ def journaled_attributes(object)+ if object.is_a?(Journal::BaseJournal)+ object.journaled_attributes.stringify_keys+ else+ object.attributes.slice(*object.class.journal_class.journaled_attributes.map(&:to_s))+ end+ end+ end+ end+enddiff --git a/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb b/lib_static/plugins/acts_as_journalized/lib/journal_changes.rbindex cf6525c6b27..7d31af740a2 100644--- a/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb+++ b/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb@@ -54,13 +54,7 @@ module JournalChangesenddef subsequent_journal_data_changes- normalized_new_data = normalize_newlines(data.journaled_attributes)- normalized_old_data = normalize_newlines(predecessor.data.journaled_attributes)-- normalized_new_data- .select { |attribute, new_value| no_nil_to_empty_strings?(normalized_old_data, attribute, new_value) }- .to_h { |attribute, new_value| [attribute, [normalized_old_data[attribute], new_value]] }- .with_indifferent_access+ ::Acts::Journalized::JournableDiffer.changes(predecessor.data, data)enddef get_association_changes(predecessor, journal_association, association, key, value)@@ -129,15 +123,4 @@ module JournalChangesselected_journals.sort.join(',')endend-- def normalize_newlines(data)- data.each_with_object({}) do |e, h|- h[e[0]] = (e[1].is_a?(String) ? e[1].gsub(/\r\n/, "\n") : e[1])- end- end-- def no_nil_to_empty_strings?(normalized_old_data, attribute, new_value)- old_value = normalized_old_data[attribute]- new_value != old_value && (new_value.present? || old_value.present?)- endenddiff --git a/spec/lib/acts_as_journalized/journable_differ_spec.rb b/spec/lib/acts_as_journalized/journable_differ_spec.rbnew file mode 100644index 00000000000..fc12fd69c87--- /dev/null+++ b/spec/lib/acts_as_journalized/journable_differ_spec.rb@@ -0,0 +1,83 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# ++++require 'spec_helper'++describe Acts::Journalized::JournableDiffer do+ describe '.changes' do+ context 'when the objects are work packages' do+ let(:original) do+ build_stubbed(:work_package,+ subject: 'The original work package title',+ description: "The description\n")+ end+ let(:changed) do+ build_stubbed(:work_package,+ subject: 'The changed work package title',+ description: "The description\r\n",+ priority: original.priority,+ type: original.type,+ project: original.project)+ end++ it 'returns the changes' do+ expect(described_class.changes(original, changed))+ .to eql("subject" => [original.subject, changed.subject],+ "author_id" => [original.author_id, changed.author_id],+ "status_id" => [original.status_id, changed.status_id])+ end+ end++ context 'when the objects are WorkPackageJournal' do+ let(:original) do+ build_stubbed(:journal_work_package_journal,+ subject: 'The original work package title',+ description: "The description\n",+ priority_id: 5,+ type_id: 89,+ project_id: 12,+ status_id: 45)+ end+ let(:changed) do+ build_stubbed(:journal_work_package_journal,+ subject: 'The changed work package title',+ description: "The description\r\n",+ priority_id: original.priority_id,+ type_id: original.type_id,+ project_id: original.project_id,+ status_id: original.status_id + 12)+ end++ it 'returns the changes' do+ expect(described_class.changes(original, changed))+ .to eql("subject" => [original.subject, changed.subject],+ "status_id" => [original.status_id, changed.status_id])+ end+ end+ end+enddiff --git a/spec/models/journable/with_historic_attributes_spec.rb b/spec/models/journable/with_historic_attributes_spec.rbindex d2ac3671171..bec9371db95 100644--- a/spec/models/journable/with_historic_attributes_spec.rb+++ b/spec/models/journable/with_historic_attributes_spec.rb@@ -511,4 +511,24 @@ describe Journable::WithHistoricAttributes doendendend++ describe '.changed_at_timestamp' do+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }++ subject { described_class.wrap(work_package1, timestamps:) }++ context 'for a timestamp where the work package did exist' do+ it 'returns the changed attributes at the timestamp compared to the current attribute values' do+ expect(subject.changed_at_timestamp(Timestamp.parse("2022-01-01T00:00:00Z")))+ .to match_array ['subject']+ end+ end++ context 'for a timestamp where the work package did not exist' do+ it 'returns the changed attributes at the timestamp compared to the current attribute values' do+ expect(subject.changed_at_timestamp(Timestamp.parse("2021-01-01T00:00:00Z")))+ .to be_empty+ end+ end+ endend