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

Ticket #6351: to_currency_helper_with_order.2.patch

File to_currency_helper_with_order.2.patch, 3.0 kB (added by chrismear, 2 years ago)

Updated patch with doc and test

  • actionpack/test/template/number_helper_test.rb

    old new  
    2525    assert_equal("$1,234,567,890", number_to_currency(1234567890.50, {:precision => 0})) 
    2626    assert_equal("$1,234,567,890.5", number_to_currency(1234567890.50, {:precision => 1})) 
    2727    assert_equal("£1234567890,50", number_to_currency(1234567890.50, {:unit => "£", :separator => ",", :delimiter => ""})) 
     28    assert_equal("1,234,567,890.50 GBP", number_to_currency(1234567890.50, {:unit => "GBP", :order => [:number, :unit]})) 
    2829    assert_equal("$1,234,567,890.50", number_to_currency("1234567890.50")) 
    2930    assert_equal("$x.", number_to_currency("x")) 
    3031    assert_nil number_to_currency(nil) 
  • 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 [:unit, :number]. If the unit comes after the number, a space will automatically be inserted before the unit. 
    4950      # 
    5051      #  number_to_currency(1234567890.50)     => $1,234,567,890.50 
    5152      #  number_to_currency(1234567890.506)    => $1,234,567,890.51 
    5253      #  number_to_currency(1234567890.506, :precision => 3)    => $1,234,567,890.506 
    5354      #  number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "")  
    5455      #     => &pound;1234567890,50 
     56      #  number_to_currency(1234567890.50, :unit => "GBP", :order => [:number, :unit]) => 1,234,567,890.50 GBP 
    5557      def number_to_currency(number, options = {}) 
    5658        options   = options.stringify_keys 
    5759        precision = options["precision"] || 2 
    5860        unit      = options["unit"] || "$" 
    5961        separator = precision > 0 ? options["separator"] || "." : "" 
    6062        delimiter = options["delimiter"] || "," 
     63        order     = options["order"] || [:unit, :number] 
    6164         
     65        unit = " " + unit 
     66         
    6267        begin 
    63           parts = number_with_precision(number, precision).split('.') 
    64           unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s 
     68          output = order.map do |item| 
     69            case item 
     70            when :unit 
     71              unit 
     72            when :number 
     73              parts = number_with_precision(number, precision).split('.') 
     74              number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s 
     75            end 
     76          end 
     77          output.join("").strip 
    6578        rescue 
    6679          number 
    6780        end