Sha256: fe7196b1bde74f9c42391a1196bc168c567b66dc77d0a8f7e95dd218c6f7071d

Contents?: true

Size: 1.02 KB

Versions: 46

Compression:

Stored size: 1.02 KB

Contents

# encoding: utf-8

require 'money'

module Spree
  class Money
    class <<self
      attr_accessor :default_formatting_rules
    end
    self.default_formatting_rules = {
      # Ruby money currently has this as false, which is wrong for the vast
      # majority of locales.
      sign_before_symbol: true
    }

    attr_reader :money

    delegate :cents, to: :money

    def initialize(amount, options={})
      @money = Monetize.parse([amount, (options[:currency] || Spree::Config[:currency])].join)
      @options = Spree::Money.default_formatting_rules.merge(options)
    end

    def to_s
      @money.format(@options)
    end

    def to_html(options = { html: true })
      output = @money.format(@options.merge(options))
      if options[:html]
        # 1) prevent blank, breaking spaces
        # 2) prevent escaping of HTML character entities
        output = output.sub(" ", "&nbsp;").html_safe
      end
      output
    end

    def as_json(*)
      to_s
    end

    def ==(obj)
      @money == obj.money
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
spree_core-3.2.7 lib/spree/money.rb
spree_core-3.1.12 lib/spree/money.rb
spree_core-3.2.6 lib/spree/money.rb
spree_core-3.1.11 lib/spree/money.rb
spree_core-3.1.10 lib/spree/money.rb
spree_core-3.2.5 lib/spree/money.rb
spree_core-3.1.9 lib/spree/money.rb
spree_core-3.2.4 lib/spree/money.rb
spree_core-3.1.8 lib/spree/money.rb
spree_core-3.3.0.rc3 lib/spree/money.rb
spree_core-3.1.7 lib/spree/money.rb
spree_core-3.2.3 lib/spree/money.rb
spree_core-3.2.2 lib/spree/money.rb
spree_core-3.3.0.rc2 lib/spree/money.rb
spree_core-3.1.6 lib/spree/money.rb
spree_core-3.3.0.rc1 lib/spree/money.rb
spree_core-3.1.5 lib/spree/money.rb
spree_core-3.2.1 lib/spree/money.rb
spree_core-3.2.0 lib/spree/money.rb
spree_core-3.1.4 lib/spree/money.rb