-
-
Save edavis10/1135390 to your computer and use it in GitHub Desktop.
Refactoring
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
def validate_status_changes | |
return if changes.keys == ["status"] | |
return if changes["status"].present? && changes["status"].second == "open" | |
return if changes["status"].present? && changes["status"].first == "open" | |
errors.add_to_base(:cant_update_locked_deliverable) if locked? | |
errors.add_to_base(:cant_update_closed_deliverable) if closed? | |
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
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 |
That got me unblocked, here is what I came up with (tests all green)
def valid_status_change?
change_to_status_only? || changing_to_the_open_status? || changing_from_the_open_status?
end
def change_to_status_only?
["status"] == changes.keys
end
def changing_to_the_open_status?
changes["status"].present? && "open" == changes["status"].second
end
def changing_from_the_open_status?
changes["status"].present? && "open" == changes["status"].first
end
def validate_status_changes
return if valid_status_change?
errors.add_to_base(:cant_update_locked_deliverable) if locked?
errors.add_to_base(:cant_update_closed_deliverable) if closed?
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can't get that version to work. changes is an instance method that isn't available.
undefined local variable or method `changes' for #Class:0xb5cfa6b0 (NameError)