From 5757f086ecf843ded46bc4bfeda65bad8397a42a Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 3 Mar 2026 16:59:56 +1100 Subject: [PATCH] Set owner enterprise when creating source variant --- .../admin/products_v3_controller.rb | 2 ++ .../admin/products_v3/_variant_row.html.haml | 2 +- config/locales/en.yml | 2 +- spec/requests/admin/products_v3_spec.rb | 22 ++++++++++++++++++- spec/system/admin/products_v3/index_spec.rb | 8 ++++--- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index 74f108dacc..9dbafeb7ce 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -120,6 +120,8 @@ module Admin variant.price = source_variant.price variant.save! variant.source_variants << source_variant + # Owner is my enterprise which has permission to create sourced variants from that supplier + variant.owner_id = EnterpriseRelationship.permitted_by(source_variant.supplier).permitting(spree_current_user.enterprises).with_permission(:create_sourced_variants).pluck(:child_id).first variant.on_demand = source_variant.on_demand variant.on_hand = source_variant.on_hand variant.save! diff --git a/app/views/admin/products_v3/_variant_row.html.haml b/app/views/admin/products_v3/_variant_row.html.haml index df56c92a2e..1aa5f0649b 100644 --- a/app/views/admin/products_v3/_variant_row.html.haml +++ b/app/views/admin/products_v3/_variant_row.html.haml @@ -4,7 +4,7 @@ %td.col-image -# empty - variant.source_variants.each do |source_variant| - = content_tag(:span, "🔗", title: t('admin.products_page.variant_row.sourced_from', source_name: source_variant.name, source_id: source_variant.id)) + = content_tag(:span, "🔗", title: t('admin.products_page.variant_row.sourced_from', source_name: source_variant.name, source_id: source_variant.id, owner_name: variant.owner&.name)) %td.col-name.field.naked_inputs = f.hidden_field :id = f.text_field :display_name, 'aria-label': t('admin.products_page.columns.name'), placeholder: variant.product.name diff --git a/config/locales/en.yml b/config/locales/en.yml index 37be1f0f74..cfd5f8a4bb 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -721,7 +721,7 @@ en: image: edit: Edit variant_row: - sourced_from: "Sourced from: %{source_name} (%{source_id})" + sourced_from: "Sourced from: %{source_name} (%{source_id}); Owned by: %{owner_name}" product_preview: product_preview: Product preview shop_tab: Shop diff --git a/spec/requests/admin/products_v3_spec.rb b/spec/requests/admin/products_v3_spec.rb index 485273c699..e5fdfac7b7 100644 --- a/spec/requests/admin/products_v3_spec.rb +++ b/spec/requests/admin/products_v3_spec.rb @@ -88,7 +88,7 @@ RSpec.describe "Admin::ProductsV3" do permissions_list: [:create_sourced_variants]) } - it "creates a clone of the variant, retaining link as source" do + it "clones the variant, retaining link as source" do params = { variant_id: variant.id, product_index: 1 } expect { @@ -104,6 +104,26 @@ RSpec.describe "Admin::ProductsV3" do # The new variant's source is the original expect(new_variant.source_variants.first).to eq variant end + + context "and I'm also owner of another enterprise" do + let!(:enterprise2) { create(:enterprise) } + let(:user) { create(:user, enterprises: [enterprise, enterprise2]) } + + it "clones the variant, owned by my enterprise that has permission" do + enterprise2.owner = user + params = { variant_id: variant.id, product_index: 1 } + + expect { + post(admin_create_sourced_variant_path, as: :turbo_stream, params:) + + expect(response).to have_http_status(:ok) + }.to change { variant.product.variants.count }.by(1) + + # The new variant is owned by my enterprise that has permission, not the other one + new_variant = variant.product.variants.last + expect(new_variant.owner).to eq enterprise + end + end end end end diff --git a/spec/system/admin/products_v3/index_spec.rb b/spec/system/admin/products_v3/index_spec.rb index d835b807c3..83fddcf354 100644 --- a/spec/system/admin/products_v3/index_spec.rb +++ b/spec/system/admin/products_v3/index_spec.rb @@ -8,7 +8,7 @@ RSpec.describe 'As an enterprise user, I can browse my products' do include AuthenticationHelper include FileHelper - let(:producer) { create(:supplier_enterprise) } + let(:producer) { create(:supplier_enterprise, name: "My Enterprise") } let(:user) { create(:user, enterprises: [producer]) } before do @@ -146,7 +146,8 @@ RSpec.describe 'As an enterprise user, I can browse my products' do let!(:v3_source) { p3.variants.first } let!(:v3_sourced) { - create(:variant, display_name: "Variant3-sourced", product: p3, supplier: source_producer) + create(:variant, display_name: "Variant3-sourced", product: p3, supplier: source_producer, + owner: producer) } let!(:enterprise_relationship) { # Other producer grants me access to manage their variant @@ -161,7 +162,8 @@ RSpec.describe 'As an enterprise user, I can browse my products' do it "shows sourced variant with indicator" do within row_containing_name("Variant3-sourced") do - expect(page).to have_selector 'span[title*="Sourced from"]' + expect(page).to have_selector 'span[title*="Sourced from: "]' + expect(page).to have_selector 'span[title*="Owned by: My Enterprise"]' end end end