This project is archived and is in readonly mode.
[PATCH] Improvements in number_helper.rb
-
Bernardo Padua
Here is the patch (with ticket update keyword).
-
Bernardo Padua
- Assigned user set to José Valim
BTW, somebody please close #1340 Rounding problem in number_with_precision as it looks like it was already fixed.
-
José Valim
- Assigned user cleared.
- Milestone cleared.
-
José Valim
- State changed from open to new
-
Bernardo Padua
Thanks for the nice feedback, José and Jeremy. Following some suggestions by José (exchanged in portuguese, I won't bother printing them here... :)), I refactored the patch (and also found/fixed some minor issues), here is the 2nd take. In the process I rewrote the number_helper_i18n_test.rb, since it used mocking in a very brittle way and broke completely with the slightest refactoring.
Something I forgot to write in the ticket (but it is in the docs), is that you can also supply your custom formatting for
number_to_human:number_to_human(2000, :format => "Y%n%u", :units => {:thousand => "k"}) # => "Y2k"Any questions/requests let me know.
-
Bernardo Padua
I just discovered there was a line/hack in
number_to_currencywhich wasn't doing anything useful (its probably from when it didn't rely onnumber_with_precision), so I yanked it:separator = '' if precision == 0Now
number_to_currencylooks much better. Hope its not too late. -
Bernardo Padua
Ops, me one more time!
I just figured out rails 3 will have this
html_safething (I wasn't aware, since I am still working with 2.3), and most of the outputs from NumberHelper were nothtml_safe(only number_to_currency was, for some reason).I am trying to fix that, hold until I send the updated patch.
-
Bernardo Padua
Hi,
I finally added support for html_safe throughout NumberHelper methods. The updated patch follows (there are two commits, one for the previous improvements, other for this).
We have to make sure the number (which may also be a string in all helpers) does not have any unsafe stuff, so it was not just a matter of adding
.html_safeto everything that is returned. Most number helpers return thenumberparam unchanged if they are not a valid number (sometimes prepended/appended by something, like number + "%" innumber_to_percentage), so this is where the danger lies. To accommodate that I had to be a little bit more strict when parsing the numbers in some helpers (which were not doing any parsing) and had to add a way for the helpers to throw exceptions so they could be "bubbled up" to helpers that in turn relied on other helpers (eg.:number_to_currencyrelies onnumber_with_precision).Of course it is tested. I also structured the tests a little bit more to make a few of those things more clear (methods returning number param unchanged, returning nil when number is nil, etc).
Waiting for your reviews now.
-
Repository
- State changed from new to resolved
(from [75904c566e3ea475045450ba8fb1a74070a94fcb]) Adds number_to_human and several improvements in NumberHelper. [#4239 [PATCH] Improvements in number_helper.rb state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/75904c566e3ea475045450ba8fb1a7...
