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

Ticket #7892: number_helper_added_unitside_option.diff

File number_helper_added_unitside_option.diff, 1.9 kB (added by HappyCoder, 2 years ago)
  • actionpack/lib/action_view/helpers/number_helper.rb

    old new  
    4444      # in the +options+ hash. 
    4545      # * <tt>:precision</tt>  -  Sets the level of precision, defaults to 2 
    4646      # * <tt>:unit</tt>  - Sets the denomination of the currency, defaults to "$" 
     47      # * <tt>:unitside</tt>  - Sets the position of the unit, defaults to "left" 
    4748      # * <tt>:separator</tt>  - Sets the separator between the units, defaults to "." 
    4849      # * <tt>:delimiter</tt>  - Sets the thousands delimiter, defaults to "," 
    4950      # 
     
    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(15.5, :unit => " kr.", :unitside => "right")    => 15.5 kr. 
    5557      def number_to_currency(number, options = {}) 
    5658        options   = options.stringify_keys 
    5759        precision = options["precision"] || 2 
    5860        unit      = options["unit"] || "$" 
     61        unitside = options["unitside"] || "left" 
    5962        separator = precision > 0 ? options["separator"] || "." : "" 
    6063        delimiter = options["delimiter"] || "," 
    6164 
    6265        begin 
    6366          parts = number_with_precision(number, precision).split('.') 
    64           unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s 
     67          if unitside == "left": 
     68            unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s 
     69          elsif unitside == "right": 
     70            number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s + unit 
     71          end 
    6572        rescue 
    6673          number 
    6774        end