Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6351: to_currency_helper_with_order.3.patch

File to_currency_helper_with_order.3.patch, 3.2 kB (added by jesperronn, 1 year ago)

Updated patch and tested with recent version of trunk (revision 6661)

  • actionpack/lib/action_view/helpers/number_helper.rb

    old new  
    4646      # * <tt>:unit</tt>  - Sets the denomination of the currency, defaults to "$" 
    4747      # * <tt>:separator</tt>  - Sets the separator between the units, defaults to "." 
    4848      # * <tt>:delimiter</tt>  - Sets the thousands delimiter, defaults to "," 
     49      # * <tt>:order</tt> - Sets the order that the unit and the number appear, defaults to  
     50      #   [:unit, :number]. If the unit comes after the number, a space will automatically be  
     51      #   inserted before the unit. 
    4952      # 
    5053      #  number_to_currency(1234567890.50)     => $1,234,567,890.50 
    5154      #  number_to_currency(1234567890.506)    => $1,234,567,890.51 
    5255      #  number_to_currency(1234567890.506, :precision => 3)    => $1,234,567,890.506 
    5356      #  number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "") 
    5457      #     => &pound;1234567890,50 
     58      #  number_to_currency(1234567890.50, :unit => "GBP", :order => [:number, :unit]) => 1,234,567,890.50 GBP  
    5559      def number_to_currency(number, options = {}) 
    5660        options   = options.stringify_keys 
    5761        precision = options["precision"] || 2 
    5862        unit      = options["unit"] || "$" 
    5963        separator = precision > 0 ? options["separator"] || "." : "" 
    6064        delimiter = options["delimiter"] || "," 
    61  
     65        order     = options["order"] || [:unit, :number]  
     66         
     67        unit      = " " + unit  
    6268        begin 
    63           parts = number_with_precision(number, precision).split('.') 
    64           unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s 
     69          output = order.map do |item|  
     70            case item  
     71            when :unit  
     72              unit  
     73            when :number  
     74              parts = number_with_precision(number, precision).split('.')  
     75              number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s  
     76            end  
     77          end  
     78          output = output.join("").strip 
    6579        rescue 
    66           number 
     80          output = number 
    6781        end 
     82        output 
    6883      end 
    6984 
    7085      # Formats a +number+ as a percentage string. You can customize the 
  • actionpack/test/template/number_helper_test.rb

    old new  
    2424    assert_equal("$1,234,567,892", number_to_currency(1234567891.50, {:precision => 0})) 
    2525    assert_equal("$1,234,567,890.5", number_to_currency(1234567890.50, {:precision => 1})) 
    2626    assert_equal("&pound;1234567890,50", number_to_currency(1234567890.50, {:unit => "&pound;", :separator => ",", :delimiter => ""})) 
     27    assert_equal("1,234,567,890.50 GBP", number_to_currency(1234567890.50, {:unit => "GBP", :order => [:number, :unit]}))  
    2728    assert_equal("$1,234,567,890.50", number_to_currency("1234567890.50")) 
    2829    assert_equal("$x.", number_to_currency("x")) 
    2930    assert_nil number_to_currency(nil)