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

reworked custom field form due to renderig errors when using rails partials within a view_component

Jonas Jabari06bff13d6ee9
diff --git a/app/components/settings/project_custom_fields/form_component.html.erb b/app/components/settings/project_custom_fields/_form_component.html.erb
similarity index 65%
rename from app/components/settings/project_custom_fields/form_component.html.erb
rename to app/components/settings/project_custom_fields/_form_component.html.erb
index 700c6c860e3..d0219dd100d 100644
--- a/app/components/settings/project_custom_fields/form_component.html.erb
+++ b/app/components/settings/project_custom_fields/_form_component.html.erb
@@ -26,19 +26,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
See COPYRIGHT and LICENSE files for more details.
++#%>
-<%= content_tag(:div, data: {
- controller: 'admin--custom-fields',
- 'application-target': 'dynamic',
- 'admin--custom-fields-format-value': @custom_field.field_format
- }) do %>
- <%= error_messages_for 'custom_field' %>
-
- <%= labelled_tabular_form_for(@custom_field, **form_config) do |f| %>
- <%= render partial: 'custom_fields/form', locals: { f: f, custom_field: @custom_field } %>
- <% if @custom_field.new_record? %>
- <%= hidden_field_tag 'type', @custom_field.type %>
- <% end %>
- <%= styled_button_tag t(:button_save), class: '-highlight -with-icon icon-checkmark' %>
+<%= error_messages_for 'custom_field' %>
+
+<%= labelled_tabular_form_for(@custom_field, **form_config) do |f| %>
+ <%= render partial: 'custom_fields/form', locals: { f: f, custom_field: @custom_field } %>
+ <% if @custom_field.new_record? %>
+ <%= hidden_field_tag 'type', @custom_field.type %>
<% end %>
+ <%= styled_button_tag t(:button_save), class: '-highlight -with-icon icon-checkmark' %>
<% end %>
diff --git a/app/components/settings/project_custom_fields/form_component.rb b/app/components/settings/project_custom_fields/_form_component.rb
similarity index 52%
rename from app/components/settings/project_custom_fields/form_component.rb
rename to app/components/settings/project_custom_fields/_form_component.rb
index 211c8191756..9baa0135b3e 100644
--- a/app/components/settings/project_custom_fields/form_component.rb
+++ b/app/components/settings/project_custom_fields/_form_component.rb
@@ -25,32 +25,44 @@
#
# See COPYRIGHT and LICENSE files for more details.
#++
+#
+#
+#
+#
+#
+# TODO: currently not working properly when using rails partials within the template of this component
+# renders <td><tr> tags as strings instead of proper HTML tags within the custom options table
+# using plain rails views for form rendering for now (tbd!)
+#
+#
+#
+#
+#
+# module Settings
+# module ProjectCustomFields
+# class FormComponent < ApplicationComponent
+# include ApplicationHelper
+# include CustomFieldsHelper
+# include StimulusHelper
+# include ErrorMessageHelper
+# include OpenProject::FormTagHelper
+# include OpPrimer::ComponentHelpers
-module Settings
- module ProjectCustomFields
- class FormComponent < ApplicationComponent
- include ApplicationHelper
- include CustomFieldsHelper
- include StimulusHelper
- include ErrorMessageHelper
- include OpenProject::FormTagHelper
- include OpPrimer::ComponentHelpers
-
- def initialize(custom_field: ProjectCustomField.new)
- super
+# def initialize(custom_field: ProjectCustomField.new)
+# super
- @custom_field = custom_field
- end
+# @custom_field = custom_field
+# end
- private
+# private
- def form_config
- {
- as: :custom_field,
- url: @custom_field.persisted? ? admin_settings_project_custom_field_path(@custom_field) : admin_settings_project_custom_fields_path,
- html: { method: @custom_field.persisted? ? :put : :post, id: 'custom_field_form' }
- }
- end
- end
- end
-end
+# def form_config
+# {
+# as: :custom_field,
+# url: @custom_field.persisted? ? admin_settings_project_custom_field_path(@custom_field) : admin_settings_project_custom_fields_path,
+# html: { method: @custom_field.persisted? ? :put : :post, id: 'custom_field_form' }
+# }
+# end
+# end
+# end
+# end
diff --git a/app/controllers/concerns/custom_fields/shared_actions.rb b/app/controllers/concerns/custom_fields/shared_actions.rb
index be8da8be6a4..1e8265f0e76 100644
--- a/app/controllers/concerns/custom_fields/shared_actions.rb
+++ b/app/controllers/concerns/custom_fields/shared_actions.rb
@@ -31,16 +31,16 @@ module CustomFields
extend ActiveSupport::Concern
included do
- def index_path(params = {})
- if @custom_field.type == 'ProjectCustomField'
+ def index_path(custom_field, params = {})
+ if custom_field.type == 'ProjectCustomField'
admin_settings_project_custom_fields_path(**params)
else
custom_fields_path(**params)
end
end
- def edit_path(params = {})
- if @custom_field.type == 'ProjectCustomField'
+ def edit_path(custom_field, params = {})
+ if custom_field.type == 'ProjectCustomField'
admin_settings_project_custom_field_path(**params)
else
edit_custom_field_path(**params)
@@ -55,7 +55,7 @@ module CustomFields
if call.success?
flash[:notice] = t(:notice_successful_create)
call_hook(:controller_custom_fields_new_after_save, custom_field: call.result)
- redirect_to index_path(tab: call.result.class.name)
+ redirect_to index_path(call.result, tab: call.result.class.name)
else
@custom_field = call.result || new_custom_field
render action: 'new'
@@ -74,7 +74,7 @@ module CustomFields
if call.success?
flash[:notice] = t(:notice_successful_update)
call_hook(:controller_custom_fields_edit_after_save, custom_field: @custom_field)
- redirect_back_or_default(edit_path(id: @custom_field.id))
+ redirect_back_or_default(edit_path(@custom_field, id: @custom_field.id))
else
render action: 'edit'
end
@@ -98,7 +98,7 @@ module CustomFields
rescue StandardError
flash[:error] = I18n.t(:error_can_not_delete_custom_field)
end
- redirect_to index_path(tab: @custom_field.class.name)
+ redirect_to index_path(@custom_field, tab: @custom_field.class.name)
end
def delete_option
@@ -112,7 +112,7 @@ module CustomFields
flash[:error] = @custom_option.errors.full_messages
end
- redirect_to edit_path(id: @custom_field.id)
+ redirect_to edit_path(@custom_field, id: @custom_field.id)
end
def new_custom_field
diff --git a/app/views/admin/settings/project_custom_fields/edit.html.erb b/app/views/admin/settings/project_custom_fields/edit.html.erb
index b3f3bc19b06..7b24dc2c02a 100644
--- a/app/views/admin/settings/project_custom_fields/edit.html.erb
+++ b/app/views/admin/settings/project_custom_fields/edit.html.erb
@@ -26,6 +26,24 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
See COPYRIGHT and LICENSE files for more details.
++#%>
+<% content_controller 'admin--custom-fields',
+ dynamic: true,
+ 'admin--custom-fields-format-value': @custom_field.field_format
+%>
+
<%= render(Settings::ProjectCustomFields::EditFormHeaderComponent.new(custom_field: @custom_field)) %>
-<%= render(Settings::ProjectCustomFields::FormComponent.new(custom_field: @custom_field)) %>
+
+<%= error_messages_for 'custom_field' %>
+
+<%= labelled_tabular_form_for @custom_field, as: :custom_field,
+ url: admin_settings_project_custom_field_path(@custom_field),
+ html: {method: :put, id: 'custom_field_form'} do |f| %>
+ <%= render partial: 'custom_fields/form', locals: { f: f, custom_field: @custom_field } %>
+ <% if @custom_field.new_record? %>
+ <%= hidden_field_tag 'type', @custom_field.type %>
+ <% end %>
+ <%= styled_button_tag t(:button_save), class: '-highlight -with-icon icon-checkmark' %>
+<% end %>
+
+
diff --git a/app/views/admin/settings/project_custom_fields/new.html.erb b/app/views/admin/settings/project_custom_fields/new.html.erb
index bc254c2f1cd..781470cc8a9 100644
--- a/app/views/admin/settings/project_custom_fields/new.html.erb
+++ b/app/views/admin/settings/project_custom_fields/new.html.erb
@@ -26,5 +26,21 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
See COPYRIGHT and LICENSE files for more details.
++#%>
-<%= render(Settings::ProjectCustomFields::NewFormHeaderComponent.new()) %>
-<%= render(Settings::ProjectCustomFields::FormComponent.new(custom_field: @custom_field)) %>
+<% content_controller 'admin--custom-fields',
+ dynamic: true,
+ 'admin--custom-fields-format-value': @custom_field.field_format
+%>
+
+<%= render(Settings::ProjectCustomFields::NewFormHeaderComponent.new) %>
+
+<%= error_messages_for 'custom_field' %>
+
+<%= labelled_tabular_form_for @custom_field, as: :custom_field,
+ url: admin_settings_project_custom_fields_path,
+ html: { id: 'custom_field_form' } do |f| %>
+ <%= render partial: 'custom_fields/form', locals: { f: f, custom_field: @custom_field } %>
+ <% if @custom_field.new_record? %>
+ <%= hidden_field_tag 'type', @custom_field.type %>
+ <% end %>
+ <%= styled_button_tag t(:button_save), class: '-highlight -with-icon icon-checkmark' %>
+<% end %>
diff --git a/app/views/custom_fields/_form.html.erb b/app/views/custom_fields/_form.html.erb
index 7b206824642..a137f0431fe 100644
--- a/app/views/custom_fields/_form.html.erb
+++ b/app/views/custom_fields/_form.html.erb
@@ -26,10 +26,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
See COPYRIGHT and LICENSE files for more details.
++#%>
-<!-- Enable reusing this form partial within a view_component which does not pass instance vars directly -->
-<!-- custom_field is passed via the local api when rendering the parial from a view_component -->
-<% @custom_field = custom_field if @custom_field.nil? %>
-
<section class="form--section" id="custom_field_form">
<div class="form--field -required" id="custom_field_name_attributes">
<%= f.text_field :name, required: true, container_class: '-middle' %>
@@ -105,15 +101,15 @@ See COPYRIGHT and LICENSE files for more details.
<% end %>
<div class="form--field">
<%= render partial: "custom_fields/custom_options", locals: { custom_field: @custom_field, f: f } %>
- <button
- type="button"
- class="spot-link"
- data-test-selector="add-custom-option"
- data-action="admin--custom-fields#addOption"
- >
- <span class="spot-icon spot-icon_1_25 spot-icon_add"></span>
- <span><%= t(:button_add) %></span>
- </button>
+ <button
+ type="button"
+ class="spot-link"
+ data-test-selector="add-custom-option"
+ data-action="admin--custom-fields#addOption"
+ >
+ <span class="spot-icon spot-icon_1_25 spot-icon_add"></span>
+ <span><%= t(:button_add) %></span>
+ </button>
</div>
</fieldset>
<% end %>