Created
July 27, 2016 14:23
-
-
Save Koroeskohr/d2cc0861d4fc673bb6ac0ef820dd790a to your computer and use it in GitHub Desktop.
Object monkey patch for Rails view syntactic sugar
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
class Object | |
def or_if_blank(object = nil, &block) | |
if blank? | |
return yield if block_given? | |
object | |
else | |
self | |
end | |
end | |
end | |
user = User.first | |
user.address | |
# => "Chez ta reum" | |
user.address.or_if_blank("C'est vide :(") | |
# => "Chez ta reum" | |
user.address = "" | |
# => "" | |
user.address.or_if_blank("C'est vide :(") | |
# => "C'est vide :(" | |
user.address.or_if_blank { link_to "C'est vide, gtfo", root_path } | |
# => "<a(...)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment