Skip to content

Instantly share code, notes, and snippets.

@mattantonelli
Created October 20, 2014 22:48
Show Gist options
  • Save mattantonelli/1339fc76803e06672f71 to your computer and use it in GitHub Desktop.
Save mattantonelli/1339fc76803e06672f71 to your computer and use it in GitHub Desktop.
Generate a link to a related model with text indicating the range from the objects' names and a total count of the objects. Will generate multiple ranges as necessary, intended for numeric names.
def range_and_count_link(network_elements, path, with_link, unique)
ranges = generate_name_ranges(network_elements)
count = unique ? network_elements.uniq.size : network_elements.size
count_str = with_link && count > 0 ? link_to(count, path) : count.to_s
"#{ranges} (#{count_str})".html_safe
end
def range_and_count(network_element, element_to_count, with_link = true, unique = false)
poly_route = with_link ? [network_element, element_to_count] : network_element
range_and_count_link(network_element.send(element_to_count), polymorphic_url(poly_route), with_link, unique)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment