-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[58105] Added update_item and delete_branch methods to HierarchicalIt…
…emService
- Loading branch information
Andreas Pfohl
committed
Oct 11, 2024
1 parent
787a249
commit f2c75a2
Showing
6 changed files
with
276 additions
and
18 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
app/contracts/custom_fields/hierarchy/update_item_contract.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# 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 CustomFields | ||
module Hierarchy | ||
class UpdateItemContract < Dry::Validation::Contract | ||
params do | ||
required(:item).filled | ||
optional(:label).filled(:string) | ||
optional(:short).filled(:string) | ||
end | ||
|
||
rule(:item) do | ||
if value.is_a?(CustomField::Hierarchy::Item) | ||
if !value.persisted? | ||
key.failure("Item must exist") | ||
elsif value.parent.nil? | ||
key.failure("Item must not be a root item") | ||
end | ||
else | ||
key.failure("Item must be of type 'Item'") | ||
end | ||
end | ||
|
||
rule(:label) do | ||
next unless key? | ||
|
||
if CustomField::Hierarchy::Item.exists?(parent_id: values[:item].parent, label: value) | ||
key.failure("Label must be unique within the same hierarchy level") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
spec/contracts/custom_fields/hierarchy/update_item_contract_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# 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 "rails_helper" | ||
|
||
RSpec.describe CustomFields::Hierarchy::UpdateItemContract do | ||
subject { described_class.new } | ||
|
||
# rubocop:disable Rails/DeprecatedActiveModelErrorsMethods | ||
describe "#call" do | ||
let(:vader) { create(:hierarchy_item) } | ||
let(:luke) { create(:hierarchy_item, label: "luke", short: "ls", parent: vader) } | ||
let(:leia) { create(:hierarchy_item, label: "leia", short: "lo", parent: vader) } | ||
|
||
before do | ||
luke | ||
leia | ||
end | ||
|
||
context "when all required fields are valid" do | ||
it "is valid" do | ||
[ | ||
{ item: luke, label: "Luke Skywalker", short: "LS" }, | ||
{ item: luke, label: "Luke Skywalker" }, | ||
{ item: luke, short: "LS" }, | ||
{ item: luke, short: "lo" }, | ||
{ item: luke } | ||
].each { |params| expect(subject.call(params)).to be_success } | ||
end | ||
end | ||
|
||
context "when item is a root item" do | ||
let(:params) { { item: vader } } | ||
|
||
it("is invalid") do | ||
result = subject.call(params) | ||
expect(result).to be_failure | ||
expect(result.errors.to_h).to include(item: ["Item must not be a root item"]) | ||
end | ||
end | ||
|
||
context "when item is not of type 'Item'" do | ||
let(:invalid_item) { create(:custom_field) } | ||
let(:params) { { item: invalid_item } } | ||
|
||
it("is invalid") do | ||
result = subject.call(params) | ||
expect(result).to be_failure | ||
expect(result.errors.to_h).to include(item: ["Item must be of type 'Item'"]) | ||
end | ||
end | ||
|
||
context "when item is not persisted" do | ||
let(:item) { build(:hierarchy_item) } | ||
let(:params) { { item: } } | ||
|
||
it "is invalid" do | ||
result = subject.call(params) | ||
expect(result).to be_failure | ||
expect(result.errors.to_h).to include(item: ["Item must exist"]) | ||
end | ||
end | ||
|
||
context "when the label already exist in the same hierarchy level" do | ||
let(:params) { { item: luke, label: "leia" } } | ||
|
||
it "is invalid" do | ||
result = subject.call(params) | ||
expect(result).to be_failure | ||
expect(result.errors.to_h).to include(label: ["Label must be unique within the same hierarchy level"]) | ||
end | ||
end | ||
|
||
context "when fields are invalid" do | ||
it "is invalid" do | ||
[ | ||
{}, | ||
{ item: nil }, | ||
{ item: luke, label: nil }, | ||
{ item: luke, label: 42 }, | ||
{ item: luke, short: nil }, | ||
{ item: luke, short: 42 } | ||
].each { |params| expect(subject.call(params)).to be_failure } | ||
end | ||
end | ||
end | ||
# rubocop:enable Rails/DeprecatedActiveModelErrorsMethods | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters