-
-
Save andrzejkrzywda/1082203 to your computer and use it in GitHub Desktop.
Which version do you prefere?
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
require 'test/unit' | |
class KeyTest < Test::Unit::TestCase | |
def test_key | |
object = Object.new | |
assert_equal(:submit, object_key(object)) | |
def object.persisted?; true end | |
assert_equal(:update, object_key(object)) | |
def object.persisted?; false end | |
assert_equal(:create, object_key(object)) | |
end | |
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 object_key | |
if @object && @object.respond_to?(:persisted?) | |
@object.persisted? ? :update : :create | |
else | |
:submit | |
end | |
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 object_key | |
return :submit unless @object && @object.respond_to?(:persisted?) | |
return @object.persisted? ? :update : :create | |
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 object_key | |
# assume that nil does not respond to :persisted? and if it does, then respect that | |
return :submit unless @object.respond_to?(:persisted?) | |
return @object.persisted? ? :update : :create | |
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 object_key | |
if @object && @object.respond_to?(:persisted?) && @object.persisted? | |
:update | |
elsif @object && @object.respond_to?(:persisted?) | |
:create | |
else | |
:submit | |
end | |
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 object_key | |
return :submit if ! persistable?(object) | |
return :update if ! @object.persisted? | |
return :create if @object.persisted? | |
end | |
def persistable?(object) | |
object && object.respond_to?(:persisted?) | |
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 object_key(object) | |
object.persisted? ? :update : :create | |
rescue; :submit | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment