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.

Extend Stock Location, add `Store#default_stock_location` and `Admin:: StockLocationsHelper`

Damian Legawiec16f2e4a0e356
diff --git a/admin/app/helpers/spree/admin/stock_locations_helper.rb b/admin/app/helpers/spree/admin/stock_locations_helper.rb
new file mode 100644
index 0000000000..836bca8ab9
--- /dev/null
+++ b/admin/app/helpers/spree/admin/stock_locations_helper.rb
@@ -0,0 +1,22 @@
+module Spree
+ module Admin
+ module StockLocationsHelper
+ def available_stock_locations(_opts = {})
+ Spree::StockLocation.order_default.active.accessible_by(current_ability)
+ end
+
+ def available_stock_locations_list(opts = {})
+ available_stock_locations(opts).map { |stock_location| [stock_location.display_name, stock_location.id] }
+ end
+
+ # overwrite this to customize behavior
+ def available_stock_locations_for_product(_product)
+ available_stock_locations
+ end
+
+ def default_stock_location_for_product(_product)
+ current_store.default_stock_location
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/stock_location.rb b/core/app/models/spree/stock_location.rb
index 0ed07da8f4..61497f4f74 100644
--- a/core/app/models/spree/stock_location.rb
+++ b/core/app/models/spree/stock_location.rb
@@ -28,6 +28,8 @@ module Spree
after_save :ensure_one_default
after_update :conditional_touch_records
+ delegate :name, to: :country, prefix: true
+
def state_text
state.try(:abbr) || state.try(:name) || state_name
end
@@ -137,6 +139,10 @@ module Spree
)
end
+ def display_name
+ @display_name ||= [admin_name, name].delete_if(&:blank?).join(' / ')
+ end
+
private
def create_stock_items
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index c375fa94fc..7dec5d925e 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -199,6 +199,13 @@ module Spree
@checkout_zone_or_default ||= checkout_zone || Spree::Zone.default_checkout_zone
end
+ def default_stock_location
+ @default_stock_location ||= begin
+ stock_location_scope = Spree::StockLocation.order_default
+ stock_location_scope.first || stock_location_scope.create(default: true, name: Spree.t(:default_stock_location_name), country: default_country)
+ end
+ end
+
def favicon
return unless favicon_image&.attachment&.attached?
diff --git a/core/spec/models/spree/stock_location_spec.rb b/core/spec/models/spree/stock_location_spec.rb
index 485471165f..4a6c9dab66 100644
--- a/core/spec/models/spree/stock_location_spec.rb
+++ b/core/spec/models/spree/stock_location_spec.rb
@@ -319,5 +319,27 @@ module Spree
expect(subject.address).to be_an_instance_of(Spree::Address)
end
end
+
+ describe '#display_name' do
+ it 'returns the name' do
+ expect(subject.display_name).to eq(subject.name)
+ end
+
+ context 'with admin name set' do
+ let(:admin_name) { 'admin name' }
+
+ before { subject.admin_name = admin_name }
+
+ it 'returns the admin name' do
+ expect(subject.display_name).to eq("#{admin_name} / #{subject.name}")
+ end
+ end
+ end
+
+ describe '#country_name' do
+ it 'returns the country name' do
+ expect(subject.country_name).to eq(subject.country.name)
+ end
+ end
end
end
diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rb
index e68ed1040e..c2c867704d 100644
--- a/core/spec/models/spree/store_spec.rb
+++ b/core/spec/models/spree/store_spec.rb
@@ -673,4 +673,23 @@ describe Spree::Store, type: :model do
end
end
end
+
+ describe '#default_stock_location' do
+ context 'with default stock location' do
+ let!(:default_stock_location) { create(:stock_location, default: true) }
+
+ it 'returns the default stock location' do
+ expect(subject.default_stock_location).to eq(default_stock_location)
+ end
+ end
+
+ context 'without default stock location' do
+ it 'creates a new default stock location' do
+ expect { subject.default_stock_location }.to change(Spree::StockLocation, :count).by(1)
+ expect(subject.default_stock_location.default?).to eq(true)
+ expect(subject.default_stock_location.country).to eq(subject.default_country)
+ expect(subject.default_stock_location.name).to eq(Spree.t(:default_stock_location_name))
+ end
+ end
+ end
end