Skip to content

Instantly share code, notes, and snippets.

@rjz
Created April 29, 2012 23:01
Show Gist options
  • Save rjz/2553749 to your computer and use it in GitHub Desktop.
Save rjz/2553749 to your computer and use it in GitHub Desktop.
Rails helper for pretty-printing ordered parameters
# 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