Sha256: eaa105839d51164616dba4a68a6b7c2537097b49f943b5525edb4f848a4c8b6f

Contents?: true

Size: 805 Bytes

Versions: 1

Compression:

Stored size: 805 Bytes

Contents

require "formatted_attributes"
require "active_support/all"
require "action_view/helpers/number_helper"

ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"

load File.dirname(__FILE__) + "/schema.rb"

class Product < ActiveRecord::Base
  include ActionView::Helpers::NumberHelper
  formatted :price, :shipping, :with => :cents

  validates_numericality_of :price, :shipping, :only_integer => true

  private
  def format_to_cents(number)
    _, operator, number, precision = *number.to_s.match(/^([+-])?(\d+)(?:[,.](\d+))?$/)
    (BigDecimal("#{operator}#{number}.#{precision.to_i}") * 100).to_i
  end

  def format_from_cents(number)
    number = BigDecimal(number.to_s) / 100
    number_to_currency(number, :unit => "", :separator => ",", :delimiter => "")
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
formatted_attributes-0.1.0 spec/spec_helper.rb