diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-05-28 22:58:41 -0300 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-05-28 22:58:42 -0300 |
commit | ef3e696d1fbfde7a77592bc1fd24586a214c505d (patch) | |
tree | 358214b477228580044f77d202e036da7a00a7a8 /actionpack | |
parent | 68f23bea6b3eec48f26e87ab9cdf3ff361820831 (diff) | |
download | rails-ef3e696d1fbfde7a77592bc1fd24586a214c505d.tar.gz rails-ef3e696d1fbfde7a77592bc1fd24586a214c505d.tar.bz2 rails-ef3e696d1fbfde7a77592bc1fd24586a214c505d.zip |
Parse float value only once in number helpers
Refactor number helpers output with safety handling to call float
parsing only once. Also remove 'erb' require.
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_view/helpers/number_helper.rb | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index b743f5e0c9..61010f4fc3 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -5,7 +5,6 @@ require 'active_support/core_ext/object/blank' require 'active_support/core_ext/numeric' require 'active_support/core_ext/string/output_safety' require 'active_support/number_helper' -require 'erb' module ActionView # = Action View Number Helpers @@ -112,7 +111,7 @@ module ActionView def number_to_currency(number, options = {}) return unless number options = escape_unsafe_delimiters_and_separators(options.symbolize_keys) - + wrap_with_output_safety_handling(number, options[:raise]){ ActiveSupport::NumberHelper.number_to_currency(number, options) } end @@ -155,7 +154,7 @@ module ActionView def number_to_percentage(number, options = {}) return unless number options = escape_unsafe_delimiters_and_separators(options.symbolize_keys) - + wrap_with_output_safety_handling(number, options[:raise]){ ActiveSupport::NumberHelper.number_to_percentage(number, options) } end @@ -400,25 +399,26 @@ module ActionView end private - + def escape_unsafe_delimiters_and_separators(options) options[:separator] = ERB::Util.html_escape(options[:separator]) if options[:separator] && !options[:separator].html_safe? options[:delimiter] = ERB::Util.html_escape(options[:delimiter]) if options[:delimiter] && !options[:delimiter].html_safe? options end - + def wrap_with_output_safety_handling(number, raise_on_invalid, &block) - raise InvalidNumberError, number if raise_on_invalid && !valid_float?(number) - + valid_float = valid_float?(number) + raise InvalidNumberError, number if raise_on_invalid && !valid_float + formatted_number = yield - - if valid_float?(number) || number.html_safe? + + if valid_float || number.html_safe? formatted_number.html_safe else formatted_number - end + end end - + def valid_float?(number) !parse_float(number, false).nil? end |