Created
April 29, 2012 23:01
-
-
Save rjz/2553749 to your computer and use it in GitHub Desktop.
Rails helper for pretty-printing ordered parameters
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
# Compile a list of options into an ordered array | |
def compile_options(obj, order = []) | |
components = [] | |
order = obj.keys if order.empty? | |
order.each { |k| components << obj[k] unless obj[k].blank? } | |
components | |
end | |
# Example: returns "Omaha", "Omaha, Nebraska", or "Nebraska" | |
def pretty_location(opts = {}) | |
opts = { | |
:city => '', | |
:state => '' | |
}.update(opts) | |
components = compile_options(opts, [:city, :state]) | |
return components.join(', ') unless components.empty? | |
'The Internet' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment