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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Scope resources in Platform API to the current store
Damian Legawiec83eaf6a1e031
diff --git a/api/app/controllers/spree/api/v2/platform/resource_controller.rb b/api/app/controllers/spree/api/v2/platform/resource_controller.rbindex 3656ea2c1c..46baa4317e 100644--- a/api/app/controllers/spree/api/v2/platform/resource_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/resource_controller.rb@@ -61,7 +61,7 @@ module Spreedef scopereturn super if spree_current_user.present?- model_class.includes(scope_includes)+ super(skip_cancancan: true)end# We're overwriting this method because the original one calls `dookreeper_authorize`diff --git a/api/app/controllers/spree/api/v2/resource_controller.rb b/api/app/controllers/spree/api/v2/resource_controller.rbindex 39b24e7c21..9e6bf0829a 100644--- a/api/app/controllers/spree/api/v2/resource_controller.rb+++ b/api/app/controllers/spree/api/v2/resource_controller.rb@@ -26,7 +26,7 @@ module Spree[:id, :name, :number, :updated_at, :created_at]end- def scope+ def scope(skip_cancancan: false)plural_model_name = model_class.model_name.plural.gsub(/spree_/, '').to_symbase_scope = if current_store.respond_to?(plural_model_name)@@ -35,7 +35,7 @@ module Spreemodel_classend- base_scope = base_scope.accessible_by(current_ability, :show)+ base_scope = base_scope.accessible_by(current_ability, :show) unless skip_cancancanbase_scope = base_scope.includes(scope_includes) if scope_includes.any?base_scopeenddiff --git a/api/spec/controllers/spree/api/v2/platform/resource_controller_spec.rb b/api/spec/controllers/spree/api/v2/platform/resource_controller_spec.rbindex 1c8ee65d3a..69be04dcab 100644--- a/api/spec/controllers/spree/api/v2/platform/resource_controller_spec.rb+++ b/api/spec/controllers/spree/api/v2/platform/resource_controller_spec.rb@@ -4,44 +4,44 @@ class PlatformApiDummyController < Spree::Api::V2::Platform::ResourceControllerprivatedef model_class- Spree::Address- end-- def scope- Spree::Address+ Spree::Productendenddescribe Spree::Api::V2::Platform::ResourceController, type: :controller dolet(:dummy_controller) { PlatformApiDummyController.new }+ let(:store) { Spree::Store.default }describe '#resource_serializer' do- it { expect(dummy_controller.send(:resource_serializer)).to be Spree::Api::V2::Platform::AddressSerializer }+ it { expect(dummy_controller.send(:resource_serializer)).to be Spree::Api::V2::Platform::ProductSerializer }enddescribe '#collection_serializer' do- it { expect(dummy_controller.send(:collection_serializer)).to be Spree::Api::V2::Platform::AddressSerializer }+ it { expect(dummy_controller.send(:collection_serializer)).to be Spree::Api::V2::Platform::ProductSerializer }enddescribe '#collection' do+ let!(:product) { create(:product, stores: [store]) }+ let!(:product_from_another_store) { create(:product, stores: [create(:store)]) }+ let(:collection) { dummy_controller.send(:collection) }+before do- create(:address)dummy_controller.params = {}+ allow(dummy_controller).to receive(:current_store).and_return(store)+ allow(dummy_controller).to receive(:spree_current_user).and_return(nil)end- let(:collection) { dummy_controller.send(:collection) }-- it { expect(collection).to be_instance_of(Spree::Address.const_get(:ActiveRecord_Relation)) }- it { expect(collection.first).to be_instance_of(Spree::Address) }- it { expect(collection.count).to eq(1) }+ it { expect(collection.first).to be_instance_of(Spree::Product) }+ it { expect(collection.count).to eq(store.products.count) }+ it { expect(collection.map(&:id)).to include(product.id) }+ it { expect(collection.map(&:id)).not_to include(product_from_another_store.id) }enddescribe '#permitted_resource_params' dolet(:valid_attributes) do{- address: {- firstname: 'John',- lastname: 'Snow'+ product: {+ name: 'Test'}}end@@ -50,6 +50,6 @@ describe Spree::Api::V2::Platform::ResourceController, type: :controller dodummy_controller.params = valid_attributesend- it { expect(dummy_controller.send(:permitted_resource_params)).to eq(ActionController::Parameters.new(valid_attributes).require(:address).permit!) }+ it { expect(dummy_controller.send(:permitted_resource_params)).to eq(ActionController::Parameters.new(valid_attributes).require(:product).permit!) }endenddiff --git a/api/spec/controllers/spree/api/v2/resource_controller_spec.rb b/api/spec/controllers/spree/api/v2/resource_controller_spec.rbindex 1352c2905c..48c1270300 100644--- a/api/spec/controllers/spree/api/v2/resource_controller_spec.rb+++ b/api/spec/controllers/spree/api/v2/resource_controller_spec.rb@@ -4,7 +4,7 @@ class ApiV2DummyController < Spree::Api::V2::ResourceControllerprivatedef model_class- Spree::Order+ Spree::Productendend@@ -53,4 +53,21 @@ describe Spree::Api::V2::ResourceController, type: :controller doit_behaves_like 'returns proper values'endend++ describe '#collection' do+ let!(:product) { create(:product, stores: [store]) }+ let!(:product_from_another_store) { create(:product, stores: [create(:store)]) }+ let(:collection) { dummy_controller.send(:collection) }++ before do+ dummy_controller.params = {}+ allow(dummy_controller).to receive(:current_store).and_return(store)+ allow(dummy_controller).to receive(:spree_current_user).and_return(nil)+ end++ it { expect(collection.first).to be_instance_of(Spree::Product) }+ it { expect(collection.count).to eq(store.products.count) }+ it { expect(collection.map(&:id)).to include(product.id) }+ it { expect(collection.map(&:id)).not_to include(product_from_another_store.id) }+ endend