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.

Fixed admin product variants matrix form with prefix IDs

Damian Legawiecc2d86f67be8f
diff --git a/admin/app/controllers/spree/admin/products_controller.rb b/admin/app/controllers/spree/admin/products_controller.rb
index c262ed4614..9e40a640d6 100644
--- a/admin/app/controllers/spree/admin/products_controller.rb
+++ b/admin/app/controllers/spree/admin/products_controller.rb
@@ -140,13 +140,13 @@ module Spree
uniq.group_by(&:option_type).each_with_index do |option, index|
option_type, option_values = option
- @product_options[option_type.id.to_s] = {
+ @product_options[option_type.prefix_id] = {
name: option_type.presentation,
position: index + 1,
values: option_values.map { |ov| { value: ov.name, text: ov.presentation } }.uniq
}
- @product_available_options[option_type.id.to_s] = option_type.option_values.map { |ov| { id: ov.name, name: ov.presentation } }.uniq
+ @product_available_options[option_type.prefix_id] = option_type.option_values.map { |ov| { id: ov.name, name: ov.presentation } }.uniq
end
@product_stock = {}
diff --git a/admin/app/views/spree/admin/products/form/_variants.html.erb b/admin/app/views/spree/admin/products/form/_variants.html.erb
index 8d36d5e7ab..20c6d67a24 100644
--- a/admin/app/views/spree/admin/products/form/_variants.html.erb
+++ b/admin/app/views/spree/admin/products/form/_variants.html.erb
@@ -3,7 +3,7 @@
<div class="card-header">
<h5 class="card-title"><%= Spree.t(:variants) %></h5>
</div>
- <% option_types_for_select = @option_types.pluck(:presentation, :id) %>
+ <% option_types_for_select = @option_types.pluck(:presentation, :prefix_id) %>
<div class="card-body p-0">
<%# Build currency format info for JavaScript %>
<% currency_formats = supported_currencies.each_with_object({}) do |currency, hash|
@@ -38,7 +38,7 @@
<div data-variants-form-target="optionsContainer" class="options-creator__options_container">
<% @product.product_option_types.includes(:option_type).reorder('spree_product_option_types.position').each do |product_option_type| %>
<% option_type = product_option_type.option_type %>
- <div class="options-creator__option" data-variants-form-target="option" id="option-<%= option_type.id %>">
+ <div class="options-creator__option" data-variants-form-target="option" id="option-<%= option_type.prefix_id %>">
<% if can?(:manage_option_types, @product) %>
<button class="draggable" type="button">
<i class="ti ti-grip-vertical"></i>
@@ -48,7 +48,7 @@
<div class="flex justify-between items-center">
<h6 data-slot="optionName"><%= option_type.presentation %></h6>
<% if can?(:manage_option_types, @product) %>
- <button class="btn btn-light btn-sm" type="button" data-action="variants-form#editOption" data-variants-form-option-id-param="<%= option_type.id %>"><%= Spree.t(:edit) %></button>
+ <button class="btn btn-light btn-sm" type="button" data-action="variants-form#editOption" data-variants-form-option-id-param="<%= option_type.prefix_id %>"><%= Spree.t(:edit) %></button>
<% end %>
</div>
<div data-slot="optionValuesContainer">
diff --git a/admin/lib/spree/admin/testing_support/tom_select.rb b/admin/lib/spree/admin/testing_support/tom_select.rb
index 25efa36f81..cad51aedb3 100644
--- a/admin/lib/spree/admin/testing_support/tom_select.rb
+++ b/admin/lib/spree/admin/testing_support/tom_select.rb
@@ -8,7 +8,7 @@ module Spree
if create
first('.ts-dropdown .ts-dropdown-content .create.active').click
else
- first('.ts-dropdown .ts-dropdown-content .option', text: /#{Regexp.quote(value)}/i).click
+ find('.ts-dropdown .ts-dropdown-content .option', text: /#{Regexp.quote(value)}/i, match: :first).click
end
end
end
diff --git a/admin/spec/features/spree/admin/products/products_spec.rb b/admin/spec/features/spree/admin/products/products_spec.rb
index 4797854cf7..c4f7a52a81 100644
--- a/admin/spec/features/spree/admin/products/products_spec.rb
+++ b/admin/spec/features/spree/admin/products/products_spec.rb
@@ -15,10 +15,14 @@ describe 'Products', type: :feature do
end
def select_option_value(option_value_name, index: 0, create: false)
- select_element = all('[data-multi-tom-select-target="select"]')[index]
-
- within(select_element) do
- tom_select(option_value_name, from: 'new_option_values_-ts-control', create: create)
+ select_element = all('[data-multi-tom-select-target="select"]', wait: 5)[index]
+ select_element.find('.ts-wrapper').click
+ select_element.find('input[type="text"]', visible: true).native.send_keys(option_value_name)
+
+ if create
+ find('.ts-dropdown .ts-dropdown-content .create.active', match: :first, wait: 5).click
+ else
+ find('.ts-dropdown .ts-dropdown-content .option', text: /#{Regexp.quote(option_value_name)}/i, match: :first, wait: 10).click
end
end
@@ -270,12 +274,12 @@ describe 'Products', type: :feature do
color_option = Spree::OptionType.find_by(name: 'color')
size_option = Spree::OptionType.find_by(name: 'size')
- within("#option-#{color_option.id}") do
+ within("#option-#{color_option.prefix_id}") do
expect(page).to have_content('Black')
expect(page).to have_content('White')
end
- within("#option-#{size_option.id}") do
+ within("#option-#{size_option.prefix_id}") do
expect(page).to have_content('Small')
expect(page).to have_content('Medium')
expect(page).to have_content('Large')
@@ -351,13 +355,13 @@ describe 'Products', type: :feature do
color_option = Spree::OptionType.find_by(name: 'color')
- within("#option-#{color_option.id}") do
+ within("#option-#{color_option.prefix_id}") do
click_on 'Edit'
select_option_value('Red', index: 2, create: true)
click_on 'Done'
end
- within("#option-#{color_option.id}") do
+ within("#option-#{color_option.prefix_id}") do
expect(page).to have_content('Black')
expect(page).to have_content('White')
expect(page).to have_content('Red')
@@ -575,7 +579,7 @@ describe 'Products', type: :feature do
material_option = Spree::OptionType.find_by(name: 'material')
- within("#option-#{material_option.id}") do
+ within("#option-#{material_option.prefix_id}") do
expect(page).to have_content('Cotton')
expect(page).to have_content('Polyester')
end
@@ -661,13 +665,13 @@ describe 'Products', type: :feature do
it 'allows removing the first option' do
visit spree.edit_admin_product_path(product.reload)
- within("#option-#{color_option.id}") do
+ within("#option-#{color_option.prefix_id}") do
click_on 'Edit'
click_on 'Delete'
end
- expect(page).not_to have_css("#option-#{color_option.id}")
- expect(page).to have_css("#option-#{size_option.id}")
+ expect(page).not_to have_css("#option-#{color_option.prefix_id}")
+ expect(page).to have_css("#option-#{size_option.prefix_id}")
within('[data-test-id="product-variants-table"]') do
expect(page).not_to have_content('Black')
@@ -755,13 +759,13 @@ describe 'Products', type: :feature do
it 'allows removing the last option' do
visit spree.edit_admin_product_path(product.reload)
- within("#option-#{size_option.id}") do
+ within("#option-#{size_option.prefix_id}") do
click_on 'Edit'
click_on 'Delete'
end
- expect(page).to have_css("#option-#{color_option.id}")
- expect(page).not_to have_css("#option-#{size_option.id}")
+ expect(page).to have_css("#option-#{color_option.prefix_id}")
+ expect(page).not_to have_css("#option-#{size_option.prefix_id}")
within('[data-test-id="product-variants-table"]') do
expect(page).to have_content('Black')
@@ -794,18 +798,18 @@ describe 'Products', type: :feature do
it 'allows removing all options' do
visit spree.edit_admin_product_path(product.reload)
- within("#option-#{color_option.id}") do
+ within("#option-#{color_option.prefix_id}") do
click_on 'Edit'
click_on 'Delete'
end
- within("#option-#{size_option.id}") do
+ within("#option-#{size_option.prefix_id}") do
click_on 'Edit'
click_on 'Delete'
end
- expect(page).not_to have_css("#option-#{color_option.id}")
- expect(page).not_to have_css("#option-#{size_option.id}")
+ expect(page).not_to have_css("#option-#{color_option.prefix_id}")
+ expect(page).not_to have_css("#option-#{size_option.prefix_id}")
expect(page).not_to have_css('[data-test-id="product-variants-table"]')
# Update product variants
@@ -819,12 +823,12 @@ describe 'Products', type: :feature do
it 'allows removing all options and adding one back and a new one' do
visit spree.edit_admin_product_path(product.reload)
- within("#option-#{color_option.id}") do
+ within("#option-#{color_option.prefix_id}") do
click_on 'Edit'
click_on 'Delete'
end
- within("#option-#{size_option.id}") do
+ within("#option-#{size_option.prefix_id}") do
click_on 'Edit'
click_on 'Delete'
end
@@ -835,7 +839,7 @@ describe 'Products', type: :feature do
select_option_value('White', index: 0)
click_on 'Done'
- within("#option-#{color_option.id}") do
+ within("#option-#{color_option.prefix_id}") do
expect(page).to have_content('White')
end
@@ -911,7 +915,7 @@ describe 'Products', type: :feature do
click_on 'Delete selected'
- within("#option-#{color_option.id}") do
+ within("#option-#{color_option.prefix_id}") do
expect(page).not_to have_content('Red')
expect(page).to have_content('Black')
expect(page).to have_content('White')
@@ -983,7 +987,7 @@ describe 'Products', type: :feature do
click_on 'Delete selected'
- within("#option-#{color_option.id}") do
+ within("#option-#{color_option.prefix_id}") do
expect(page).not_to have_content('White')
expect(page).to have_content('Black')
end
@@ -1005,12 +1009,12 @@ describe 'Products', type: :feature do
expect(page).to have_content('successfully updated')
within('.alerts-container') { find('button').click }
- within("#option-#{color_option.id}") do
+ within("#option-#{color_option.prefix_id}") do
expect(page).to have_content('Black')
expect(page).not_to have_content('White')
end
- within("#option-#{size_option.id}") do
+ within("#option-#{size_option.prefix_id}") do
expect(page).to have_content('Small')
expect(page).not_to have_content('Medium')
expect(page).not_to have_content('Large')
@@ -1034,7 +1038,7 @@ describe 'Products', type: :feature do
end
end
- within("#option-#{size_option.id}") do
+ within("#option-#{size_option.prefix_id}") do
click_on 'Edit'
select_option_value('Extra Large', index: 3, create: true)
click_on 'Done'
diff --git a/core/app/services/spree/products/prepare_nested_attributes.rb b/core/app/services/spree/products/prepare_nested_attributes.rb
index cc0313b50c..61d7cc2ad7 100644
--- a/core/app/services/spree/products/prepare_nested_attributes.rb
+++ b/core/app/services/spree/products/prepare_nested_attributes.rb
@@ -184,7 +184,7 @@ module Spree
option_value_variant_params = {}
option_value_params.each_with_index do |opt, index|
- option_type = Spree::OptionType.find_by(id: opt[:id]) if opt.fetch(:id)
+ option_type = Spree::OptionType.find_by_param(opt[:id]) if opt.fetch(:id)
option_type ||= Spree::OptionType.where(name: opt[:name].parameterize).first_or_initialize do |o|
o.name = o.presentation = opt[:name]
o.position = opt[:position]