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

refactor: Use generic formatter for budget_id

Christophe Bliard36a4103c8d2c
diff --git a/app/models/work_package/journalized.rb b/app/models/work_package/journalized.rb
index cee4609b64e..189fc8b61f7 100644
--- a/app/models/work_package/journalized.rb
+++ b/app/models/work_package/journalized.rb
@@ -77,14 +77,6 @@ module WorkPackage::Journalized
name: JournalizedProcs.event_name,
url: JournalizedProcs.event_url
- register_journal_formatter(:cost_association) do |value, journable, field|
- association = journable.class.reflect_on_association(field.to_sym)
- if association
- record = association.class_name.constantize.find_by(id: value.to_i)
- record&.subject
- end
- end
-
register_journal_formatted_fields(:id, 'parent_id')
register_journal_formatted_fields(:fraction, 'estimated_hours', 'derived_estimated_hours')
register_journal_formatted_fields(:decimal, 'done_ratio')
@@ -92,11 +84,11 @@ module WorkPackage::Journalized
register_journal_formatted_fields(:schedule_manually, 'schedule_manually')
register_journal_formatted_fields(:attachment, /attachments_?\d+/)
register_journal_formatted_fields(:custom_field, /custom_fields_\d+/)
- register_journal_formatted_fields(:cost_association, 'budget_id')
register_journal_formatted_fields(:ignore_non_working_days, 'ignore_non_working_days')
# Joined
register_journal_formatted_fields :named_association, :parent_id, :project_id,
+ :budget_id,
:status_id, :type_id,
:assigned_to_id, :priority_id,
:category_id, :version_id,
diff --git a/lib_static/plugins/acts_as_journalized/lib/acts/journalized/format_hooks.rb b/lib_static/plugins/acts_as_journalized/lib/acts/journalized/format_hooks.rb
index 8e99b02b5d0..08a67e95cb5 100644
--- a/lib_static/plugins/acts_as_journalized/lib/acts/journalized/format_hooks.rb
+++ b/lib_static/plugins/acts_as_journalized/lib/acts/journalized/format_hooks.rb
@@ -53,25 +53,15 @@ module Acts::Journalized
module ClassMethods
# Shortcut to register a formatter for a number of fields
- def register_journal_formatted_fields(formatter, *field_names)
+ def register_journal_formatted_fields(formatter_key, *field_names)
journal_data_type = journal_class.name
field_names.each do |field|
- JournalFormatter.register_formatted_field(journal_data_type, field, formatter)
+ JournalFormatter.register_formatted_field(journal_data_type, field, formatter_key)
end
end
- # Shortcut to register a new proc as a named formatter. Overwrites
- # existing formatters with the same name
- def register_journal_formatter(formatter, klass = nil, &block)
- if block_given?
- klass = Class.new(JournalFormatter::Proc) do
- @proc = block
- end
- end
-
- raise ArgumentError 'Provide either a class or a block defining the value formatting' if klass.nil?
-
- JournalFormatter.register formatter.to_sym => klass
+ def register_journal_formatter(formatter_key, formatter_class)
+ JournalFormatter.register formatter_key.to_sym => formatter_class
end
end
end
diff --git a/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb b/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb
index 24e3c86a86f..5ec3402ba74 100644
--- a/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb
+++ b/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb
@@ -59,7 +59,6 @@ require_relative './journal_formatter/fraction'
require_relative './journal_formatter/id'
require_relative './journal_formatter/named_association'
require_relative './journal_formatter/plaintext'
-require_relative './journal_formatter/proc'
module JournalFormatter
mattr_accessor :formatters, :registered_fields
@@ -68,10 +67,10 @@ module JournalFormatter
formatters.merge!(hash)
end
- def self.register_formatted_field(journal_data_type, field, formatter)
+ def self.register_formatted_field(journal_data_type, field, formatter_key)
field_key = field.is_a?(Regexp) ? field : Regexp.new("^#{field}$")
- registered_fields[journal_data_type].merge!(field_key => formatter.to_sym)
+ registered_fields[journal_data_type].merge!(field_key => formatter_key.to_sym)
end
def self.default_formatters
diff --git a/lib_static/plugins/acts_as_journalized/lib/journal_formatter/proc.rb b/lib_static/plugins/acts_as_journalized/lib/journal_formatter/proc.rb
deleted file mode 100644
index dbf1b94d3d4..00000000000
--- a/lib_static/plugins/acts_as_journalized/lib/journal_formatter/proc.rb
+++ /dev/null
@@ -1,53 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-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 JournalFormatter
- class Proc < Attribute
- class << self
- attr_accessor :proc
- end
-
- private
-
- def format_details(key, values)
- label = label(key)
-
- old_value, value = *format_values(values, key)
-
- [label, old_value, value]
- end
-
- def format_values(values, key)
- field = key.to_s.gsub(/_id\z/, '')
-
- values.map do |value|
- self.class.proc.call value, @journal.journable, field
- end
- end
- end
-end