Skip to content

Commit f9549bb

Browse files
committed
Set owner enterprise when creating source variant
1 parent 448d27b commit f9549bb

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

app/controllers/admin/products_v3_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ def create_sourced_variant
120120
variant.price = source_variant.price
121121
variant.save!
122122
variant.source_variants << source_variant
123+
# Owner is my enterprise which has permission to create sourced variants from that supplier
124+
# todo: test if it works! request spec, system spec?
125+
variant.owner_id = EnterpriseRelationship.permitted_by(source_variant.supplier).permitting(spree_current_user.enterprises).with_permission(:create_sourced_variants).pluck(:child_id).first
123126
variant.on_demand = source_variant.on_demand
124127
variant.on_hand = source_variant.on_hand
125128
variant.save!

app/views/admin/products_v3/_variant_row.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
%td.col-image
44
-# empty
55
- variant.source_variants.each do |source_variant|
6-
= content_tag(:span, "🔗", title: t('admin.products_page.variant_row.sourced_from', source_name: source_variant.name, source_id: source_variant.id))
6+
= 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))
77
%td.col-name.field.naked_inputs
88
= f.hidden_field :id
99
= f.text_field :display_name, 'aria-label': t('admin.products_page.columns.name'), placeholder: variant.product.name

config/locales/en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ en:
717717
image:
718718
edit: Edit
719719
variant_row:
720-
sourced_from: "Sourced from: %{source_name} (%{source_id})"
720+
sourced_from: "Sourced from: %{source_name} (%{source_id}); Owned by: %{owner_name}"
721721
product_preview:
722722
product_preview: Product preview
723723
shop_tab: Shop

spec/requests/admin/products_v3_spec.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
permissions_list: [:create_sourced_variants])
8989
}
9090

91-
it "creates a clone of the variant, retaining link as source" do
91+
it "clones the variant, retaining link as source" do
9292
params = { variant_id: variant.id, product_index: 1 }
9393

9494
expect {
@@ -104,6 +104,26 @@
104104
# The new variant's source is the original
105105
expect(new_variant.source_variants.first).to eq variant
106106
end
107+
108+
context "and I'm also owner of another enterprise" do
109+
let!(:enterprise2) { create(:enterprise) }
110+
let(:user) { create(:user, enterprises: [enterprise, enterprise2]) }
111+
112+
it "clones the variant, owned by my enterprise that has permission" do
113+
enterprise2.owner = user
114+
params = { variant_id: variant.id, product_index: 1 }
115+
116+
expect {
117+
post(admin_create_sourced_variant_path, as: :turbo_stream, params:)
118+
119+
expect(response).to have_http_status(:ok)
120+
}.to change { variant.product.variants.count }.by(1)
121+
122+
# The new variant is owned by my enterprise that has permission, not the other one
123+
new_variant = variant.product.variants.last
124+
expect(new_variant.owner).to eq enterprise
125+
end
126+
end
107127
end
108128
end
109129
end

spec/system/admin/products_v3/index_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
include AuthenticationHelper
99
include FileHelper
1010

11-
let(:producer) { create(:supplier_enterprise) }
11+
let(:producer) { create(:supplier_enterprise, name: "My Enterprise") }
1212
let(:user) { create(:user, enterprises: [producer]) }
1313

1414
before do
@@ -146,7 +146,8 @@
146146

147147
let!(:v3_source) { p3.variants.first }
148148
let!(:v3_sourced) {
149-
create(:variant, display_name: "Variant3-sourced", product: p3, supplier: source_producer)
149+
create(:variant, display_name: "Variant3-sourced", product: p3, supplier: source_producer,
150+
owner: producer)
150151
}
151152
let!(:enterprise_relationship) {
152153
# Other producer grants me access to manage their variant
@@ -161,7 +162,8 @@
161162

162163
it "shows sourced variant with indicator" do
163164
within row_containing_name("Variant3-sourced") do
164-
expect(page).to have_selector 'span[title*="Sourced from"]'
165+
expect(page).to have_selector 'span[title*="Sourced from: "]'
166+
expect(page).to have_selector 'span[title*="Owned by: My Enterprise"]'
165167
end
166168
end
167169
end

0 commit comments

Comments
 (0)