Sha256: 3b309c0d61d8007d142b61cf05a0a58e44d108edd2269b022a63a896d60e4846

Contents?: true

Size: 1.03 KB

Versions: 64

Compression:

Stored size: 1.03 KB

Contents

module Spree
  class Order < Spree::Base
    module CurrencyUpdater
      extend ActiveSupport::Concern

      included do

        def homogenize_line_item_currencies
          update_line_item_currencies!
          update!
        end

      end

      # Updates prices of order's line items
      def update_line_item_currencies!
        line_items.where('currency != ?', currency).each do |line_item|
          update_line_item_price!(line_item)
        end
      end

      # Returns the price object from given item
      def price_from_line_item(line_item)
        line_item.variant.prices.where(currency: currency).first
      end

      # Updates price from given line item
      def update_line_item_price!(line_item)
        price = price_from_line_item(line_item)

        if price
          line_item.update_attributes!(currency: price.currency, price: price.amount)
        else
          raise RuntimeError, "no #{currency} price found for #{line_item.product.name} (#{line_item.variant.sku})"
        end
      end

    end
  end
end

Version data entries

64 entries across 64 versions & 2 rubygems

Version Path
solidus_core-1.1.4 app/models/spree/order/currency_updater.rb
solidus_core-1.0.7 app/models/spree/order/currency_updater.rb
spree_core-3.0.10 app/models/spree/order/currency_updater.rb
spree_core-3.0.9 app/models/spree/order/currency_updater.rb
solidus_core-1.0.6 app/models/spree/order/currency_updater.rb
spree_core-3.0.8 app/models/spree/order/currency_updater.rb
spree_core-3.0.7 app/models/spree/order/currency_updater.rb
solidus_core-1.1.3 app/models/spree/order/currency_updater.rb
solidus_core-1.0.5 app/models/spree/order/currency_updater.rb
spree_core-3.0.6.1 app/models/spree/order/currency_updater.rb
spree_core-3.0.6 app/models/spree/order/currency_updater.rb
spree_core-3.0.5 app/models/spree/order/currency_updater.rb
solidus_core-1.0.4 app/models/spree/order/currency_updater.rb
solidus_core-1.1.2 app/models/spree/order/currency_updater.rb
solidus_core-1.0.3 app/models/spree/order/currency_updater.rb
solidus_core-1.1.1 app/models/spree/order/currency_updater.rb
solidus_core-1.1.0 app/models/spree/order/currency_updater.rb
solidus_core-1.1.0.pre2 app/models/spree/order/currency_updater.rb
solidus_core-1.1.0.pre1 app/models/spree/order/currency_updater.rb
solidus_core-1.1.0.beta1 app/models/spree/order/currency_updater.rb