Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Update `Spree::OrderMerger` and add `Spree::Seeds::PaymentMethods` (#12082)

Łukasz Adamczyk37b7f5266f45
diff --git a/.rubocop.yml b/.rubocop.yml
index 0b0b914dd8..a4c8f7ca76 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -189,7 +189,10 @@ RSpec/NamedSubject:
RSpec/MultipleExpectations:
Enabled: false
-RSpec/FilePath:
+RSpec/SpecFilePathFormat:
+ Enabled: false
+
+RSpec/SpecFilePathSuffix:
Enabled: false
RSpec/LetSetup:
diff --git a/common_spree_dependencies.rb b/common_spree_dependencies.rb
index 3068499806..3dfd7e98ad 100644
--- a/common_spree_dependencies.rb
+++ b/common_spree_dependencies.rb
@@ -63,4 +63,6 @@ end
group :development do
# gem 'github_fast_changelog'
gem 'solargraph'
+ gem 'ruby-lsp'
+ gem 'ruby-lsp-rails'
end
diff --git a/core/app/models/spree/order_merger.rb b/core/app/models/spree/order_merger.rb
index 83c29af6ff..3951b99718 100644
--- a/core/app/models/spree/order_merger.rb
+++ b/core/app/models/spree/order_merger.rb
@@ -16,6 +16,7 @@ module Spree
end
set_user(user)
+ clear_addresses(other_order)
persist_merge
# So that the destroy doesn't take out line items which may have been re-assigned
@@ -39,6 +40,11 @@ module Spree
order.associate_user!(user) if !order.user && !user.blank?
end
+ def clear_addresses(other_order)
+ other_order.ship_address = nil
+ other_order.bill_address = nil
+ end
+
# The idea is the end developer can choose to override the merge
# to their own choosing. Default is merge with errors.
def handle_merge(current_line_item, other_order_line_item)
diff --git a/core/app/services/spree/seeds/all.rb b/core/app/services/spree/seeds/all.rb
index ff7d01bc2f..80fab48499 100644
--- a/core/app/services/spree/seeds/all.rb
+++ b/core/app/services/spree/seeds/all.rb
@@ -21,6 +21,9 @@ module Spree
# store & stock location
Stores.call
StockLocations.call
+
+ # add store resources
+ PaymentMethods.call
end
end
end
diff --git a/core/app/services/spree/seeds/payment_methods.rb b/core/app/services/spree/seeds/payment_methods.rb
new file mode 100644
index 0000000000..db1d011ed4
--- /dev/null
+++ b/core/app/services/spree/seeds/payment_methods.rb
@@ -0,0 +1,18 @@
+module Spree
+ module Seeds
+ class PaymentMethods
+ prepend Spree::ServiceModule::Base
+
+ def call
+ payment_method = Spree::PaymentMethod::StoreCredit.find_or_initialize_by(
+ name: Spree.t(:store_credit_name),
+ description: Spree.t(:store_credit_name),
+ active: true
+ )
+
+ payment_method.stores = Spree::Store.all if payment_method.new_record?
+ payment_method.save!
+ end
+ end
+ end
+end
diff --git a/core/spec/models/spree/order_merger_spec.rb b/core/spec/models/spree/order_merger_spec.rb
index 13d104dfce..a52033573f 100644
--- a/core/spec/models/spree/order_merger_spec.rb
+++ b/core/spec/models/spree/order_merger_spec.rb
@@ -129,5 +129,18 @@ module Spree
expect(order_1.errors.full_messages).not_to be_empty
end
end
+
+ context 'merging an order with addresses assigned to an other complete order' do
+ let!(:complete_order) { create(:order_ready_to_ship) }
+
+ before do
+ order_2.update!(ship_address: complete_order.ship_address, bill_address: complete_order.bill_address)
+ end
+
+ it 'destroys the other order' do
+ subject.merge!(order_2)
+ expect { order_2.reload }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
end
end
diff --git a/core/spec/services/spree/seeds/payment_methods_spec.rb b/core/spec/services/spree/seeds/payment_methods_spec.rb
new file mode 100644
index 0000000000..855ff4feae
--- /dev/null
+++ b/core/spec/services/spree/seeds/payment_methods_spec.rb
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Seeds::PaymentMethods do
+ subject { described_class.call }
+
+ let(:store_credit_payment_method) { Spree::PaymentMethod::StoreCredit.last }
+ let(:store) { Spree::Store.default }
+
+ let!(:other_store) { create(:store) }
+
+ it 'creates a Store Credit payment method' do
+ expect { subject }.to change(Spree::PaymentMethod::StoreCredit, :count).by(1)
+
+ expect(store_credit_payment_method).to be_present
+ expect(store_credit_payment_method).to be_active
+ expect(store_credit_payment_method.stores).to contain_exactly(store, other_store)
+ expect(store_credit_payment_method.name).to eq('Store Credit')
+ expect(store_credit_payment_method.description).to eq('Store Credit')
+ end
+
+ context 'when the Store Credit payment method already exists' do
+ before do
+ create(
+ :store_credit_payment_method,
+ stores: [store, other_store],
+ name: 'Store Credit',
+ description: 'Store Credit',
+ active: true
+ )
+ end
+
+ it "doesn't create a new payment method" do
+ expect { subject }.not_to change(Spree::PaymentMethod::StoreCredit, :count)
+
+ expect(store_credit_payment_method).to be_present
+ expect(store_credit_payment_method).to be_active
+ expect(store_credit_payment_method.stores).to contain_exactly(store, other_store)
+ expect(store_credit_payment_method.name).to eq('Store Credit')
+ expect(store_credit_payment_method.description).to eq('Store Credit')
+ end
+ end
+end