Ticket #6351: to_currency_helper_with_order.patch
| File to_currency_helper_with_order.patch, 2.5 kB (added by jesperronn, 2 years ago) |
|---|
-
actionpack/lib/action_view/helpers/number_helper.rb
old new 34 34 # number_to_currency(1234567890.506) => $1,234,567,890.51 35 35 # number_to_currency(1234567890.50, {:unit => "£", :separator => ",", :delimiter => ""}) => £1234567890,50 36 36 def number_to_currency(number, options = {}) 37 options[:order] ||= [:unit, :number] 37 38 options = options.stringify_keys 38 39 precision, unit, separator, delimiter = options.delete("precision") { 2 }, options.delete("unit") { "$" }, options.delete("separator") { "." }, options.delete("delimiter") { "," } 39 40 separator = "" unless precision > 0 41 42 #add leading space before trailing unit 43 unit = " " + unit if options[:order] == [:number, :unit] 44 output = '' 40 45 begin 41 parts = number_with_precision(number, precision).split('.') 42 unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s 46 options["order"].each do |param| 47 case param 48 when :unit 49 output << unit 50 when :number 51 parts = number_with_precision(number, precision).split('.') 52 output << number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s 53 end 54 end 43 55 rescue 44 number56 output = number 45 57 end 58 output 46 59 end 47 60 48 61 # Formats a +number+ as into a percentage string. The +options+ hash can be used to customize the format of the output. … … 76 89 end 77 90 78 91 # Returns a formatted-for-humans file size. 79 # 92 # 80 93 # Examples: 81 94 # human_size(123) => 123 Bytes 82 95 # human_size(1234) => 1.2 KB … … 84 97 # human_size(1234567) => 1.2 MB 85 98 # human_size(1234567890) => 1.1 GB 86 99 def number_to_human_size(size) 87 case 100 case 88 101 when size == 1 : '1 Byte' 89 102 when size < 1.kilobyte: '%d Bytes' % size 90 103 when size < 1.megabyte: '%.1f KB' % (size / 1.0.kilobyte) … … 95 108 rescue 96 109 nil 97 110 end 98 111 99 112 alias_method :human_size, :number_to_human_size # deprecated alias 100 113 101 114 # Formats a +number+ with a level of +precision+.