[#6441] Refactor: only call validation on updates
This commit is contained in:
+11
-13
@@ -20,7 +20,7 @@ class Deliverable < ActiveRecord::Base
|
|||||||
validates_presence_of :type
|
validates_presence_of :type
|
||||||
validates_presence_of :manager
|
validates_presence_of :manager
|
||||||
validates_inclusion_of :status, :in => ["open","locked","closed"], :allow_blank => true, :allow_nil => true
|
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
|
# Accessors
|
||||||
include DollarizedAttribute
|
include DollarizedAttribute
|
||||||
@@ -80,18 +80,16 @@ class Deliverable < ActiveRecord::Base
|
|||||||
(new_record? || open?)
|
(new_record? || open?)
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_update_allowed
|
def validate_status_changes
|
||||||
unless new_record?
|
if changes.keys == ["status"]
|
||||||
if changes.keys == ["status"]
|
noop("Allow changes to the status only")
|
||||||
noop("Allow changes to the status only")
|
elsif changes["status"].present? && changes["status"].second == "open"
|
||||||
elsif changes["status"].present? && changes["status"].second == "open"
|
noop("Allow any changes when going to 'open'")
|
||||||
noop("Allow any changes when going to 'open'")
|
elsif changes["status"].present? && changes["status"].first == "open"
|
||||||
elsif changes["status"].present? && changes["status"].first == "open"
|
noop("Allow any changes when going from 'open' to another status")
|
||||||
noop("Allow any changes when going from 'open' to another status")
|
else
|
||||||
else
|
errors.add_to_base(:cant_update_locked_deliverable) if locked?
|
||||||
errors.add_to_base(:cant_update_locked_deliverable) if locked?
|
errors.add_to_base(:cant_update_closed_deliverable) if closed?
|
||||||
errors.add_to_base(:cant_update_closed_deliverable) if closed?
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user