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 3 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
%>
52 changes: 52 additions & 0 deletions app/components/work_packages/types/settings_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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, **options)
@copy_workflow_from = options[:copy_workflow_from].to_i
Kharonus marked this conversation as resolved.
Show resolved Hide resolved
super
end

def form_options
Kharonus marked this conversation as resolved.
Show resolved Hide resolved
{
url: model.new_record? ? types_path : type_path(id: model.id),
method: model.new_record? ? :post : :patch,
model:,
copy_workflow_from: @copy_workflow_from
}
end
end
end
end
8 changes: 3 additions & 5 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
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
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.

12 changes: 7 additions & 5 deletions app/views/types/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ 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 %>
<%=
render WorkPackages::Types::SettingsComponent.new(
@type,
copy_workflow_from: params.dig(:type, :copy_workflow_from),
params:
Kharonus marked this conversation as resolved.
Show resolved Hide resolved
)
%>
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
21 changes: 12 additions & 9 deletions spec/controllers/types_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
is_for_all: true)
end
let(:custom_field_2) { create(:work_package_custom_field) }
let(:status_0) { create(:status) }
let(:status_1) { create(:status) }
let(:status_old) { create(:status) }
let(:status_new) { create(:status) }

context "with an unauthorized account" do
let(:current_user) { create(:user) }
Expand Down Expand Up @@ -174,16 +174,20 @@
let!(:existing_type) { create(:type, name: "Existing type") }
let!(:workflow) do
create(:workflow,
old_status: status_0,
new_status: status_1,
old_status: status_old,
new_status: status_new,
type_id: existing_type.id)
end

let(:params) do
{ "type" => { name: "New type",
project_ids: { "1" => project.id },
custom_field_ids: { "1" => custom_field_1.id, "2" => custom_field_2.id } },
"copy_workflow_from" => existing_type.id }
{
"type" => {
name: "New type",
project_ids: { "1" => project.id },
custom_field_ids: { "1" => custom_field_1.id, "2" => custom_field_2.id },
copy_workflow_from: existing_type.id
}
}
end

before do
Expand Down Expand Up @@ -218,7 +222,6 @@

it { expect(response).to have_http_status(:ok) }
it { expect(response).to render_template "edit" }
it { expect(response).to render_template "types/form/_settings" }
it { expect(response.body).to have_css "input[@name='type[name]'][@value='My type']" }
it { expect(response.body).to have_css "input[@name='type[is_milestone]'][@value='1'][@checked='checked']" }
end
Expand Down
Loading
Loading