#716 √ resolved
Clemens Kofler

NumberHelper i18n compatibility

Reported by Clemens Kofler | July 28th, 2008 @ 10:13 PM | in 2.x

I've taken the NumberHelper for another ride to make it fully i18n compatible (with exception of number_to_phone).

It's a really massive patch so here's a rundown of the changes:

  • A small but important change to the current definition in the en-US locale: I've changed the namespacing so that all NumberHelper related stuff sits in the :number namespace and the :number namespace defines special options of each method as its child elements. I think this makes sense from an OO perspective: :'number.format' defines default options which can be overridden or extended by it's children (e.g. :currency changes the default :precision from 3 to 2 and adds :unit and :format as new specific options).
  • The changes forced me to change a few bits of the i18n number_to_currency mocha testing. Nothing big, since it's only implementation testing.
  • Pretty much all methods now internally rely on number_with_precision which, in turn, relies on number_with_delimiter. This makes all methods compatible with the :separator, :delimiter and :precision options.
  • number_to_currency had some weird behavior: When called like number_to_currency("x") one of the tests enforced it to return "$x.". All other methods return invalid parameters untouched. I've changed it so that it returns "$x" (without the dot). It makes a little more sense (still not much) and it cuts down the code in the method. I've commented out the old test and added a new one.
  • Similar for number_to_human_size: This was the only method to return nil for invalid parameters. In this case, I've only commented out the failing test. The new behavior is that nil returns nil and everything else that evaluates to 0 using to_i returns "0 Bytes".
  • The ugly unDRY case-when in number_to_human_size is gone and replaced by some easy mathematical logarithm calculation. I've also moved this part into its own private method.
  • Added tests for new options and behaviors.
  • General cleanup and consolidation: symbolize_keys is now used throughout. Some Regexps were unnecessary due to the new structure.
  • Updated and consolidated docs.
  • Added deprecation notices for API changes in this ticket. This also forced me to update the old tests in order to avoid deprecation warnings. But AFAIK this is standard practice anyway.

I hope this is all clear and makes sense. If not, feel free to task.

I think this patch is really valuable regarding Rails' late i18n efforts and Helper API consolidation.

Apart from the tests I mentioned, all existing tests pass.

PS: Due to my micro commits, the patch looks bigger than it is.

Comments and changes to this ticket

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins

People watching this ticket