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

Refactored to a service based approach as suggested by @dombesz

Jonas Jabarib8a07a95e25d
diff --git a/app/contracts/project_custom_field_sections/base_contract.rb b/app/contracts/project_custom_field_sections/base_contract.rb
new file mode 100644
index 00000000000..951c64d1e39
--- /dev/null
+++ b/app/contracts/project_custom_field_sections/base_contract.rb
@@ -0,0 +1,37 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 ProjectCustomFieldSections
+ class BaseContract < ::ModelContract
+ include RequiresAdminGuard
+
+ attribute :name
+ attribute :position
+ attribute :type
+ end
+end
diff --git a/app/contracts/project_custom_field_sections/create_contract.rb b/app/contracts/project_custom_field_sections/create_contract.rb
new file mode 100644
index 00000000000..91e44c3aaec
--- /dev/null
+++ b/app/contracts/project_custom_field_sections/create_contract.rb
@@ -0,0 +1,32 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 ProjectCustomFieldSections
+ class CreateContract < BaseContract
+ end
+end
diff --git a/app/contracts/project_custom_field_sections/delete_contract.rb b/app/contracts/project_custom_field_sections/delete_contract.rb
new file mode 100644
index 00000000000..3b5e38d4930
--- /dev/null
+++ b/app/contracts/project_custom_field_sections/delete_contract.rb
@@ -0,0 +1,39 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 ProjectCustomFieldSections
+ class DeleteContract < BaseContract
+ validate :section_not_in_use
+
+ def section_not_in_use
+ if model.custom_fields.exists?
+ errors.add(:base, :in_use)
+ end
+ end
+ end
+end
diff --git a/app/contracts/project_custom_field_sections/update_contract.rb b/app/contracts/project_custom_field_sections/update_contract.rb
new file mode 100644
index 00000000000..85960355c76
--- /dev/null
+++ b/app/contracts/project_custom_field_sections/update_contract.rb
@@ -0,0 +1,32 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 ProjectCustomFieldSections
+ class UpdateContract < BaseContract
+ end
+end
diff --git a/app/controllers/admin/settings/project_custom_field_sections_controller.rb b/app/controllers/admin/settings/project_custom_field_sections_controller.rb
index 041749c9e0a..e861d0b475c 100644
--- a/app/controllers/admin/settings/project_custom_field_sections_controller.rb
+++ b/app/controllers/admin/settings/project_custom_field_sections_controller.rb
@@ -31,61 +31,74 @@ module Admin::Settings
include OpTurbo::ComponentStream
include Admin::Settings::ProjectCustomFields::ComponentStreams
- before_action :set_project_custom_field_section, only: %i[move drop]
+ before_action :set_project_custom_field_section, only: %i[update move drop destroy]
def create
- @project_custom_field_section = ProjectCustomFieldSection.new(
- name: project_custom_field_section_params[:name],
- position: 1 # show new sections at the top of the list, otherwise might not be visible to user
+ # show new sections at the top of the list, otherwise might not be visible to user
+ call = ::ProjectCustomFieldSections::CreateService.new(user: current_user).call(
+ project_custom_field_section_params.merge(position: 1)
)
- if @project_custom_field_section.save
+ if call.success?
update_header_via_turbo_stream # required to closed the dialog
update_sections_via_turbo_stream(project_custom_field_sections: ProjectCustomFieldSection.all)
else
- update_section_dialog_body_form_via_turbo_stream(project_custom_field_section: @project_custom_field_section)
+ update_section_dialog_body_form_via_turbo_stream(project_custom_field_section: call.result)
end
respond_with_turbo_streams
end
def update
- @project_custom_field_section = ProjectCustomFieldSection.find(params[:id])
+ call = ::ProjectCustomFieldSections::UpdateService.new(user: current_user, model: @project_custom_field_section).call(
+ project_custom_field_section_params
+ )
- if @project_custom_field_section.update(project_custom_field_section_params)
- update_section_via_turbo_stream(project_custom_field_section: @project_custom_field_section)
+ if call.success?
+ update_section_via_turbo_stream(project_custom_field_section: call.result)
else
- update_section_dialog_body_form_via_turbo_stream(project_custom_field_section: @project_custom_field_section)
+ update_section_dialog_body_form_via_turbo_stream(project_custom_field_section: call.result)
end
respond_with_turbo_streams
end
def destroy
- @project_custom_field_section = ProjectCustomFieldSection.find(params[:id])
+ call = ::ProjectCustomFieldSections::DeleteService.new(user: current_user, model: @project_custom_field_section).call
- if @project_custom_field_section.destroy
+ if call.success?
update_sections_via_turbo_stream(project_custom_field_sections: ProjectCustomFieldSection.all)
+ else
+ # TODO: show error message
end
respond_with_turbo_streams
end
def move
- @project_custom_field_section.move_to = params[:move_to]&.to_sym
+ call = ::ProjectCustomFieldSections::UpdateService.new(user: current_user, model: @project_custom_field_section).call(
+ move_to: params[:move_to]&.to_sym
+ )
- if @project_custom_field_section.save
+ if call.success?
update_sections_via_turbo_stream(project_custom_field_sections: ProjectCustomFieldSection.all)
+ else
+ # TODO: show error message
end
respond_with_turbo_streams
end
def drop
- @project_custom_field_section.insert_at(params[:position].to_i)
-
- update_sections_via_turbo_stream(project_custom_field_sections: ProjectCustomFieldSection.all)
+ call = ::ProjectCustomFieldSections::UpdateService.new(user: current_user, model: @project_custom_field_section).call(
+ position: params[:position].to_i
+ )
+ if call.success?
+ update_sections_via_turbo_stream(project_custom_field_sections: ProjectCustomFieldSection.all)
+ else
+ # TODO: show error message
+ end
respond_with_turbo_streams
end
diff --git a/app/services/project_custom_field_sections/create_service.rb b/app/services/project_custom_field_sections/create_service.rb
new file mode 100644
index 00000000000..01cf2b20db7
--- /dev/null
+++ b/app/services/project_custom_field_sections/create_service.rb
@@ -0,0 +1,32 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 ProjectCustomFieldSections
+ class CreateService < ::BaseServices::Create
+ end
+end
diff --git a/app/services/project_custom_field_sections/delete_service.rb b/app/services/project_custom_field_sections/delete_service.rb
new file mode 100644
index 00000000000..823c96e8c41
--- /dev/null
+++ b/app/services/project_custom_field_sections/delete_service.rb
@@ -0,0 +1,32 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 ProjectCustomFieldSections
+ class DeleteService < ::BaseServices::Delete
+ end
+end
diff --git a/app/services/project_custom_field_sections/set_attributes_service.rb b/app/services/project_custom_field_sections/set_attributes_service.rb
new file mode 100644
index 00000000000..db7da29c9cd
--- /dev/null
+++ b/app/services/project_custom_field_sections/set_attributes_service.rb
@@ -0,0 +1,32 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 ProjectCustomFieldSections
+ class SetAttributesService < ::BaseServices::SetAttributes
+ end
+end
diff --git a/app/services/project_custom_field_sections/update_service.rb b/app/services/project_custom_field_sections/update_service.rb
new file mode 100644
index 00000000000..7611c6d4cd7
--- /dev/null
+++ b/app/services/project_custom_field_sections/update_service.rb
@@ -0,0 +1,32 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 ProjectCustomFieldSections
+ class UpdateService < ::BaseServices::Update
+ end
+end