Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rubiety/154971 to your computer and use it in GitHub Desktop.
Save rubiety/154971 to your computer and use it in GitHub Desktop.
module ApplicationHelper
def tree_options_from_collection_for_select(objects, value = :id, name = :name, selected = nil, options = {})
options[:indent_with] ||= '- '
options[:level] ||= 0
objects.inject([]) do |entries, object|
entries << [options[:indent_with] * options[:level] + object.send(name), object.send(value)]
entries += tree_options_from_collection_for_select(object.children, value, name, selected, options.merge(:level => options[:level] + 1))
entries
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment