From 6cebbddb0ac2fa50ea9a750686f3ca9e0e18c977 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 30 Dec 2015 13:09:22 -0800 Subject: [PATCH] Remove Order::CurrencyUpdater This was intended to be used to update line items in an order to a new currency. Nowhere do we allow reassigning a currency to an existing order, or to line items, so this should not be needed. I don't think we should support changing currencies at all. If this was desirable behaviour for a store, they should implement it themselves as there are many caveats. --- core/app/models/spree/order.rb | 2 - .../models/spree/order/currency_updater.rb | 40 ------------------- .../spree/order/currency_updater_spec.rb | 32 --------------- 3 files changed, 74 deletions(-) delete mode 100644 core/app/models/spree/order/currency_updater.rb delete mode 100644 core/spec/models/spree/order/currency_updater_spec.rb diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb index 53d5926b956..37266410a25 100644 --- a/core/app/models/spree/order.rb +++ b/core/app/models/spree/order.rb @@ -9,7 +9,6 @@ class Order < Spree::Base ORDER_NUMBER_PREFIX = 'R' include Spree::Order::Checkout - include Spree::Order::CurrencyUpdater include Spree::Order::Payments class InsufficientStock < StandardError; end @@ -93,7 +92,6 @@ def states before_create :create_token before_create :link_by_email - before_update :homogenize_line_item_currencies, if: :currency_changed? validates :email, presence: true, if: :require_email validates :email, email: true, if: :require_email, allow_blank: true diff --git a/core/app/models/spree/order/currency_updater.rb b/core/app/models/spree/order/currency_updater.rb deleted file mode 100644 index 761bf2aa914..00000000000 --- a/core/app/models/spree/order/currency_updater.rb +++ /dev/null @@ -1,40 +0,0 @@ -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 diff --git a/core/spec/models/spree/order/currency_updater_spec.rb b/core/spec/models/spree/order/currency_updater_spec.rb deleted file mode 100644 index 6dd836474f6..00000000000 --- a/core/spec/models/spree/order/currency_updater_spec.rb +++ /dev/null @@ -1,32 +0,0 @@ -require 'spec_helper' - -describe Spree::Order, :type => :model do - context 'CurrencyUpdater' do - context "when changing order currency" do - let!(:line_item) { create(:line_item) } - let!(:euro_price) { create(:price, variant: line_item.variant, amount: 8, currency: 'EUR') } - - context "#homogenize_line_item_currencies" do - it "succeeds without error" do - line_item.order.update_attributes!(currency: 'EUR') - end - - it "changes the line_item currencies" do - expect { line_item.order.update_attributes!(currency: 'EUR') }.to change{ line_item.reload.currency }.from('USD').to('EUR') - end - - it "changes the line_item amounts" do - expect { line_item.order.update_attributes!(currency: 'EUR') }.to change{ line_item.reload.amount }.to(8) - end - - it "fails to change the order currency when no prices are available in that currency" do - expect { line_item.order.update_attributes!(currency: 'GBP') }.to raise_error(RuntimeError, /\Ano GBP price found/) - end - - it "calculates the item total in the order.currency" do - expect { line_item.order.update_attributes!(currency: 'EUR') }.to change{ line_item.order.item_total }.to(8) - end - end - end - end -end