This project is archived and is in readonly mode.

#716 ✓resolved
Clemens Kofler

NumberHelper i18n compatibility

Reported by Clemens Kofler | July 29th, 2008 @ 12:16 AM | 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

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

Referenced by