Set owner enterprise when creating source variant

This commit is contained in:
David Cook
2026-03-03 16:59:56 +11:00
parent 8955ffe126
commit 5757f086ec
5 changed files with 30 additions and 6 deletions

View File

@@ -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!

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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