Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#60144] primerize wp types settings form #17704

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions app/components/work_packages/types/settings_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<%#-- 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.

++#%>

<%=
primer_form_with(**form_options) do |f|
render(WorkPackages::Types::SettingsForm.new(f))
end
%>
63 changes: 63 additions & 0 deletions app/components/work_packages/types/settings_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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 WorkPackages
module Types
class SettingsComponent < ApplicationComponent
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable

def initialize(model, copy_workflow_from: nil, **)
@copy_workflow_from = copy_workflow_from
super(model, **)
end

def form_options
Kharonus marked this conversation as resolved.
Show resolved Hide resolved
if model.new_record?
create_form_options
else
update_form_options
end
end

private

attr_reader :copy_workflow_from

def create_form_options
{ url: types_path, method: :post, model:, copy_workflow_from: }
end

def update_form_options
{ url: type_path(id: model.id), method: :patch, model: }
end
end
end
end
10 changes: 4 additions & 6 deletions app/controllers/types_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,17 @@ def edit
end
end

def create # rubocop:disable Metrics/AbcSize
def create
CreateTypeService
.new(current_user)
.call(permitted_type_params, copy_workflow_from: params[:copy_workflow_from]) do |call|
.call(permitted_type_params, copy_workflow_from: params.dig(:type, :copy_workflow_from)) do |call|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Am I missing something here (most probably yes) but since we are feeding the all the permitted type params, why do we need to the copy_workflow_from as a special ❄️?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy_workflow_from is no property from the type record.

previously (as you can see from the change) it was passed not even inside params|type but directly on top level params.

Usually, having something like this, I'd rework the whole form to use a form model, as this is apparently what the create service wants. But for sake of simplicity I tried to keep the previous approach alive. But with the primer forms I no longer was able to define the form input field outside the "model".

@type = call.result

call.on_success do
redirect_to_type_tab_path(@type, t(:notice_successful_create))
Kharonus marked this conversation as resolved.
Show resolved Hide resolved
end

call.on_failure do |result|
flash[:error] = result.errors.full_messages.join("\n")
load_projects_and_types
call.on_failure do
render action: :new, status: :unprocessable_entity
end
end
Expand Down Expand Up @@ -137,7 +135,7 @@ def load_projects_and_types

def redirect_to_type_tab_path(type, notice)
tab = params["tab"] || "settings"
redirect_to(edit_tab_type_path(type, tab:), notice:)
redirect_to(edit_tab_type_path(type, tab:), notice:, status: :see_other)
end

def render_edit_tab(type, status: :ok)
Expand Down
116 changes: 116 additions & 0 deletions app/forms/work_packages/types/settings_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# 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 WorkPackages
module Types
class SettingsForm < ApplicationForm
form do |settings_form|
settings_form.text_field(
name: :name,
label: label(:name),
placeholder: I18n.t(:label_name),
input_width: :large,
required: true,
disabled: model.is_standard?
)

settings_form.color_select_list(
name: :color_id,
label: Color.model_name.human,
caption: I18n.t("types.edit.settings.type_color_text"),
input_width: :large
)

if show_work_flow_copy?
settings_form.select_list(
name: :copy_workflow_from,
label: label(:copy_workflow_from),
include_blank: true,
input_width: :large
) do |other_types|
work_package_types.each do |type|
other_types.option(
value: type.id,
label: type.name,
selected: type.id == prefilled_copy_workflow_from
)
end
end
end

settings_form.rich_text_area(
name: :description,
label: label(:description),
input_width: :large,
rich_text_options: { showAttachments: false }
)

settings_form.check_box(
name: :is_milestone,
label: label(:is_milestone)
)

settings_form.check_box(
name: :is_in_roadmap,
label: label(:is_in_roadmap)
)

settings_form.check_box(
name: :is_default,
label: label(:is_default)
)

settings_form.submit(
name: :submit,
label: I18n.t(:button_save),
scheme: :primary
)
end

private

def label(attribute)
model.class.human_attribute_name(attribute)
end

def show_work_flow_copy?
model.new_record?
end

def work_package_types
Type.all
end

def prefilled_copy_workflow_from
@builder.options[:copy_workflow_from]
end
end
end
end
39 changes: 39 additions & 0 deletions app/helpers/conversion_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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 ConversionHelper
include Dry::Monads[:maybe]

def maybe_integer(value)
Some(Integer(value || "", 10))
rescue ArgumentError
None()
end
end
4 changes: 2 additions & 2 deletions app/helpers/types_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def types_tabs
tabs = [
{
name: "settings",
partial: "types/form/settings",
path: edit_tab_type_path(id: @type.id, tab: :settings),
label: "types.edit.settings.tab"
label: "types.edit.settings.tab",
view_component: WorkPackages::Types::SettingsComponent
},
{
name: "form_configuration",
Expand Down
75 changes: 0 additions & 75 deletions app/views/types/form/_settings.html.erb

This file was deleted.

9 changes: 4 additions & 5 deletions app/views/types/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ See COPYRIGHT and LICENSE files for more details.
end
%>

<%= error_messages_for 'type' %>

<%= form_for controller.type, builder: TabularFormBuilder, lang: current_language do |f| %>
<%= render partial: 'types/form/settings', locals: { f: f } %>
<% end %>
<%=
copy_workflow_from = maybe_integer(params.dig(:type, :copy_workflow_from)).value_or(nil)
render WorkPackages::Types::SettingsComponent.new(@type, copy_workflow_from:)
%>
2 changes: 1 addition & 1 deletion lib/primer/open_project/forms/color_select.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%= content_tag(:div, **@field_wrap_arguments) do %>
<%= builder.hidden_field :color_id %>
<%= angular_component_tag("opce-colors-autocompleter",
class: "colors-autocomplete form--select-container -middle",
class: "colors-autocomplete form--select-container",
data: {
colors:,
classes: "ng-select--primerized",
Expand Down
Loading