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

Ticket #7892: number_helper_added_unitside_option_with_test.patch

File number_helper_added_unitside_option_with_test.patch, 2.8 kB (added by kamal, 1 year ago)

adds tests for this patch, modified slightly to patch cleanly against trunk r7128

  • 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("£1234567890,50", number_to_currency(1234567890.50, {:unit => "£", :separator => ",", :delimiter => ""})) 
     27    assert_equal("1234567890.50 kr.", number_to_currency(1234567890.50, {:unit => " kr.", :unitside => "right", :delimiter => ""})) 
    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) 
  • actionpack/lib/action_view/helpers/number_helper.rb

    old new  
    5050      # in the +options+ hash. 
    5151      # 
    5252      # ==== Options 
    53       # * <tt>:precision</tt>  -  Sets the level of precision (defaults to 2). 
     53      # * <tt>:precision</tt>  - Sets the level of precision (defaults to 2). 
    5454      # * <tt>:unit</tt>  - Sets the denomination of the currency (defaults to "$"). 
     55      # * <tt>:unitside</tt>  - Sets the position of the unit (defaults to "left"). 
    5556      # * <tt>:separator</tt>  - Sets the separator between the units (defaults to "."). 
    5657      # * <tt>:delimiter</tt>  - Sets the thousands delimiter (defaults to ","). 
    5758      # 
     
    6263      # 
    6364      #  number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "") 
    6465      #  # => &pound;1234567890,50 
     66      #  number_to_currency(15.5, :unit => " kr.", :unitside => "right") 
     67      #  # => 15.5 kr. 
    6568      def number_to_currency(number, options = {}) 
    6669        options   = options.stringify_keys 
    6770        precision = options["precision"] || 2 
    6871        unit      = options["unit"] || "$" 
     72        unitside = options["unitside"] || "left" 
    6973        separator = precision > 0 ? options["separator"] || "." : "" 
    7074        delimiter = options["delimiter"] || "," 
    7175 
    7276        begin 
    7377          parts = number_with_precision(number, precision).split('.') 
    74           unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s 
     78          if unitside == "left" 
     79            unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s 
     80          elsif unitside == "right" 
     81            number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s + unit 
     82          end 
    7583        rescue 
    7684          number 
    7785        end