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

projects queries controller update action with permissive contract

Ivan Kuchin1c9c2efaeb10
diff --git a/app/contracts/queries/projects/project_queries/update_contract.rb b/app/contracts/queries/projects/project_queries/update_contract.rb
new file mode 100644
index 00000000000..82e6479a947
--- /dev/null
+++ b/app/contracts/queries/projects/project_queries/update_contract.rb
@@ -0,0 +1,31 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-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 Queries::Projects::ProjectQueries
+ class UpdateContract < BaseContract; end
+end
diff --git a/app/controllers/projects/queries_controller.rb b/app/controllers/projects/queries_controller.rb
index b1e2d162eb7..ee7173a73af 100644
--- a/app/controllers/projects/queries_controller.rb
+++ b/app/controllers/projects/queries_controller.rb
@@ -31,7 +31,7 @@ class Projects::QueriesController < ApplicationController
# No need for a more specific authorization check. That is carried out in the contracts.
before_action :require_login
- before_action :find_query, only: :destroy
+ before_action :find_query, only: %i[update destroy]
before_action :load_query_or_deny_access, only: %i[new]
current_menu_item [:new, :create] do
@@ -58,6 +58,20 @@ class Projects::QueriesController < ApplicationController
end
end
+ def update
+ call = Queries::Projects::ProjectQueries::UpdateService
+ .new(user: current_user, model: @query)
+ .call(permitted_query_params)
+
+ if call.success?
+ redirect_to projects_path(query_id: call.result.id)
+ else
+ render template: "/projects/index",
+ layout: "global",
+ locals: { query: call.result, state: :edit }
+ end
+ end
+
def destroy
Queries::Projects::ProjectQueries::DeleteService.new(user: current_user, model: @query)
.call
diff --git a/app/services/queries/projects/project_queries/update_service.rb b/app/services/queries/projects/project_queries/update_service.rb
new file mode 100644
index 00000000000..15dd89fcef2
--- /dev/null
+++ b/app/services/queries/projects/project_queries/update_service.rb
@@ -0,0 +1,29 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-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.
+# ++
+
+class Queries::Projects::ProjectQueries::UpdateService < BaseServices::Update; end
diff --git a/spec/contracts/queries/projects/project_queries/update_contract_spec.rb b/spec/contracts/queries/projects/project_queries/update_contract_spec.rb
new file mode 100644
index 00000000000..384e76874c9
--- /dev/null
+++ b/spec/contracts/queries/projects/project_queries/update_contract_spec.rb
@@ -0,0 +1,48 @@
+#-- 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.
+#++
+
+require "spec_helper"
+require_relative "shared_contract_examples"
+
+RSpec.describe Queries::Projects::ProjectQueries::UpdateContract do
+ it_behaves_like "project queries contract" do
+ let(:query) do
+ Queries::Projects::ProjectQuery.new(name: query_name).tap do |query|
+ query.extend(OpenProject::ChangedBySystem)
+
+ query.change_by_system do
+ query.user = query_user
+ end
+
+ query.select(*query_selects)
+ end
+ end
+
+ let(:contract) { described_class.new(query, current_user) }
+ end
+end
diff --git a/spec/services/queries/projects/project_queries/update_service_spec.rb b/spec/services/queries/projects/project_queries/update_service_spec.rb
new file mode 100644
index 00000000000..f040f746e98
--- /dev/null
+++ b/spec/services/queries/projects/project_queries/update_service_spec.rb
@@ -0,0 +1,36 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-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.
+# ++
+
+require "spec_helper"
+require "services/base_services/behaves_like_update_service"
+
+RSpec.describe Queries::Projects::ProjectQueries::UpdateService, type: :model do
+ it_behaves_like "BaseServices update service" do
+ let(:factory) { :project_query }
+ end
+end