From 62cdfd35dee3b680744c55d1066124dff2f12ad2 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Tue, 9 Aug 2011 15:34:53 -0700 Subject: [PATCH] [#6441] Refactor: only call validation on updates --- app/models/deliverable.rb | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/app/models/deliverable.rb b/app/models/deliverable.rb index 867ef7d..ae47687 100644 --- a/app/models/deliverable.rb +++ b/app/models/deliverable.rb @@ -20,7 +20,7 @@ class Deliverable < ActiveRecord::Base validates_presence_of :type validates_presence_of :manager validates_inclusion_of :status, :in => ["open","locked","closed"], :allow_blank => true, :allow_nil => true - validate :validate_update_allowed + validate_on_update :validate_status_changes # Accessors include DollarizedAttribute @@ -80,18 +80,16 @@ class Deliverable < ActiveRecord::Base (new_record? || open?) end - def validate_update_allowed - unless new_record? - if changes.keys == ["status"] - noop("Allow changes to the status only") - elsif changes["status"].present? && changes["status"].second == "open" - noop("Allow any changes when going to 'open'") - elsif changes["status"].present? && changes["status"].first == "open" - noop("Allow any changes when going from 'open' to another status") - else - errors.add_to_base(:cant_update_locked_deliverable) if locked? - errors.add_to_base(:cant_update_closed_deliverable) if closed? - end + def validate_status_changes + if changes.keys == ["status"] + noop("Allow changes to the status only") + elsif changes["status"].present? && changes["status"].second == "open" + noop("Allow any changes when going to 'open'") + elsif changes["status"].present? && changes["status"].first == "open" + noop("Allow any changes when going from 'open' to another status") + else + errors.add_to_base(:cant_update_locked_deliverable) if locked? + errors.add_to_base(:cant_update_closed_deliverable) if closed? end end