Sha256: fb9d8ce03fd1cd88d0d6824e5e7529310b7827b2be5b18e87a2cb977f7e1376e

Contents?: true

Size: 1.03 KB

Versions: 14

Compression:

Stored size: 1.03 KB

Contents

module Spree
  class Order < ActiveRecord::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

14 entries across 14 versions & 1 rubygems

Version Path
spree_core-2.2.14 app/models/spree/order/currency_updater.rb
spree_core-2.2.13 app/models/spree/order/currency_updater.rb
spree_core-2.2.12 app/models/spree/order/currency_updater.rb
spree_core-2.2.11 app/models/spree/order/currency_updater.rb
spree_core-2.2.10 app/models/spree/order/currency_updater.rb
spree_core-2.2.9 app/models/spree/order/currency_updater.rb
spree_core-2.2.8 app/models/spree/order/currency_updater.rb
spree_core-2.2.7 app/models/spree/order/currency_updater.rb
spree_core-2.2.6 app/models/spree/order/currency_updater.rb
spree_core-2.2.5 app/models/spree/order/currency_updater.rb
spree_core-2.2.4 app/models/spree/order/currency_updater.rb
spree_core-2.2.3 app/models/spree/order/currency_updater.rb
spree_core-2.2.2 app/models/spree/order/currency_updater.rb
spree_core-2.2.1 app/models/spree/order/currency_updater.rb