From f170453493dd7c2d62f7b866867f7a3c8ec623d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Fri, 20 Jan 2012 16:26:45 -0300 Subject: [PATCH 1/2] Remove code duplication --- .../lib/action_view/helpers/number_helper.rb | 53 +++++++------------ 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index c560750927..d4f1c22c14 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -237,14 +237,8 @@ module ActionView def number_with_delimiter(number, options = {}) options.symbolize_keys! - begin - Float(number) - rescue ArgumentError, TypeError - if options[:raise] - raise InvalidNumberError, number - else - return number - end + parse_float_number(number, options[:raise]) do + return number end defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) @@ -253,7 +247,6 @@ module ActionView parts = number.to_s.to_str.split('.') parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}") parts.join(options[:separator]).html_safe - end # Formats a +number+ with the specified level of :precision (e.g., 112.32 has a precision @@ -290,14 +283,8 @@ module ActionView def number_with_precision(number, options = {}) options.symbolize_keys! - number = begin - Float(number) - rescue ArgumentError, TypeError - if options[:raise] - raise InvalidNumberError, number - else - return number - end + number = parse_float_number(number, options[:raise]) do + return number end defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) @@ -329,7 +316,6 @@ module ActionView else formatted_number end - end STORAGE_UNITS = [:byte, :kb, :mb, :gb, :tb].freeze @@ -367,14 +353,8 @@ module ActionView def number_to_human_size(number, options = {}) options.symbolize_keys! - number = begin - Float(number) - rescue ArgumentError, TypeError - if options[:raise] - raise InvalidNumberError, number - else - return number - end + number = parse_float_number(number, options[:raise]) do + return number end defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) @@ -486,14 +466,8 @@ module ActionView def number_to_human(number, options = {}) options.symbolize_keys! - number = begin - Float(number) - rescue ArgumentError, TypeError - if options[:raise] - raise InvalidNumberError, number - else - return number - end + number = parse_float_number(number, options[:raise]) do + return number end defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) @@ -536,6 +510,17 @@ module ActionView decimal_format.gsub(/%n/, formatted_number).gsub(/%u/, unit).strip.html_safe end + private + + def parse_float_number(number, raise_error) + Float(number) + rescue ArgumentError, TypeError + if raise_error + raise InvalidNumberError, number + else + yield + end + end end end end From 977f6b497843971419baa815f03f43a0e239a0ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Fri, 20 Jan 2012 16:37:48 -0300 Subject: [PATCH 2/2] Refactor translations retrieval --- .../lib/action_view/helpers/number_helper.rb | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index d4f1c22c14..b736f4424c 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -125,11 +125,10 @@ module ActionView options.symbolize_keys! - defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) - currency = I18n.translate(:'number.currency.format', :locale => options[:locale], :default => {}) + currency = translations_for('currency', options[:locale]) currency[:negative_format] ||= "-" + currency[:format] if currency[:format] - defaults = DEFAULT_CURRENCY_VALUES.merge(defaults).merge!(currency) + defaults = DEFAULT_CURRENCY_VALUES.merge(defaults_translations(options[:locale])).merge!(currency) defaults[:negative_format] = "-" + options[:format] if options[:format] options = defaults.merge!(options) @@ -152,7 +151,6 @@ module ActionView e.number.to_s.html_safe? ? formatted_number.html_safe : formatted_number end end - end # Formats a +number+ as a percentage string (e.g., 65%). You can customize the format in the +options+ hash. @@ -190,9 +188,7 @@ module ActionView options.symbolize_keys! - defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) - percentage = I18n.translate(:'number.percentage.format', :locale => options[:locale], :default => {}) - defaults = defaults.merge(percentage) + defaults = defaults_translations(options[:locale]).merge(translations_for('percentage', options[:locale])) options = options.reverse_merge(defaults) @@ -241,8 +237,7 @@ module ActionView return number end - defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) - options = options.reverse_merge(defaults) + options = options.reverse_merge(defaults_translations(options[:locale])) parts = number.to_s.to_str.split('.') parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}") @@ -287,9 +282,7 @@ module ActionView return number end - defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) - precision_defaults = I18n.translate(:'number.precision.format', :locale => options[:locale], :default => {}) - defaults = defaults.merge(precision_defaults) + defaults = defaults_translations(options[:locale]).merge(translations_for('precision', options[:locale])) options = options.reverse_merge(defaults) # Allow the user to unset default values: Eg.: :significant => false precision = options.delete :precision @@ -357,9 +350,7 @@ module ActionView return number end - defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) - human = I18n.translate(:'number.human.format', :locale => options[:locale], :default => {}) - defaults = defaults.merge(human) + defaults = defaults_translations(options[:locale]).merge(translations_for('human', options[:locale])) options = options.reverse_merge(defaults) #for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files @@ -470,9 +461,7 @@ module ActionView return number end - defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) - human = I18n.translate(:'number.human.format', :locale => options[:locale], :default => {}) - defaults = defaults.merge(human) + defaults = defaults_translations(options[:locale]).merge(translations_for('human', options[:locale])) options = options.reverse_merge(defaults) #for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files @@ -512,6 +501,14 @@ module ActionView private + def defaults_translations(locale) + I18n.translate(:'number.format', :locale => locale, :default => {}) + end + + def translations_for(namespace, locale) + I18n.translate(:"number.#{namespace}.format", :locale => locale, :default => {}) + end + def parse_float_number(number, raise_error) Float(number) rescue ArgumentError, TypeError