Sha256: c561d052090e4121170e01aa01f37d3bd9c568b8b83b797682e370a4dbc86509

Contents?: true

Size: 1.55 KB

Versions: 34

Compression:

Stored size: 1.55 KB

Contents

require 'money'

Money.locale_backend = :i18n

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, :currency, 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 amount_in_cents
      (cents / currency.subunit_to_unit.to_f * 100).round
    end

    def to_s
      money.format(options)
    end

    # 1) prevent blank, breaking spaces
    # 2) prevent escaping of HTML character entities
    def to_html(opts = { html: true })
      # html option is deprecated and we need to fallback to html_wrap
      opts[:html_wrap] = opts[:html]
      opts.delete(:html)

      output = money.format(options.merge(opts))
      if opts[:html_wrap]
        output.gsub!(/<\/?[^>]*>/, '') # we don't want wrap every element in span
        output = output.sub(' ', '&nbsp;').html_safe
      end

      output
    end

    def as_json(*)
      to_s
    end

    def decimal_mark
      options[:decimal_mark] || money.decimal_mark
    end

    def thousands_separator
      options[:thousands_separator] || money.thousands_separator
    end

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

    private

    attr_reader :options
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
spree_core-4.0.9 lib/spree/money.rb
spree_core-4.0.8 lib/spree/money.rb
spree_core-3.7.14.1 lib/spree/money.rb
spree_core-4.0.7.1 lib/spree/money.rb
spree_core-4.0.7 lib/spree/money.rb
spree_core-3.7.14 lib/spree/money.rb
spree_core-4.0.6 lib/spree/money.rb
spree_core-3.7.13 lib/spree/money.rb
spree_core-4.0.5 lib/spree/money.rb
spree_core-3.7.12 lib/spree/money.rb
spree_core-4.0.4 lib/spree/money.rb
spree_core-3.7.11 lib/spree/money.rb
spree_core-3.7.10 lib/spree/money.rb
spree_core-4.0.3 lib/spree/money.rb
spree_core-3.7.9 lib/spree/money.rb
spree_core-4.0.2 lib/spree/money.rb
spree_core-3.7.8 lib/spree/money.rb
spree_core-4.0.1 lib/spree/money.rb
spree_core-3.7.7 lib/spree/money.rb
spree_core-4.0.0 lib/spree/money.rb