Skip to content

Instantly share code, notes, and snippets.

@Koroeskohr
Created July 27, 2016 14:23
Show Gist options
  • Save Koroeskohr/d2cc0861d4fc673bb6ac0ef820dd790a to your computer and use it in GitHub Desktop.
Save Koroeskohr/d2cc0861d4fc673bb6ac0ef820dd790a to your computer and use it in GitHub Desktop.
Object monkey patch for Rails view syntactic sugar
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