diff --git a/Gemfile b/Gemfile index b56eff29d3..c7155eee2e 100644 --- a/Gemfile +++ b/Gemfile @@ -56,7 +56,7 @@ gem 'friendly_id', '~> 5.2.1' gem 'acts_as_list', '~> 0.9.9' gem 'acts_as_tree', '~> 2.7.0' gem 'awesome_nested_set', '~> 3.1.3' -gem 'typed_dag', '~> 2.0.0' +gem 'typed_dag', '~> 2.0.1' gem 'color-tools', '~> 1.3.0', require: 'color' diff --git a/Gemfile.lock b/Gemfile.lock index 9cbffdb3f8..cdefad2dab 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -568,7 +568,7 @@ GEM tilt (2.0.7) timecop (0.9.1) ttfunk (1.5.0) - typed_dag (2.0.0) + typed_dag (2.0.1) rails (>= 5.0.4) tzinfo (1.2.3) thread_safe (~> 0.1) @@ -719,7 +719,7 @@ DEPENDENCIES thin (~> 1.7.2) timecop (~> 0.9.0) transactional_lock! - typed_dag (~> 2.0.0) + typed_dag (~> 2.0.1) tzinfo-data (~> 1.2017.2) unicorn unicorn-worker-killer diff --git a/app/contracts/relations/update_contract.rb b/app/contracts/relations/update_contract.rb index 1ce941fa6e..d7b4a2c7be 100644 --- a/app/contracts/relations/update_contract.rb +++ b/app/contracts/relations/update_contract.rb @@ -32,17 +32,29 @@ require 'relations/base_contract' module Relations class UpdateContract < BaseContract - def validate - links_immutable + validate :from_immutable + validate :to_immutable - super + private + + def from_immutable + errors.add :from, :error_readonly if from_id_changed_and_not_swapped? end - private + def to_immutable + errors.add :to, :error_readonly if to_id_changed_and_not_swapped? + end + + def from_id_changed_and_not_swapped? + model.from_id_changed? && !from_and_to_swapped? + end + + def to_id_changed_and_not_swapped? + model.to_id_changed? && !from_and_to_swapped? + end - def links_immutable - errors.add :from, :error_readonly if model.from_id_changed? - errors.add :to, :error_readonly if model.to_id_changed? + def from_and_to_swapped? + model.to_id == model.from_id_was && model.from_id == model.to_id_was end end end diff --git a/spec/features/work_packages/details/details_relations_spec.rb b/spec/features/work_packages/details/details_relations_spec.rb index 535477b795..1c86cfca21 100644 --- a/spec/features/work_packages/details/details_relations_spec.rb +++ b/spec/features/work_packages/details/details_relations_spec.rb @@ -137,17 +137,31 @@ describe 'Work package relations tab', js: true, selenium: true do find(toggle_btn_selector).click expect(page).to have_selector(toggle_btn_selector, text: 'Group by relation type', wait: 10) + # Expect current to be follows and other one related expect(page).to have_selector('.relation-row--type', text: 'Follows') expect(page).to have_selector('.relation-row--type', text: 'Related To') - # Expect current to be follows, then edit to blocks + # edit to blocks relations.edit_relation_type(to_1, to_type: 'Blocks') + # the other one should not be altered expect(page).to have_selector('.relation-row--type', text: 'Blocks') expect(page).to have_selector('.relation-row--type', text: 'Related To') updated_relation = Relation.find(relation_1.id) expect(updated_relation.relation_type).to eq('blocks') + expect(updated_relation.from_id).to eq(work_package.id) + expect(updated_relation.to_id).to eq(to_1.id) + + relations.edit_relation_type(to_1, to_type: 'Blocked by') + + expect(page).to have_selector('.relation-row--type', text: 'Blocked by') + expect(page).to have_selector('.relation-row--type', text: 'Related To') + + updated_relation = Relation.find(relation_1.id) + expect(updated_relation.relation_type).to eq('blocks') + expect(updated_relation.from_id).to eq(to_1.id) + expect(updated_relation.to_id).to eq(work_package.id) end end