From 9f40a7184b9558814ce7a344a52e020b9345b69c Mon Sep 17 00:00:00 2001 From: Hagen Schink Date: Fri, 3 Jan 2014 08:34:06 +0100 Subject: [PATCH] Adds status transition validation --- app/models/type.rb | 12 ++++++++++++ app/models/work_package/validations.rb | 20 ++++++++++++++++++++ config/locales/de.yml | 2 ++ config/locales/en.yml | 2 ++ 4 files changed, 36 insertions(+) diff --git a/app/models/type.rb b/app/models/type.rb index 183e429ec4..03198b45d3 100644 --- a/app/models/type.rb +++ b/app/models/type.rb @@ -102,8 +102,20 @@ class Type < ActiveRecord::Base PlanningElementTypeColor.all end + def is_valid_transition?(status_id_a, status_id_b, roles) + transition_exists?(status_id_a, status_id_b, roles.collect(&:id)) + end + private + def check_integrity raise "Can't delete type" if WorkPackage.find(:first, :conditions => ["type_id=?", self.id]) end + + def transition_exists?(status_id_a, status_id_b, role_ids) + !workflows.where(old_status_id: status_id_a, + new_status_id: status_id_b, + role_id: role_ids) + .empty? + end end diff --git a/app/models/work_package/validations.rb b/app/models/work_package/validations.rb index 86474b1c83..7540f14b5a 100644 --- a/app/models/work_package/validations.rb +++ b/app/models/work_package/validations.rb @@ -46,6 +46,8 @@ module WorkPackage::Validations validate :validate_milestone_constraint validate :validate_parent_constraint + + validate :validate_status_transition end def validate_start_date_before_soonest_start_date @@ -84,4 +86,22 @@ module WorkPackage::Validations errors.add :parent_id, :cannot_be_milestone if parent.is_milestone? end end + + def validate_status_transition + if status_changed? && !status_transition_exists? + errors.add :status_id, :status_transition_invalid + end + end + + private + + def status_changed? + self.status_id_was != 0 && self.status_id_changed? + end + + def status_transition_exists? + roles = User.current.roles_for_project(self.project) + + self.type.is_valid_transition?(self.status_id_was, self.status_id, roles) + end end diff --git a/config/locales/de.yml b/config/locales/de.yml index 0d78f9ea08..e567622dd5 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -219,6 +219,8 @@ de: cannot_be_in_another_project: "darf nicht in einem anderen Projekt sein" not_a_valid_parent: "ist ungültig" does_not_exist: "existiert nicht" + status_id: + status_transition_invalid: "es existiert kein valider Übergang vom alten zum neuen Status" project_association: identical_projects: "kann nicht von einem Projekt auf sich selbst erstellt werden" project_association_not_allowed: "erlaubt keine Projekt-Abhängigkeiten" diff --git a/config/locales/en.yml b/config/locales/en.yml index 6f98db0843..5781d12fb4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -220,6 +220,8 @@ en: cannot_be_in_another_project: "cannot be in another project" not_a_valid_parent: "is invaild" does_not_exist: "does not exist" + status_id: + status_transition_invalid: "no valid transition exists from old to new status" user: attributes: password: