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.

* refactor after code review * adjust specs

Łukasz Adamczykae5673c19da2
diff --git a/backend/app/controllers/spree/admin/products_controller.rb b/backend/app/controllers/spree/admin/products_controller.rb
index ac69037808..415b61233c 100644
--- a/backend/app/controllers/spree/admin/products_controller.rb
+++ b/backend/app/controllers/spree/admin/products_controller.rb
@@ -4,7 +4,6 @@ module Spree
helper 'spree/products'
before_action :load_data, except: :index
- before_action :load_stores, only: [:edit, :update]
create.before :create_before
update.before :update_before
@@ -107,10 +106,6 @@ module Spree
@shipping_categories = ShippingCategory.order(:name)
end
- def load_stores
- @stores = Spree::Store.pluck(:name, :id)
- end
-
def collection
return @collection if @collection.present?
diff --git a/backend/app/views/spree/admin/products/_form.html.erb b/backend/app/views/spree/admin/products/_form.html.erb
index d868ae31cd..963f43e26b 100644
--- a/backend/app/views/spree/admin/products/_form.html.erb
+++ b/backend/app/views/spree/admin/products/_form.html.erb
@@ -253,7 +253,7 @@
<%= f.field_container :stores, class: ['form-group'] do %>
<%= f.label :store_ids, Spree.t(:stores) %>
<% if can? :modify, Spree::Store %>
- <%= f.select :store_ids, @stores, { include_blank: true }, { class: 'select2-clear', multiple: true, "data-placeholder" => Spree.t(:select_stores) } %>
+ <%= f.select :store_ids, @stores.pluck(:name, :id), { include_blank: true }, { class: 'select2-clear', multiple: true, "data-placeholder" => Spree.t(:select_stores) } %>
<% elsif @product.stores.any? %>
<ul class="text_list">
<% @product.stores.each do |store| %>
diff --git a/backend/spec/spec_helper.rb b/backend/spec/spec_helper.rb
index 2869c822f5..0a3d4ced85 100644
--- a/backend/spec/spec_helper.rb
+++ b/backend/spec/spec_helper.rb
@@ -31,7 +31,6 @@ require 'rspec/rails'
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
-
require 'capybara-select-2'
require 'database_cleaner'
require 'ffaker'
@@ -52,8 +51,6 @@ require 'spree/testing_support/flatpickr_capybara'
require 'spree/core/controller_helpers/strong_parameters'
require 'webdrivers'
-Capybara.javascript_driver = :selenium_chrome
-
RSpec.configure do |config|
config.color = true
config.default_formatter = 'doc'
diff --git a/core/app/models/spree/store_product.rb b/core/app/models/spree/store_product.rb
index 17f073eb29..2fb0539a9e 100644
--- a/core/app/models/spree/store_product.rb
+++ b/core/app/models/spree/store_product.rb
@@ -2,7 +2,7 @@ module Spree
class StoreProduct < Spree::Base
self.table_name = 'spree_products_stores'
- belongs_to :store, class_name: 'Spree::Store'
+ belongs_to :store, class_name: 'Spree::Store', touch: true
belongs_to :product, class_name: 'Spree::Product', touch: true
validates :store, :product, presence: true
diff --git a/core/lib/spree/testing_support/factories/product_factory.rb b/core/lib/spree/testing_support/factories/product_factory.rb
index ffafe2a9f8..6860ea8238 100644
--- a/core/lib/spree/testing_support/factories/product_factory.rb
+++ b/core/lib/spree/testing_support/factories/product_factory.rb
@@ -9,9 +9,22 @@ FactoryBot.define do
deleted_at { nil }
shipping_category { |r| Spree::ShippingCategory.first || r.association(:shipping_category) }
+ transient do
+ with_store { true }
+ end
+
# ensure stock item will be created for this products master
before(:create) { create(:stock_location) unless Spree::StockLocation.any? }
+ before(:create) do |product, evaluator|
+ if evaluator.with_store && product.stores.empty?
+ default_store = Spree::Store.default.persisted? ? Spree::Store.default : nil
+ store = default_store || create(:store)
+
+ product.stores << store
+ end
+ end
+
factory :custom_product do
name { 'Custom Product' }
price { 17.99 }
diff --git a/core/spec/models/spree/store_product_spec.rb b/core/spec/models/spree/store_product_spec.rb
index c174d8475a..d170f15981 100644
--- a/core/spec/models/spree/store_product_spec.rb
+++ b/core/spec/models/spree/store_product_spec.rb
@@ -1,33 +1,40 @@
require 'spec_helper'
describe Spree::StoreProduct, type: :model do
- let!(:store) { create(:store) }
- let!(:store_2) { create(:store) }
- let!(:product) { create(:product) }
+ let!(:store) { create(:store) }
+ let!(:product) { create(:product, with_store: false) }
context 'assigning store' do
+ subject(:assign_store) { product.stores << store }
+
it 'touches product' do
- expect { product.stores << store }.to change { product.reload.updated_at }
+ expect { assign_store }.to change(product, :updated_at)
+ end
+
+ it 'touches store' do
+ expect { assign_store }.to change(store, :updated_at)
end
it 'increases Store Product count' do
- expect { product.stores << store }.to change(described_class, :count).from(0).to(1)
+ expect { assign_store }.to change(described_class, :count).from(0).to(1)
end
end
context 'unassignes store' do
+ subject(:unassign_store) { product.stores.destroy(store) }
+
before { product.stores << store }
it 'touches product' do
- expect { product.stores.destroy(store) }.to change { product.reload.updated_at }
+ expect { unassign_store }.to change(product, :updated_at)
end
it 'decreases Store Product count' do
- expect { product.stores.destroy(store) }.to change(described_class, :count).from(1).to(0)
+ expect { unassign_store }.to change(described_class, :count).from(1).to(0)
end
it 'does not remove store' do
- expect { product.stores.destroy(store) }.not_to change { Spree::Store.count }
+ expect { unassign_store }.not_to change { Spree::Store.count }
end
end
end
diff --git a/frontend/app/controllers/spree/products_controller.rb b/frontend/app/controllers/spree/products_controller.rb
index 500e6fbc14..7bdd8f60db 100644
--- a/frontend/app/controllers/spree/products_controller.rb
+++ b/frontend/app/controllers/spree/products_controller.rb
@@ -62,7 +62,7 @@ module Spree
end
def can_show_product?
- raise ActiveRecord::RecordNotFound if @product.stores.empty? || @product.stores.exclude?(current_store)
+ raise ActiveRecord::RecordNotFound if @product.stores.exclude?(current_store)
end
def load_variants
diff --git a/frontend/spec/features/address_spec.rb b/frontend/spec/features/address_spec.rb
index 52e18107f6..e7ad9491ac 100644
--- a/frontend/spec/features/address_spec.rb
+++ b/frontend/spec/features/address_spec.rb
@@ -3,7 +3,8 @@ require 'spec_helper'
describe 'Address', type: :feature, inaccessible: true do
stub_authorization!
- let!(:mug) { create(:product, name: 'RoR Mug') }
+ let!(:mug) { create(:product, name: 'RoR Mug', stores: [store]) }
+ let(:store) { create(:store, default: true) }
before do
create(:order_with_totals, state: 'cart')
@@ -17,7 +18,6 @@ describe 'Address', type: :feature, inaccessible: true do
end
context 'store checkout_zone', js: true do
- let!(:store) { create(:store, default: true) }
let!(:asia_zone) do
hk = create(:country, name: 'Hong Kong', iso_name: 'HK')
create(:zone, name: 'Asia', kind: 'country', default_tax: true).tap do |zone|
diff --git a/frontend/spec/features/cart_spec.rb b/frontend/spec/features/cart_spec.rb
index b024681911..0db1588074 100644
--- a/frontend/spec/features/cart_spec.rb
+++ b/frontend/spec/features/cart_spec.rb
@@ -120,10 +120,10 @@ describe 'Cart', type: :feature, inaccessible: true, js: true do
end
context 'switching currency' do
- let!(:product) { create(:product) }
+ let!(:product) { create(:product, stores: [store]) }
+ let(:store) { create(:store, default: true, supported_currencies: 'USD,EUR,GBP') }
before do
- create(:store, default: true, supported_currencies: 'USD,EUR,GBP')
create(:price, variant: product.master, currency: 'EUR', amount: 16.00)
create(:price, variant: product.master, currency: 'GBP', amount: 23.00)
add_to_cart(product)