Skip to content

Instantly share code, notes, and snippets.

@xarimanx
Created July 4, 2013 14:48
Show Gist options
  • Save xarimanx/5928414 to your computer and use it in GitHub Desktop.
Save xarimanx/5928414 to your computer and use it in GitHub Desktop.
generate options with custom data attrs
def options_from_collection_for_select_with_data(collection, value_method, text_method, selected = nil, data = {})
options = collection.map do |element|
[element.send(text_method), element.send(value_method), data.map do |k, v|
{"data-#{k}" => element.send(v)}
end
].flatten
end
selected, disabled = extract_selected_and_disabled(selected)
select_deselect = {}
select_deselect[:selected] = extract_values_from_collection(collection, value_method, selected)
select_deselect[:disabled] = extract_values_from_collection(collection, value_method, disabled)
options_for_select(options, select_deselect)
end
def options_from_collection_for_select_with_custom(collection, value_method, text_method, data_name, custom_method, option_method, selected = nil)
options = collection.map do |element|
[element.send(text_method), element.send(value_method), {"data-#{data_name}" => send(custom_method, element.send(option_method))}]
end
selected, disabled = extract_selected_and_disabled(selected)
select_deselect = {}
select_deselect[:selected] = extract_values_from_collection(collection, value_method, selected)
select_deselect[:disabled] = extract_values_from_collection(collection, value_method, disabled)
options_for_select(options, select_deselect)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment