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

creates the create contract to create types during type creation

Marcello Rochab7aebbfe0b8a
diff --git a/app/contracts/work_package_types/base_contract.rb b/app/contracts/work_package_types/base_contract.rb
new file mode 100644
index 00000000000..9b0dd662c5d
--- /dev/null
+++ b/app/contracts/work_package_types/base_contract.rb
@@ -0,0 +1,37 @@
+# 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 WorkPackageTypes
+ class BaseContract < ::ModelContract
+ include RequiresAdminGuard
+
+ def self.model = Type
+ end
+end
diff --git a/app/contracts/work_package_types/create_contract.rb b/app/contracts/work_package_types/create_contract.rb
new file mode 100644
index 00000000000..357e29c653e
--- /dev/null
+++ b/app/contracts/work_package_types/create_contract.rb
@@ -0,0 +1,42 @@
+# 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 WorkPackageTypes
+ class CreateContract < BaseContract
+ attribute :color_id
+ attribute :description
+ attribute :is_default
+ attribute :is_in_roadmap
+ attribute :is_milestone
+ attribute :name
+
+ validates :is_in_roadmap, inclusion: { in: [true, false] }
+ end
+end
diff --git a/spec/contracts/work_package_types/create_contract_spec.rb b/spec/contracts/work_package_types/create_contract_spec.rb
new file mode 100644
index 00000000000..b285eb2e1be
--- /dev/null
+++ b/spec/contracts/work_package_types/create_contract_spec.rb
@@ -0,0 +1,114 @@
+# 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.
+#++
+
+require "spec_helper"
+
+module WorkPackageTypes
+ RSpec.describe CreateContract do
+ let(:user) { create(:admin) }
+ let(:base_attributes) do
+ { name: "O-Negative", description: nil, is_milestone: true, is_default: false, is_in_roadmap: true }
+ end
+ let(:attributes) { base_attributes }
+ let(:model) { Type.new(attributes) }
+
+ subject(:contract) { described_class.new(model, user, options: {}) }
+
+ context "when the user isn't admin" do
+ let(:user) { create(:user) }
+
+ it "the contract is invalid" do
+ expect(contract.validate).to be_falsey
+ end
+
+ it "adds and error to the contract" do
+ contract.validate
+ expect(contract.errors.details).to eq(base: [{ error: :error_unauthorized }])
+ end
+ end
+
+ describe "name validations" do
+ context "when name is blank" do
+ let(:attributes) { base_attributes.merge(name: "") }
+
+ it "the contract is invalid" do
+ expect(contract.validate).to be_falsey
+ end
+
+ it "adds and error to the contract" do
+ contract.validate
+ expect(contract.errors.details[:name]).to eq([{ error: :blank }])
+ end
+ end
+
+ context "when name is not unique (case insensitive)" do
+ before { create(:type, name: "o-negative") }
+
+ it "the contract is invalid" do
+ expect(contract.validate).to be_falsey
+ end
+
+ it "adds and error to the contract" do
+ contract.validate
+ expect(contract.errors.details[:name]).to eq([{ error: :taken, value: attributes[:name] }])
+ end
+ end
+
+ context "when name is too long" do
+ let(:attributes) { base_attributes.merge(name: "A" * 300) }
+
+ it "the contract is invalid" do
+ expect(contract.validate).to be_falsey
+ end
+
+ it "adds and error to the contract" do
+ contract.validate
+ expect(contract.errors.details[:name]).to eq([{ count: 255, error: :too_long }])
+ end
+ end
+
+ context "when is_in_milestone or is_default aren't booleans" do
+ let(:attributes) { base_attributes.merge(is_default: nil, is_milestone: nil, is_in_roadmap: nil) }
+
+ it "the contract is invalid" do
+ expect(contract.validate).to be_falsey
+ end
+
+ it "adds and error to the contract" do
+ contract.validate
+
+ expect(contract.errors.details[:is_default]).to eq([{ error: :inclusion, value: nil }])
+ expect(contract.errors.details[:is_milestone]).to eq([{ error: :inclusion, value: nil }])
+ expect(contract.errors.details[:is_in_roadmap]).to eq([{ error: :inclusion, value: nil }])
+ end
+ end
+ end
+ end
+end