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 with better names and simplified logic

Christophe Bliard47acc2c93242
diff --git a/app/models/project.rb b/app/models/project.rb
index 28eb7fa243e..76757ab893f 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -104,13 +104,13 @@ class Project < ApplicationRecord
author: nil,
datetime: :created_at
- register_on_journal_formatter(:active_status, 'active')
- register_on_journal_formatter(:template, 'templated')
- register_on_journal_formatter(:plaintext, 'identifier')
- register_on_journal_formatter(:plaintext, 'name')
- register_on_journal_formatter(:diff, 'description')
- register_on_journal_formatter(:visibility, 'public')
- register_on_journal_formatter(:parent, 'parent_id')
+ register_journal_formatted_fields(:active_status, 'active')
+ register_journal_formatted_fields(:template, 'templated')
+ register_journal_formatted_fields(:plaintext, 'identifier')
+ register_journal_formatted_fields(:plaintext, 'name')
+ register_journal_formatted_fields(:diff, 'description')
+ register_journal_formatted_fields(:visibility, 'public')
+ register_journal_formatted_fields(:parent, 'parent_id')
has_paper_trail
diff --git a/app/models/setting.rb b/app/models/setting.rb
index 5ec0ec19e34..6503a8b63f9 100644
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -346,7 +346,7 @@ class Setting < ApplicationRecord
if definition.serialized? && value.is_a?(String)
YAML::safe_load(value, permitted_classes: [Symbol, ActiveSupport::HashWithIndifferentAccess, Date, Time, URI::Generic])
.tap { |maybe_hash| normalize_hash!(maybe_hash) if maybe_hash.is_a?(Hash) }
- elsif value != '' && !value.nil?
+ elsif value != ''.freeze && !value.nil?
read_formatted_setting(value, definition.format)
else
definition.format == :string ? value : nil
diff --git a/app/models/work_package/journalized.rb b/app/models/work_package/journalized.rb
index de026a52d69..cee4609b64e 100644
--- a/app/models/work_package/journalized.rb
+++ b/app/models/work_package/journalized.rb
@@ -85,25 +85,24 @@ module WorkPackage::Journalized
end
end
- register_on_journal_formatter(:id, 'parent_id')
- register_on_journal_formatter(:fraction, 'estimated_hours')
- register_on_journal_formatter(:fraction, 'derived_estimated_hours')
- register_on_journal_formatter(:decimal, 'done_ratio')
- register_on_journal_formatter(:diff, 'description')
- register_on_journal_formatter(:schedule_manually, 'schedule_manually')
- register_on_journal_formatter(:attachment, /attachments_?\d+/)
- register_on_journal_formatter(:custom_field, /custom_fields_\d+/)
- register_on_journal_formatter(:cost_association, 'budget_id')
- register_on_journal_formatter(:ignore_non_working_days, 'ignore_non_working_days')
+ register_journal_formatted_fields(:id, 'parent_id')
+ register_journal_formatted_fields(:fraction, 'estimated_hours', 'derived_estimated_hours')
+ register_journal_formatted_fields(:decimal, 'done_ratio')
+ register_journal_formatted_fields(:diff, 'description')
+ 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_on_journal_formatter :named_association, :parent_id, :project_id,
- :status_id, :type_id,
- :assigned_to_id, :priority_id,
- :category_id, :version_id,
- :author_id, :responsible_id
- register_on_journal_formatter :datetime, :start_date, :due_date
- register_on_journal_formatter :plaintext, :subject
- register_on_journal_formatter :day_count, :duration
+ register_journal_formatted_fields :named_association, :parent_id, :project_id,
+ :status_id, :type_id,
+ :assigned_to_id, :priority_id,
+ :category_id, :version_id,
+ :author_id, :responsible_id
+ register_journal_formatted_fields :datetime, :start_date, :due_date
+ register_journal_formatted_fields :plaintext, :subject
+ register_journal_formatted_fields :day_count, :duration
end
end
diff --git a/lib_static/plugins/acts_as_journalized/lib/acts/journalized/data_class.rb b/lib_static/plugins/acts_as_journalized/lib/acts/journalized/data_class.rb
index 3f68855be77..835c9e01147 100644
--- a/lib_static/plugins/acts_as_journalized/lib/acts/journalized/data_class.rb
+++ b/lib_static/plugins/acts_as_journalized/lib/acts/journalized/data_class.rb
@@ -15,15 +15,9 @@ module Acts::Journalized
if namespace == 'Journal'
self
else
- "Journal::#{journal_class_name}".constantize
+ "Journal::#{base_class.name}Journal".constantize
end
end
-
- private
-
- def journal_class_name
- "#{base_class.name}Journal"
- end
end
end
end
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 826a311057b..8e99b02b5d0 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,11 +53,10 @@ module Acts::Journalized
module ClassMethods
# Shortcut to register a formatter for a number of fields
- def register_on_journal_formatter(formatter, *field_names)
- formatter = formatter.to_sym
- journal_class = self.journal_class
+ def register_journal_formatted_fields(formatter, *field_names)
+ journal_data_type = journal_class.name
field_names.each do |field|
- JournalFormatter.register_formatted_field(journal_class.name.to_sym, field, formatter)
+ JournalFormatter.register_formatted_field(journal_data_type, field, formatter)
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 b4135a99113..24e3c86a86f 100644
--- a/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb
+++ b/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb
@@ -65,19 +65,13 @@ module JournalFormatter
mattr_accessor :formatters, :registered_fields
def self.register(hash)
- if hash[:class]
- klazz = hash.delete(:class)
-
- register_formatted_field(klazz, hash.keys.first, hash.values.first)
- else
- formatters.merge!(hash)
- end
+ formatters.merge!(hash)
end
- def self.register_formatted_field(klass, field, formatter)
+ def self.register_formatted_field(journal_data_type, field, formatter)
field_key = field.is_a?(Regexp) ? field : Regexp.new("^#{field}$")
- registered_fields[klass].merge!(field_key => formatter)
+ registered_fields[journal_data_type].merge!(field_key => formatter.to_sym)
end
def self.default_formatters
@@ -91,50 +85,54 @@ module JournalFormatter
end
self.formatters = default_formatters
- self.registered_fields = Hash.new do |hash, klass|
- hash[klass] = {}
+ self.registered_fields = Hash.new do |hash, journal_data_type|
+ hash[journal_data_type] = {}
end
def render_detail(detail, options = {})
options = options.reverse_merge(html: true, only_path: true, cache: JournalFormatterCache.request_instance)
if detail.respond_to? :to_ary
- key = detail.first
+ field = detail.first
values = detail.last
else
- key = detail
- values = details[key.to_s]
+ field = detail
+ values = details[field.to_s]
end
- formatter = formatter_instance(key.to_s)
+ formatter = formatter_instance(field)
return if formatter.nil?
- formatter.render(key, values, options).html_safe # rubocop:disable Rails/OutputSafety
+ formatter.render(field, values, options).html_safe # rubocop:disable Rails/OutputSafety
end
- def formatter_instance(formatter_key)
+ def formatter_instance(field)
# Some attributes on a model are named dynamically.
# This is especially true for associations created by plugins.
# Those are sometimes named according to the schema "association_name[n]" or
# "association_name_[n]" where n is an integer representing an id.
# Using regexp we are able to handle those fields with the rest.
- formatter_type = data.class.to_s.to_sym
- formatter = lookup_formatter formatter_key, formatter_type
+ formatter_key = lookup_formatter_key(field)
+
+ formatter_instances[formatter_key] if formatter_key
+ end
- formatter_instances(formatter_type)[formatter] unless formatter.nil?
+ def journal_data_type
+ data_type
end
- def lookup_formatter(formatter_key, formatter_type)
+ def lookup_formatter_key(field)
JournalFormatter
- .registered_fields[formatter_type].keys
- .detect { |k| formatter_key.match(k) }
+ .registered_fields[journal_data_type]
+ .find { |regexp, _formatter_key| field.match(regexp) }
+ .then { |_regexp, formatter_key| formatter_key }
end
- def formatter_instances(formatter_type)
- @formatter_instances ||= Hash.new do |hash, key|
- f = JournalFormatter.formatters[JournalFormatter.registered_fields[formatter_type][key]]
- hash[key] = f.new(self)
+ def formatter_instances
+ @formatter_instances ||= Hash.new do |hash, formatter_key|
+ formatter_class = JournalFormatter.formatters[formatter_key]
+ hash[formatter_key] = formatter_class.new(self)
end
end
end
diff --git a/modules/backlogs/lib/open_project/backlogs/patches/work_package_patch.rb b/modules/backlogs/lib/open_project/backlogs/patches/work_package_patch.rb
index bc6ca9f5bc1..111612a982e 100644
--- a/modules/backlogs/lib/open_project/backlogs/patches/work_package_patch.rb
+++ b/modules/backlogs/lib/open_project/backlogs/patches/work_package_patch.rb
@@ -35,9 +35,9 @@ module OpenProject::Backlogs::Patches::WorkPackagePatch
before_validation :backlogs_before_validation, if: lambda { backlogs_enabled? }
- register_on_journal_formatter(:fraction, 'remaining_hours')
- register_on_journal_formatter(:decimal, 'story_points')
- register_on_journal_formatter(:decimal, 'position')
+ register_journal_formatted_fields(:fraction, 'remaining_hours')
+ register_journal_formatted_fields(:decimal, 'story_points')
+ register_journal_formatted_fields(:decimal, 'position')
validates_numericality_of :story_points, only_integer: true,
allow_nil: true,
diff --git a/modules/meeting/app/models/meeting.rb b/modules/meeting/app/models/meeting.rb
index 71273e94797..c03be3a922a 100644
--- a/modules/meeting/app/models/meeting.rb
+++ b/modules/meeting/app/models/meeting.rb
@@ -64,10 +64,10 @@ class Meeting < ApplicationRecord
author: Proc.new(&:user),
description: ''
- register_on_journal_formatter(:plaintext, 'title')
- register_on_journal_formatter(:fraction, 'duration')
- register_on_journal_formatter(:datetime, 'start_time')
- register_on_journal_formatter(:plaintext, 'location')
+ register_journal_formatted_fields(:plaintext, 'title')
+ register_journal_formatted_fields(:fraction, 'duration')
+ register_journal_formatted_fields(:datetime, 'start_time')
+ register_journal_formatted_fields(:plaintext, 'location')
accepts_nested_attributes_for :participants, allow_destroy: true