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

Extract OpPrimer::ConditionalLayoutComponent

Alexander Brandon Colesadad3b295e68
diff --git a/app/components/op_primer/conditional_layout_component.rb b/app/components/op_primer/conditional_layout_component.rb
new file mode 100644
index 00000000000..b00613384e5
--- /dev/null
+++ b/app/components/op_primer/conditional_layout_component.rb
@@ -0,0 +1,65 @@
+# 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.
+#++
+
+module OpPrimer
+ # Conditionally renders a `Primer::Alpha::Layout` around the given content. If
+ # the given condition is true, the component will render around the content.
+ # If the condition is false, only the content is rendered.
+ class ConditionalLayoutComponent < Primer::Component # rubocop:disable OpenProject/AddPreviewForViewComponent
+ delegate :with_sidebar, to: :@layout
+
+ # @param condition [Boolean] Whether or not to wrap the content in a Layout component.
+ # @param layout_component_args [Hash] The arguments to pass to the Layout component.
+ def initialize(condition:, **layout_component_args)
+ super
+
+ @condition = condition
+ @layout = Primer::Alpha::Layout.new(**layout_component_args)
+ end
+
+ def call
+ return content unless @condition
+
+ render @layout
+ end
+
+ private
+
+ def render?
+ content?
+ end
+
+ def before_render
+ content
+
+ @layout.with_main { content }
+ end
+ end
+end
diff --git a/modules/overviews/app/components/overviews/show_component.html.erb b/modules/overviews/app/components/overviews/show_component.html.erb
index 92f82514d77..571fa3d1be3 100644
--- a/modules/overviews/app/components/overviews/show_component.html.erb
+++ b/modules/overviews/app/components/overviews/show_component.html.erb
@@ -47,33 +47,27 @@ See COPYRIGHT and LICENSE files for more details.
%>
<%=
- if sidebar_enabled?
- render(Primer::Alpha::Layout.new) do |component|
- component.with_main do
- angular_component_tag "opce-overview"
- end
-
- component.with_sidebar(width: :narrow, col_placement: :end) do
- if life_cycle_sidebar_enabled?
- concat(
- render_sidebar_turbo_frame(
- "project-life-cycle-sidebar",
- src: project_life_cycle_sidebar_path(project)
- )
+ render(OpPrimer::ConditionalLayoutComponent.new(condition: sidebar_enabled?)) do |component|
+ component.with_sidebar(width: :narrow, col_placement: :end) do
+ if life_cycle_sidebar_enabled?
+ concat(
+ render_sidebar_turbo_frame(
+ "project-life-cycle-sidebar",
+ src: project_life_cycle_sidebar_path(project)
)
- end
+ )
+ end
- if custom_fields_sidebar_enabled?
- concat(
- render_sidebar_turbo_frame(
- "project-custom-fields-sidebar",
- src: project_custom_fields_sidebar_path(project)
- )
+ if custom_fields_sidebar_enabled?
+ concat(
+ render_sidebar_turbo_frame(
+ "project-custom-fields-sidebar",
+ src: project_custom_fields_sidebar_path(project)
)
- end
+ )
end
end
- else
+
angular_component_tag "opce-overview"
end
%>
diff --git a/spec/components/op_primer/conditional_layout_component_spec.rb b/spec/components/op_primer/conditional_layout_component_spec.rb
new file mode 100644
index 00000000000..5e3e91282cd
--- /dev/null
+++ b/spec/components/op_primer/conditional_layout_component_spec.rb
@@ -0,0 +1,91 @@
+# 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 "rails_helper"
+
+RSpec.describe OpPrimer::ConditionalLayoutComponent, type: :component do
+ def render_component(*, **, &)
+ render_inline(described_class.new(*, **), &)
+ end
+
+ let(:sidebar_content) { "sidebar" }
+ let(:main_content) { "content" }
+
+ subject(:rendered_component) do
+ render_component(condition:) do |component|
+ component.with_sidebar do
+ sidebar_content
+ end
+
+ main_content
+ end
+ end
+
+ context "when content is empty" do
+ let(:condition) { false }
+ let(:main_content) { "" }
+
+ it "renders nothing" do
+ expect(rendered_component.to_s).to be_blank
+ end
+ end
+
+ context "when condition is false" do
+ let(:condition) { false }
+
+ it "does not render layout" do
+ expect(rendered_component).to have_no_css ".Layout"
+ end
+
+ it "renders content" do
+ expect(rendered_component).to have_text "content"
+ end
+
+ it "does not render sidebar" do
+ expect(rendered_component).to have_no_css ".Layout-sidebar"
+ end
+ end
+
+ context "when condition is true" do
+ let(:condition) { true }
+
+ it "renders layout" do
+ expect(rendered_component).to have_css ".Layout"
+ end
+
+ it "renders content" do
+ expect(rendered_component).to have_css ".Layout-main", text: "content"
+ end
+
+ it "renders sidebar" do
+ expect(rendered_component).to have_css ".Layout-sidebar"
+ end
+ end
+end