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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
add contract/service for query creation
ulferts1d9caaef875c
diff --git a/app/contracts/queries/base_contract.rb b/app/contracts/queries/base_contract.rbindex d8beb65a253..49418dd3187 100644--- a/app/contracts/queries/base_contract.rb+++ b/app/contracts/queries/base_contract.rb@@ -26,8 +26,6 @@# See COPYRIGHT and LICENSE files for more details.#++-require 'model_contract'-module Queriesclass BaseContract < ::ModelContractattribute :namediff --git a/app/contracts/queries/create_contract.rb b/app/contracts/queries/create_contract.rbindex 91e46b8791c..32218baf5c7 100644--- a/app/contracts/queries/create_contract.rb+++ b/app/contracts/queries/create_contract.rb@@ -26,8 +26,6 @@# See COPYRIGHT and LICENSE files for more details.#++-require 'queries/base_contract'-module Queriesclass CreateContract < BaseContractenddiff --git a/app/contracts/queries/projects/project_queries/base_contract.rb b/app/contracts/queries/projects/project_queries/base_contract.rbnew file mode 100644index 00000000000..1d007b22dc6--- /dev/null+++ b/app/contracts/queries/projects/project_queries/base_contract.rb@@ -0,0 +1,52 @@+# -- 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 BaseContract < ::ModelContract+ attribute :name+ attribute :columns+ attribute :filters+ attribute :orders++ def self.model+ Queries::Projects::ProjectQuery+ end++ validates :name,+ presence: true,+ length: { maximum: 255 }++ validate :user_is_current_user_and_logged_in++ def user_is_current_user_and_logged_in+ unless user.logged? && user == model.user+ errors.add :base, :error_unauthorized+ end+ end+ end+enddiff --git a/app/contracts/queries/projects/project_queries/create_contract.rb b/app/contracts/queries/projects/project_queries/create_contract.rbnew file mode 100644index 00000000000..f4b66999b43--- /dev/null+++ b/app/contracts/queries/projects/project_queries/create_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 CreateContract < BaseContract; end+enddiff --git a/app/controllers/projects/menus_controller.rb b/app/controllers/projects/menus_controller.rbindex 3d2f11c984e..e241481618c 100644--- a/app/controllers/projects/menus_controller.rb+++ b/app/controllers/projects/menus_controller.rb@@ -48,14 +48,14 @@ module Projectsdef static_filters[- query_menu_item(Queries::Projects::DefaultFactory.static_query_all, selected: no_query_props?),- query_menu_item(Queries::Projects::DefaultFactory.static_query_my, id: 'my'),- query_menu_item(Queries::Projects::DefaultFactory.static_query_archived, id: 'archived')+ query_menu_item(::Queries::Projects::Factory.static_query_all, selected: no_query_props?),+ query_menu_item(::Queries::Projects::Factory.static_query_my, id: 'my'),+ query_menu_item(::Queries::Projects::Factory.static_query_archived, id: 'archived')]enddef my_filters- Queries::Projects::ProjectQuery+ ::Queries::Projects::ProjectQuery.where(user: current_user).order(:name).map do |query|diff --git a/app/controllers/projects/queries_controller.rb b/app/controllers/projects/queries_controller.rbindex d29f7f7e201..c99e433d5e6 100644--- a/app/controllers/projects/queries_controller.rb+++ b/app/controllers/projects/queries_controller.rb@@ -27,16 +27,23 @@# ++class Projects::QueriesController < ApplicationController+ # No need for a more specific authorization check. That is carried out in the contracts.+ before_action :require_loginbefore_action :find_query, only: :destroy- # TODO authorizationdef create- query = load_query- query.name = params[:name]+ call = Queries::Projects::ProjectQueries::CreateService.new(user: current_user)+ .call(load_query.attributes.merge(name: params[:name]))- query.save+ if call.success?+ redirect_to projects_path(query_id: call.result.id)+ else+ # This is a workaround as long as a modal is used.+ # In the final implementation, errors will be displayed under the text box that replaces the header.+ flash[:error] = call.errors.full_messages- redirect_to projects_path(query_id: query.id)+ redirect_to projects_path+ endenddef destroy@@ -49,7 +56,7 @@ class Projects::QueriesController < ApplicationController# TODO: move to module to share with projects_controllerdef load_query- @query = ParamsToQueryService.new(Project, current_user).call(params)+ ParamsToQueryService.new(Project, current_user).call(params)enddef find_querydiff --git a/app/models/queries/projects/default_factory.rb b/app/models/queries/projects/factory.rbsimilarity index 98%rename from app/models/queries/projects/default_factory.rbrename to app/models/queries/projects/factory.rbindex c954557c019..94df0db7269 100644--- a/app/models/queries/projects/default_factory.rb+++ b/app/models/queries/projects/factory.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.# ++-class Queries::Projects::DefaultFactory+class Queries::Projects::Factoryclass << selfdef find(id)static_query(id) || Queries::Projects::ProjectQuery.find(id)diff --git a/app/services/params_to_query_service.rb b/app/services/params_to_query_service.rbindex 899a832b3f5..91ad6a538b8 100644--- a/app/services/params_to_query_service.rb+++ b/app/services/params_to_query_service.rb@@ -51,8 +51,8 @@ class ParamsToQueryServicedef find_query(query_id)model_name = model.name- if "::Queries::#{model_name.pluralize}".constantize.const_defined?(:DefaultFactory) && query_id- factory = "::Queries::#{model_name.pluralize}::DefaultFactory".constantize+ if "::Queries::#{model_name.pluralize}".constantize.const_defined?(:Factory)+ factory = "::Queries::#{model_name.pluralize}::Factory".constantizefactory.find(query_id)elsif query_class.respond_to?(:find) && query_iddiff --git a/app/services/queries/projects/project_queries/create_service.rb b/app/services/queries/projects/project_queries/create_service.rbnew file mode 100644index 00000000000..d346bda2fa4--- /dev/null+++ b/app/services/queries/projects/project_queries/create_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::CreateService < BaseServices::Create; enddiff --git a/app/services/queries/projects/project_queries/set_attributes_service.rb b/app/services/queries/projects/project_queries/set_attributes_service.rbnew file mode 100644index 00000000000..b9f675a4766--- /dev/null+++ b/app/services/queries/projects/project_queries/set_attributes_service.rb@@ -0,0 +1,39 @@+# -- 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::SetAttributesService < BaseServices::SetAttributes+ def set_default_attributes(_params)+ set_default_user+ end++ def set_default_user+ model.change_by_system do+ model.user = user+ end+ end+enddiff --git a/spec/contracts/queries/projects/project_queries/create_contract_spec.rb b/spec/contracts/queries/projects/project_queries/create_contract_spec.rbnew file mode 100644index 00000000000..4a991387132--- /dev/null+++ b/spec/contracts/queries/projects/project_queries/create_contract_spec.rb@@ -0,0 +1,46 @@+#-- 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::CreateContract 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+ end+ end++ let(:contract) { described_class.new(query, current_user) }+ end+enddiff --git a/spec/contracts/queries/projects/project_queries/shared_contract_examples.rb b/spec/contracts/queries/projects/project_queries/shared_contract_examples.rbnew file mode 100644index 00000000000..562dc8bc91f--- /dev/null+++ b/spec/contracts/queries/projects/project_queries/shared_contract_examples.rb@@ -0,0 +1,66 @@+#-- 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 'contracts/shared/model_contract_shared_context'++RSpec.shared_examples_for 'project queries contract' do+ include_context 'ModelContract shared context'++ let(:current_user) { build_stubbed(:user) }+ let(:query_name) { "Query name" }+ let(:query_user) { current_user }++ describe 'validation' do+ it_behaves_like 'contract is valid'++ context 'if the name is nil' do+ let(:query_name) { nil }++ it_behaves_like 'contract is invalid', name: :blank+ end++ context 'if the name is too long' do+ let(:query_name) { 'A' * 256 }++ it_behaves_like 'contract is invalid', name: :too_long+ end++ context 'if the user is not the current user' do+ let(:query_user) { build_stubbed(:user) }++ it_behaves_like 'contract is invalid', base: :error_unauthorized+ end++ context 'if the user and the current user is anonymous' do+ let(:current_user) { build_stubbed(:anonymous) }++ it_behaves_like 'contract is invalid', base: :error_unauthorized+ end+ end+enddiff --git a/spec/services/queries/projects/project_queries/create_service_spec.rb b/spec/services/queries/projects/project_queries/create_service_spec.rbnew file mode 100644index 00000000000..67718f40075--- /dev/null+++ b/spec/services/queries/projects/project_queries/create_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_create_service'++RSpec.describe Queries::Projects::ProjectQueries::CreateService, type: :model do+ it_behaves_like 'BaseServices create service' do+ let(:factory) { :project_query }+ end+enddiff --git a/spec/services/queries/projects/project_queries/set_attributes_service_spec.rb b/spec/services/queries/projects/project_queries/set_attributes_service_spec.rbnew file mode 100644index 00000000000..226465c502a--- /dev/null+++ b/spec/services/queries/projects/project_queries/set_attributes_service_spec.rb@@ -0,0 +1,107 @@+# -- 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'++RSpec.describe Queries::Projects::ProjectQueries::SetAttributesService, type: :model do+ let(:current_user) { build_stubbed(:user) }++ let(:contract_instance) do+ contract = instance_double(Queries::Projects::ProjectQueries::CreateContract)+ allow(contract)+ .to receive_messages(validate: contract_valid, errors: contract_errors)+ contract+ end++ let(:contract_errors) { instance_double(ActiveModel::Errors) }+ let(:contract_valid) { true }+ let(:model_valid) { true }++ let(:instance) do+ described_class.new(user: current_user,+ model: model_instance,+ contract_class:,+ contract_options: {})+ end+ let(:model_instance) { Queries::Projects::ProjectQuery.new }+ let(:contract_class) do+ allow(Queries::Projects::ProjectQueries::CreateContract)+ .to receive(:new)+ .and_return(contract_instance)++ Queries::Projects::ProjectQueries::CreateContract+ end++ let(:params) { {} }++ before do+ allow(model_instance)+ .to receive(:valid?)+ .and_return(model_valid)+ end++ subject { instance.call(params) }++ it 'returns the instance as the result' do+ expect(subject.result)+ .to eql model_instance+ end++ it 'is a success' do+ expect(subject)+ .to be_success+ end++ context 'with params' do+ let(:params) do+ {+ name: 'Foobar'+ }+ end++ it 'assigns the params' do+ subject++ expect(model_instance.name).to eq 'Foobar'+ end+ end++ context 'with an invalid contract' do+ let(:contract_valid) { false }++ it 'returns failure' do+ expect(subject)+ .not_to be_success+ end++ it "returns the contract's errors" do+ expect(subject.errors)+ .to eql(contract_errors)+ end+ end+end