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

Rewrite #format_text to use keyword params

Alexander Brandon Colescf5c4f752df1
diff --git a/app/views/account/_user_consent_check.html.erb b/app/views/account/_user_consent_check.html.erb
index 782a9fc5340..992923325e5 100644
--- a/app/views/account/_user_consent_check.html.erb
+++ b/app/views/account/_user_consent_check.html.erb
@@ -1,6 +1,6 @@
<section class="form--section op-user-consent-form">
<p>
- <%= format_text(user_consent_instructions(::I18n.locale), { target: "_blank" }) %>
+ <%= format_text(user_consent_instructions(::I18n.locale), target: "_blank") %>
</p>
<label class="form--label-with-check-box -required -no-ellipsis op-user-consent-form--agreement">
diff --git a/lib/open_project/text_formatting.rb b/lib/open_project/text_formatting.rb
index 8147e6f712b..dc4a1f2640f 100644
--- a/lib/open_project/text_formatting.rb
+++ b/lib/open_project/text_formatting.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -39,75 +41,72 @@ module OpenProject
module TextFormatting
include ::OpenProject::TextFormatting::Truncation
- # @!macro format_text_options
- # @param [Hash] options a customizable set of options.
- # @option options [Project] :project (@project, object#project)
- # a Project context.
- # @option options [Boolean] :only_path (true)
- # whether to generate links with relative URLs.
- # @option options [User] :current_user (User.current)
- # the current user context.
- # @option options [:plain, :rich] :format (:rich)
- # the text format.
+ # @!macro format_text_params
+ # @param [Project] project a Project context.
+ # @param [Boolean] only_path whether to generate links with relative URLs.
+ # @param [User] current_user the current user context.
+ # @param [:plain, :rich] format the text format.
# `:plain` will return plain text.
# `:rich` will render raw Markdown as HTML.
+ # @param ** [Hash] additional context to pass to the underlying rendering
+ # pipeline.
+
+ # rubocop:disable Layout/LineLength
##
- # Formats text according to system settings and provided options.
+ # Formats text according to system settings and provided params.
+ #
+ # @overload format_text(text, object: nil, project: @project || object.try(:project), only_path: true, current_user: User.current, format: :rich, **)
#
- # @overload format_text(text, options = {})
# @param [String] text the raw text to be formatted, typically Markdown.
- # @macro format_text_options
- # @option options [Object] :object an object context.
+ # @param [Object] object an object context.
+ # @macro format_text_params
#
# @example Setting a project context explicitly
# format_text("## Hello world", project: current_project)
# @example Generating links with full URLs
# format_text("[Projects](/projects)", only_path: false)
#
- # @overload format_text(object, attribute, options = {})
+ # @overload format_text(object, attribute, project: @project || object.try(:project), only_path: true, current_user: User.current, format: :rich, **)
+ #
# @param [Object] object an object, typically a model
# (i.e. `ActiveRecord::Base` descendent).
# @param [Symbol] attribute the method on that object.
# `#to_s` will be called on the return value.
- # @macro format_text_options
+ # @macro format_text_params
#
# @example
# format_text(issue, :description, options)
#
# @return [String] the formatted text as an HTML-safe String.
- def format_text(*args)
- options = args.last.is_a?(Hash) ? args.pop : {}
+ def format_text(*args, object: nil, project: nil, only_path: true, current_user: User.current, format: :rich, **)
case args.size
when 1
attribute = nil
- object = options[:object]
- text = args.shift
+ text = args.first
when 2
- object = args.shift
- attribute = args.shift
- text = object.send(attribute).to_s
+ object, attribute = args
+ text = object.public_send(attribute).to_s
else
raise ArgumentError, "invalid arguments to format_text"
end
return "" if text.blank?
- project = options.delete(:project) { @project || object.try(:project) }
- only_path = options.delete(:only_path) != false
- current_user = options.delete(:current_user) { User.current }
-
- plain = ::OpenProject::TextFormatting::Formats.plain?(options.delete(:format))
+ project ||= @project || object.try(:project)
+ plain = Formats.plain?(format)
- Renderer.format_text text,
- options.merge(
- plain:,
- object:,
- request: try(:request),
- current_user:,
- attribute:,
- only_path:,
- project:
- )
+ Renderer.format_text(
+ text,
+ **,
+ plain:,
+ object:,
+ request: try(:request),
+ current_user:,
+ attribute:,
+ only_path:,
+ project:
+ )
end
+ # rubocop:enable Layout/LineLength
end
end
diff --git a/spec/lib/open_project/text_formatting/markdown/expected_markdown.rb b/spec/lib/open_project/text_formatting/markdown/expected_markdown.rb
index c28315fbc53..0b4ca468e25 100644
--- a/spec/lib/open_project/text_formatting/markdown/expected_markdown.rb
+++ b/spec/lib/open_project/text_formatting/markdown/expected_markdown.rb
@@ -15,7 +15,7 @@ end
RSpec.shared_examples_for "format_text produces" do
let(:passed_options) { defined?(options) ? options : {} }
- subject { format_text(raw, passed_options) }
+ subject { format_text(raw, **passed_options) }
it "the expected output" do
expect(subject)