Skip to content

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

fixup! [#47064] Support custom_field_* in the attributesByTimestamp

Dombi Attila6acbb058e7a4
diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rb
index 8273b43920b..a9565217123 100644
--- a/app/models/journable/with_historic_attributes.rb
+++ b/app/models/journable/with_historic_attributes.rb
@@ -227,9 +227,20 @@ class Journable::WithHistoricAttributes < SimpleDelegator
historic_journable = at_timestamp(Timestamp.parse(timestamp))
return unless historic_journable
-
- ::Acts::Journalized::JournableDiffer
- .changes(__getobj__, historic_journable)
+ changes = ::Acts::Journalized::JournableDiffer.changes(__getobj__, historic_journable)
+
+ changes.merge!(
+ ::Acts::Journalized::JournableDiffer.association_changes(
+ historic_journable,
+ __getobj__,
+ 'custom_values',
+ 'custom_fields',
+ :custom_field_id,
+ :value
+ )
+ )
+
+ changes
end
def changed_attributes_at_timestamp(timestamp)
diff --git a/app/models/journable/with_historic_attributes/loader.rb b/app/models/journable/with_historic_attributes/loader.rb
index 9da3b495bdd..e442f011af3 100644
--- a/app/models/journable/with_historic_attributes/loader.rb
+++ b/app/models/journable/with_historic_attributes/loader.rb
@@ -103,8 +103,16 @@ class Journable::WithHistoricAttributes
journal = journals_by_id[work_package.journal_id]
work_package.association(:journals).loaded!
work_package.association(:journals).target = Array(journal)
+
+ # Build the associated customizable_journals as custom values, this way the historic work packages
+ # will behave just as the normal ones. Additionally set the reverse customized association
+ # on the custom_values that points to the work_package itself.
+ historic_custom_values = Array(journal&.customizable_journals).map do |customizable_journal|
+ customizable_journal.as_custom_value(customized: work_package)
+ end
+
work_package.association(:custom_values).loaded!
- work_package.association(:custom_values).target = Array(journal&.customizable_journals)
+ work_package.association(:custom_values).target = historic_custom_values
end
journalized
end
diff --git a/app/models/journal/customizable_journal.rb b/app/models/journal/customizable_journal.rb
index 72dbfb56968..22bfa6cce5a 100644
--- a/app/models/journal/customizable_journal.rb
+++ b/app/models/journal/customizable_journal.rb
@@ -30,4 +30,10 @@ class Journal::CustomizableJournal < Journal::AssociatedJournal
self.table_name = 'customizable_journals'
belongs_to :custom_field
+
+ def as_custom_value(attributes = {})
+ custom_value = CustomValue.new(attributes.merge(custom_field:, value:))
+ custom_value.readonly!
+ custom_value
+ end
end
diff --git a/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb b/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb
index 89fad76fdfd..4036916eac9 100644
--- a/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb
+++ b/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb
@@ -72,7 +72,9 @@ module API
private
def representable_map(*)
- Representable::Binding::Map.new(super.select { |bind| rendered_properties.include?(bind.name) })
+ Representable::Binding::Map.new(super.select do |bind|
+ rendered_properties.include?(bind.name) || bind.name.starts_with?("custom_field_")
+ end)
end
def compile_links_for(configs, *args)
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.rb
index a078e4042e4..30fff1dc0b2 100644
--- 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.rb
@@ -30,7 +30,7 @@ module Acts::Journalized
class JournableDiffer
class << self
def changes(original, changed)
- original_data = normalize_newlines(journaled_attributes(original))
+ original_data = original ? normalize_newlines(journaled_attributes(original)) : {}
normalize_newlines(journaled_attributes(changed))
.select { |attribute, new_value| no_nil_to_empty_strings?(original_data, attribute, new_value) }
@@ -38,17 +38,22 @@ module Acts::Journalized
.with_indifferent_access
end
+ def association_changes(original, changed, *association_params)
+ get_association_changes(original, changed, *association_params)
+ 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])
+ 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?)
+ # Checking new_value != old_value ensures that both values cannot be nil
+ new_value != old_value
end
def journaled_attributes(object)
@@ -58,6 +63,78 @@ module Acts::Journalized
object.attributes.slice(*object.class.journal_class.journaled_attributes.map(&:to_s))
end
end
+
+ def get_association_changes(original, changed, association, association_name, key, value)
+ # ::Kernel.binding.pry# if changed.send(association).any? && original.nil?
+
+ if original.nil?
+ changed.send(association).each_with_object({}) do |associated_journal, h|
+ changed_attribute = "#{association_name}_#{associated_journal.send(key)}"
+ new_value = associated_journal.send(value)
+ h[changed_attribute] = [nil, new_value]
+ end
+ else
+ new_journals = changed.send(association).map(&:attributes)
+ old_journals = original.send(association).map(&:attributes)
+
+ changes_on_association(new_journals, old_journals, association_name, key, value)
+ end
+ end
+
+ def changes_on_association(current, original, association_name, key, value)
+ merged_journals = merge_reference_journals_by_id(current, original, key.to_s, value.to_s)
+
+ changes = added_references(merged_journals)
+ .merge(removed_references(merged_journals))
+ .merge(changed_references(merged_journals))
+
+ to_changes_format(changes, association_name.to_s)
+ end
+
+ def added_references(merged_references)
+ merged_references
+ .select { |_, (old_value, new_value)| old_value.nil? && new_value.present? }
+ end
+
+ def removed_references(merged_references)
+ merged_references
+ .select { |_, (old_value, new_value)| old_value.present? && new_value.nil? }
+ end
+
+ def changed_references(merged_references)
+ merged_references
+ .select { |_, (old_value, new_value)| old_value.present? && new_value.present? && old_value.strip != new_value.strip }
+ end
+
+ def to_changes_format(references, key)
+ references.each_with_object({}) do |(id, (old_value, new_value)), result|
+ result["#{key}_#{id}"] = [old_value, new_value]
+ end
+ end
+
+ def merge_reference_journals_by_id(new_journals, old_journals, id_key, value)
+ all_associated_journal_ids = new_journals.pluck(id_key) | old_journals.pluck(id_key)
+
+ all_associated_journal_ids.index_with do |id|
+ [select_and_combine_journals(old_journals, id, id_key, value),
+ select_and_combine_journals(new_journals, id, id_key, value)]
+ end
+ end
+
+ def select_and_combine_journals(journals, id, key, value)
+ selected_journals = journals.select { |j| j[key] == id }.pluck(value)
+
+ if selected_journals.empty?
+ nil
+ else
+ selected_journals.sort.join(',')
+ end
+ end
end
+
+ # def initialize(original, changed)
+ # @original = original
+ # @changed = changed
+ # end
end
end
diff --git a/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb b/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb
index 67ec936bf7b..12a803d4905 100644
--- a/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb
+++ b/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb
@@ -31,100 +31,17 @@ module JournalChanges
return @changes if @changes
return {} if data.nil?
- @changes = (cause.present? ? { cause: [nil, cause] } : {}).with_indifferent_access
+ @changes = ::Acts::Journalized::JournableDiffer.changes(predecessor&.data, data)
- if predecessor.nil?
- @changes.merge!(initial_journal_data_changes)
- else
- @changes.merge!(subsequent_journal_data_changes)
- end
+ @changes.merge!(
+ ::Acts::Journalized::JournableDiffer.association_changes(predecessor, self, 'attachable_journals', 'attachments',
+ :attachment_id, :filename),
+ ::Acts::Journalized::JournableDiffer.association_changes(predecessor, self, 'customizable_journals', 'custom_fields',
+ :custom_field_id, :value)
+ )
- @changes.merge!(get_association_changes(predecessor, 'attachable', 'attachments', :attachment_id, :filename))
- @changes.merge!(get_association_changes(predecessor, 'customizable', 'custom_fields', :custom_field_id, :value))
+ @changes[:cause] = [nil, cause] if cause.present?
@changes
end
-
- private
-
- def initial_journal_data_changes
- data
- .journaled_attributes
- .compact
- .inject({}) do |result, (attribute, new_value)|
- result[attribute] = [nil, new_value]
- result
- end
- end
-
- def subsequent_journal_data_changes
- ::Acts::Journalized::JournableDiffer.changes(predecessor.data, data)
- end
-
- def get_association_changes(predecessor, journal_association, association, key, value)
- journal_assoc_name = "#{journal_association}_journals"
-
- if predecessor.nil?
- send(journal_assoc_name).each_with_object({}) do |associated_journal, h|
- changed_attribute = "#{association}_#{associated_journal.send(key)}"
- new_value = associated_journal.send(value)
- h[changed_attribute] = [nil, new_value]
- end
- else
- new_journals = send(journal_assoc_name).map(&:attributes)
- old_journals = predecessor.send(journal_assoc_name).map(&:attributes)
-
- changes_on_association(new_journals, old_journals, association, key, value)
- end
- end
-
- def changes_on_association(current, predecessor, association, key, value)
- merged_journals = merge_reference_journals_by_id(current, predecessor, key.to_s, value.to_s)
-
- changes = added_references(merged_journals)
- .merge(removed_references(merged_journals))
- .merge(changed_references(merged_journals))
-
- to_changes_format(changes, association.to_s)
- end
-
- def added_references(merged_references)
- merged_references
- .select { |_, (old_value, new_value)| old_value.nil? && new_value.present? }
- end
-
- def removed_references(merged_references)
- merged_references
- .select { |_, (old_value, new_value)| old_value.present? && new_value.nil? }
- end
-
- def changed_references(merged_references)
- merged_references
- .select { |_, (old_value, new_value)| old_value.present? && new_value.present? && old_value.strip != new_value.strip }
- end
-
- def to_changes_format(references, key)
- references.each_with_object({}) do |(id, (old_value, new_value)), result|
- result["#{key}_#{id}"] = [old_value, new_value]
- end
- end
-
- def merge_reference_journals_by_id(new_journals, old_journals, id_key, value)
- all_associated_journal_ids = new_journals.map { |j| j[id_key] } | old_journals.map { |j| j[id_key] }
-
- all_associated_journal_ids.index_with do |id|
- [select_and_combine_journals(old_journals, id, id_key, value),
- select_and_combine_journals(new_journals, id, id_key, value)]
- end
- end
-
- def select_and_combine_journals(journals, id, key, value)
- selected_journals = journals.select { |j| j[key] == id }.map { |j| j[value] }
-
- if selected_journals.empty?
- nil
- else
- selected_journals.sort.join(',')
- end
- end
end