Skip to content

Instantly share code, notes, and snippets.

@lachlanhardy
Created September 26, 2010 09:06
Show Gist options
  • Save lachlanhardy/597754 to your computer and use it in GitHub Desktop.
Save lachlanhardy/597754 to your computer and use it in GitHub Desktop.
# to explain
# ActionView lets me write
# pluralize(something.length, "Thing") =>
# "2 Things"
#
# But in this case, I want to separate the number from the text in HTML (to put an icon in between).
# Since I'm using CSS sprites, the easiest way is to wrap the text in a span with a class on it to get the correct image
# Thus:
def new_pluralize (number, text)
class_name = text.downcase
unless number == 1
text = text.pluralize
end
"#{number} <span class=\"#{class_name}\">#{text}</span>"
end
# Which lets me call
# new_pluralize(something.length, "Thing") =>
# "2 <span class="thing">Things</span>"
# Then all I have to do is insert it in my view without escaping the HTML
@stephan-buckmaster
Copy link

One possibility is to use

number.to_s + content_tag(:span, text, :class => class_name)

But I think it might be better to use some mapping; I don't know how many classes you are expecting. Are they all really going to get different markup? It looks a bit redundant to have the text both in the class, and the content of the span.

@lachlanhardy
Copy link
Author

Redundant how? One is content, one is data hook...

Basically, I'm using CSS to provide an icon for each of these cases. According to the design, the icon needs to go between the number and the text (and frankly that makes heaps more sense)

@stephan-buckmaster
Copy link

Redundant in the sense of repetition :-)

Anyway, looks like things are working fine for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment