Files
openproject/app/controllers/projects_controller.rb
T
Alexander Brandon Coles e225422ccd [#63593] Primerize Project create form
Updates the form visuals according to new wireframes: the Project create
form now only includes required (built-in and custom) fields, along with
the "Subproject of" field.

In detail:

* Introduces `Projects::NewComponent` and `TemplateSelectComponent`.
* Adds, extends necessary Primer forms with basic specs.
* Adds `ProjectsController#create` action, routes and permissions.
* Adds `highlight-when-value-selected` Stimulus controller.
* Updates project feature specs for new form, including rewriting some
  expectations that were reliant on `data-qa-name` attribute that Primer
  form components do not render by default.
* Removes now obsolete Project Status administation feature spec.
2025-06-11 16:31:30 +01:00

247 lines
7.4 KiB
Ruby

#-- 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.
#++
class ProjectsController < ApplicationController
include OpTurbo::ComponentStream
menu_item :overview
menu_item :roadmap, only: :roadmap
before_action :find_project, except: %i[index new create export_list_modal]
before_action :load_query_or_deny_access, only: %i[index export_list_modal]
before_action :authorize, only: %i[copy deactivate_work_package_attachments]
before_action :authorize_global, only: %i[new create]
before_action :require_admin, only: %i[destroy destroy_info]
before_action :find_optional_template, only: %i[new create]
before_action :find_optional_parent, only: :new
no_authorization_required! :index, :export_list_modal
include SortHelper
include PaginationHelper
include QueriesHelper
include ProjectsHelper
include Queries::Loading
include OpTurbo::DialogStreamHelper
helper_method :has_managed_project_folders?
current_menu_item :index do
:projects
end
def index # rubocop:disable Metrics/AbcSize
respond_to do |format|
format.html do
flash.now[:error] = @query.errors.full_messages if @query.errors.any?
render layout: "global", locals: { query: @query, state: :show }
end
format.any(*supported_export_formats) do
export_list(@query, request.format.symbol)
end
format.turbo_stream do
replace_via_turbo_stream(
component: Projects::IndexPageHeaderComponent.new(query: @query, current_user:, state: :show, params:)
)
update_via_turbo_stream(
component: Filter::FilterButtonComponent.new(query: @query, disable_buttons: false)
)
replace_via_turbo_stream(component: Projects::TableComponent.new(query: @query, current_user:, params:))
current_url = url_for(params.permit(:controller, :action, :query_id, :filters, :columns, :sortBy, :page, :per_page))
turbo_streams << turbo_stream.push_state(current_url)
turbo_streams << turbo_stream.turbo_frame_set_src(
"projects_sidemenu",
projects_menu_url(query_id: @query.id, controller_path: "projects")
)
turbo_streams << turbo_stream.replace("flash-messages", helpers.render_flash_messages)
render turbo_stream: turbo_streams
end
end
end
def new
if from_template?
new_from_template
else
new_blank
end
end
def create
if from_template?
create_from_template
else
create_blank
end
end
def copy
render
end
# Delete @project
def destroy
service_call = ::Projects::ScheduleDeletionService
.new(user: current_user, model: @project)
.call
if service_call.success?
flash[:notice] = I18n.t("projects.delete.scheduled")
else
flash[:error] = I18n.t("projects.delete.schedule_failed", errors: service_call.errors.full_messages.join("\n"))
end
redirect_to projects_path
end
def destroy_info
@project_to_destroy = @project
hide_project_in_layout
end
def deactivate_work_package_attachments
call = Projects::UpdateService
.new(user: current_user, model: @project, contract_class: Projects::SettingsContract)
.call(deactivate_work_package_attachments: params[:value] != "1")
if call.failure?
render json: call.errors.full_messages.join(" "), status: :unprocessable_entity
else
head :no_content
end
end
def export_list_modal
respond_with_dialog Projects::ExportListModalComponent.new(query: @query)
end
private
def from_template? = @template.present?
def new_blank
@new_project = @parent&.children&.build || Project.new
render layout: "no_menu"
end
def new_from_template
@copy_options = Projects::CopyOptions.new
@new_project = Projects::CopyService
.new(user: current_user, source: @template, contract_options: { validate_model: false })
.call(target_project_params: params.permit(:parent_id).to_h, attributes_only: true)
.result
render layout: "no_menu"
end
def create_blank
service_call = Projects::CreateService
.new(user: current_user)
.call(permitted_params.project)
@new_project = service_call.result
if service_call.success?
redirect_to project_path(@new_project), notice: I18n.t(:notice_successful_create)
else
flash.now[:error] = I18n.t(:notice_unsuccessful_create_with_reason, reason: service_call.message)
render action: :new, status: :unprocessable_entity
end
end
def create_from_template # rubocop:disable Metrics/AbcSize
@copy_options = Projects::CopyOptions.new(copy_options_params)
service_call = Projects::EnqueueCopyService
.new(user: current_user, model: @template)
.call(
target_project_params: permitted_params.project.to_h,
only: @copy_options.dependencies,
send_notifications: @copy_options.send_notifications
)
if service_call.success?
job = service_call.result
redirect_to job_status_path(job.job_id)
else
@new_project = service_call.result
flash.now[:error] = I18n.t(:notice_unsuccessful_create_with_reason, reason: service_call.message)
render action: :new, status: :unprocessable_entity
end
end
def find_optional_template
@template = Project.templated.visible(current_user).find(params[:template_id]) if params[:template_id].present?
end
def find_optional_parent
@parent = Project.visible(current_user).find(params[:parent_id]) if params[:parent_id].present?
end
def has_managed_project_folders?(project)
project.project_storages.any?(&:project_folder_automatic?)
end
def hide_project_in_layout
@project = nil
end
def export_list(query, mime_type)
job = Projects::ExportJob.perform_later(
export: Projects::Export.create,
user: current_user,
mime_type:,
query: query.to_hash
)
if request.headers["Accept"]&.include?("application/json")
render json: { job_id: job.job_id }
else
redirect_to job_status_path(job.job_id)
end
end
def supported_export_formats
::Exports::Register.list_formats(Project).map(&:to_s)
end
def copy_options_params
params.expect(copy_options: [[dependencies: []], :send_notifications])
end
helper_method :supported_export_formats
end