Module: Spree::DisplayMoney

Included in:
Adjustment, CustomerReturn, LineItem, Order, Price, ReturnAuthorization, ReturnItem, Shipment, ShippingRate, ShippingRateTax, StoreCredit, UserReporting
Defined in:
app/models/concerns/spree/display_money.rb

Instance Method Summary collapse

Instance Method Details

#money_methods(*args) ⇒ Object

Takes a list of methods that the base object wants to be able to use to display with Spree::Money, and turns them into display_* methods. Intended to help clean up some of the presentational logic that exists at the model layer.

Examples

Decorate a method, with the default option of using the base object’s currency

extend Spree::DisplayMoney
money_methods :tax_amount, :price

Decorate a method, but with some additional options

extend Spree::DisplayMoney
money_methods tax_amount: { currency: "CAD", no_cents: true }, :price


22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/concerns/spree/display_money.rb', line 22

def money_methods(*args)
  args.each do |money_method|
    money_method = { money_method => {} } unless money_method.is_a? Hash
    money_method.each do |method_name, opts|
      define_method("display_#{method_name}") do
        default_opts = respond_to?(:currency) ? { currency: } : {}
        Spree::Money.new(send(method_name), default_opts.merge(opts))
      end
    end
  end
end