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

[#61619] do not use params in view component

Eric Schubert2896eb47488c
diff --git a/app/components/work_packages/types/subject_configuration_component.rb b/app/components/work_packages/types/subject_configuration_component.rb
index 8cdba994358..4df87d4e9b7 100644
--- a/app/components/work_packages/types/subject_configuration_component.rb
+++ b/app/components/work_packages/types/subject_configuration_component.rb
@@ -34,6 +34,11 @@ module WorkPackages
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable
+ def initialize(model, subject_configuration_form_data: nil, **)
+ @subject_configuration_form_data = subject_configuration_form_data
+ super(model, **)
+ end
+
def form_options
form_model = subject_form_object
@@ -63,8 +68,8 @@ module WorkPackages
end
def subject_configuration_form_values
- if params[:types_forms_subject_configuration_form_model].present?
- params[:types_forms_subject_configuration_form_model]
+ if @subject_configuration_form_data.present?
+ @subject_configuration_form_data
else
persisted_subject_pattern = model.patterns.subject || ::Types::Pattern.new(blueprint: "", enabled: false)
subject_configuration = persisted_subject_pattern.enabled ? :generated : :manual
diff --git a/app/views/types/edit.html.erb b/app/views/types/edit.html.erb
index a1be4f7e9a7..b631640a0f5 100644
--- a/app/views/types/edit.html.erb
+++ b/app/views/types/edit.html.erb
@@ -35,9 +35,12 @@ See COPYRIGHT and LICENSE files for more details.
<%=
selected_tab = selected_tab(tabs)
+ form_data = {
+ subject_configuration_form_data: params[:types_forms_subject_configuration_form_model]
+ }
if selected_tab.key?(:view_component)
- render selected_tab[:view_component].new(@type)
+ render selected_tab[:view_component].new(@type, **form_data)
else
form_for @type,
url: update_tab_type_path(id: @type.id, tab: @tab),
diff --git a/spec/components/work_packages/types/subject_configuration_component_spec.rb b/spec/components/work_packages/types/subject_configuration_component_spec.rb
index a8be84773d4..18b2900cee1 100644
--- a/spec/components/work_packages/types/subject_configuration_component_spec.rb
+++ b/spec/components/work_packages/types/subject_configuration_component_spec.rb
@@ -32,9 +32,10 @@ require "rails_helper"
RSpec.describe WorkPackages::Types::SubjectConfigurationComponent, type: :component do
let(:type) { create(:type) }
+ let(:subject_configuration_form_data) { nil }
subject(:render_component) do
- render_inline(described_class.new(type))
+ render_inline(described_class.new(type, subject_configuration_form_data:))
end
context "when enterprise edition is activated", with_ee: %i[work_package_subject_generation] do
@@ -75,13 +76,12 @@ RSpec.describe WorkPackages::Types::SubjectConfigurationComponent, type: :compon
context "if component is rendered with form values in parameters" do
before do
- allow_any_instance_of(described_class).to receive(:params).and_return(params)
render_component
end
context "if work package type has subject pattern configured, but form params have option manual selected" do
let(:type) { create(:type, patterns: { subject: { blueprint: "Created by {{assignee}}", enabled: true } }) }
- let(:params) { { types_forms_subject_configuration_form_model: { subject_configuration: :manual } } }
+ let(:subject_configuration_form_data) { { subject_configuration: :manual } }
it "must have manual option checked" do
expect(page.find("input[type=radio][value=manual]")).to be_checked
@@ -89,11 +89,10 @@ RSpec.describe WorkPackages::Types::SubjectConfigurationComponent, type: :compon
end
context "if work package type has no subject pattern configured, but form params have a pattern" do
- let(:params) do
+ let(:subject_configuration_form_data) do
{
- types_forms_subject_configuration_form_model: {
- subject_configuration: :generated, pattern: "Created by {{assignee}}"
- }
+ subject_configuration: :generated,
+ pattern: "Created by {{assignee}}"
}
end