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

Simplify Forms #html_content implementation

Alexander Brandon Coles56c9b3c2a61c
diff --git a/lib/primer/open_project/forms/dsl/html_content_input.rb b/lib/primer/open_project/forms/dsl/html_content_input.rb
deleted file mode 100644
index 95e720501e7..00000000000
--- a/lib/primer/open_project/forms/dsl/html_content_input.rb
+++ /dev/null
@@ -1,28 +0,0 @@
-# frozen_string_literal: true
-
-module Primer
- module OpenProject
- module Forms
- module Dsl
- class HtmlContentInput < Primer::Forms::Dsl::Input
- def initialize(**system_arguments, &html_block)
- @html_block = html_block
-
- super(**system_arguments)
- end
-
- def to_component
- HtmlContent.new(&@html_block)
- end
-
- def type
- :html_content
- end
-
- def name = nil
- def form_control? = false
- end
- end
- end
- end
-end
diff --git a/lib/primer/open_project/forms/dsl/input_methods.rb b/lib/primer/open_project/forms/dsl/input_methods.rb
index a0c314d1a9e..7ca247fafb9 100644
--- a/lib/primer/open_project/forms/dsl/input_methods.rb
+++ b/lib/primer/open_project/forms/dsl/input_methods.rb
@@ -33,8 +33,8 @@ module Primer
add_input ColorSelectInput.new(builder:, form:, **decorate_options(**), &)
end
- def html_content(**, &)
- add_input HtmlContentInput.new(builder:, form:, **, &)
+ def html_content(&)
+ add_input HtmlContent.new(&)
end
def pattern_input(**, &)
diff --git a/lib/primer/open_project/forms/html_content.html.erb b/lib/primer/open_project/forms/html_content.html.erb
index 0d6a044df12..0b86a8d92fd 100644
--- a/lib/primer/open_project/forms/html_content.html.erb
+++ b/lib/primer/open_project/forms/html_content.html.erb
@@ -1 +1 @@
-<%= rendered_html_content %>
+<%= content %>
diff --git a/lib/primer/open_project/forms/html_content.rb b/lib/primer/open_project/forms/html_content.rb
index e25c65bf857..66b3e4b6eee 100644
--- a/lib/primer/open_project/forms/html_content.rb
+++ b/lib/primer/open_project/forms/html_content.rb
@@ -10,10 +10,8 @@ module Primer
@html_block = html_block
end
- def rendered_html_content
- @view_context.capture do
- @view_context.instance_exec(&@html_block)
- end
+ def perform_render(&)
+ super(&@html_block)
end
end
end
diff --git a/spec/lib/primer/open_project/forms/dsl/input_methods_spec.rb b/spec/lib/primer/open_project/forms/dsl/input_methods_spec.rb
index 0a69c392644..f88b3e3915e 100644
--- a/spec/lib/primer/open_project/forms/dsl/input_methods_spec.rb
+++ b/spec/lib/primer/open_project/forms/dsl/input_methods_spec.rb
@@ -121,6 +121,12 @@ RSpec.describe Primer::OpenProject::Forms::Dsl::InputMethods, type: :forms do
include_examples "input class", Primer::Forms::Separator
end
+ describe "#html_content" do
+ let(:field_group) { form_dsl.html_content { "content" } }
+
+ include_examples "input class", Primer::OpenProject::Forms::HtmlContent
+ end
+
describe "text input methods" do
describe "#text_field" do
let(:field_group) { form_dsl.text_field(name:, label:, **options) }
@@ -198,12 +204,6 @@ RSpec.describe Primer::OpenProject::Forms::Dsl::InputMethods, type: :forms do
it_behaves_like "supporting help texts"
end
- describe "#html_content" do
- let(:field_group) { form_dsl.html_content(name:, label:, **options) }
-
- include_examples "input class", Primer::OpenProject::Forms::Dsl::HtmlContentInput
- end
-
describe "#project_autocompleter" do
let(:field_group) { form_dsl.project_autocompleter(name:, label:, autocomplete_options: {}, **options) }
diff --git a/spec/lib/primer/open_project/forms/html_content_spec.rb b/spec/lib/primer/open_project/forms/html_content_spec.rb
new file mode 100644
index 00000000000..e531ffad30b
--- /dev/null
+++ b/spec/lib/primer/open_project/forms/html_content_spec.rb
@@ -0,0 +1,90 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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"
+
+RSpec.describe Primer::OpenProject::Forms::HtmlContent, type: :forms do
+ include ViewComponent::TestHelpers
+
+ let(:model) { build_stubbed(:comment) }
+
+ def render_form_in_view_context(&)
+ render_in_view_context(model) do |model|
+ primer_form_with(url: "/foo", model:) do |f|
+ render_inline_form(f, &)
+ end
+ end
+ end
+
+ context "with plain text" do
+ subject(:rendered_form) do
+ render_form_in_view_context do |f|
+ f.html_content do
+ "Hallo Welt"
+ end
+ end
+ page
+ end
+
+ it "renders the text" do
+ expect(rendered_form).to have_content "Hallo Welt"
+ end
+ end
+
+ context "with Rails helpers" do
+ subject(:rendered_form) do
+ render_form_in_view_context do |f|
+ f.html_content do
+ content_tag(:p, "Hallo Welt")
+ end
+ end
+ page
+ end
+
+ it "renders the HTML" do
+ expect(rendered_form).to have_element :p, text: "Hallo Welt"
+ end
+ end
+
+ context "with other view components" do
+ subject(:rendered_form) do
+ render_form_in_view_context do |f|
+ f.html_content do
+ render(Primer::Beta::Text.new(tag: :p, font_weight: :bold)) { "Hallo Welt" }
+ end
+ end
+ page
+ end
+
+ it "renders the HTML" do
+ expect(rendered_form).to have_element :p, text: "Hallo Welt"
+ end
+ end
+end