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.

Fix taxons selection in featured taxon section (#12353)

Damian Kaczmarczyk2d09a130ef2a
diff --git a/admin/app/controllers/spree/admin/taxons_controller.rb b/admin/app/controllers/spree/admin/taxons_controller.rb
index 3f8103afad..9f3b53feee 100644
--- a/admin/app/controllers/spree/admin/taxons_controller.rb
+++ b/admin/app/controllers/spree/admin/taxons_controller.rb
@@ -16,7 +16,7 @@ module Spree
before_action :load_form_data, only: [:show, :new, :create, :edit, :update]
def select_options
- render json: taxons_options_json_array
+ render json: taxons_options_json_array(with_automatic: params[:with_automatic].to_b)
end
def reposition
diff --git a/admin/app/helpers/spree/admin/taxons_helper.rb b/admin/app/helpers/spree/admin/taxons_helper.rb
index 7ab5f87ed3..90262a07fc 100644
--- a/admin/app/helpers/spree/admin/taxons_helper.rb
+++ b/admin/app/helpers/spree/admin/taxons_helper.rb
@@ -1,12 +1,17 @@
module Spree
module Admin
module TaxonsHelper
- def taxons_scope
- @taxons_scope ||= current_store.taxons.where(automatic: false).where.not(parent_id: nil)
+ def taxons_scope(with_automatic: false)
+ @memoized_taxons_scope ||= {}
+ @memoized_taxons_scope[with_automatic] ||= begin
+ scope = current_store.taxons.where.not(parent_id: nil)
+ scope = scope.where(automatic: false) unless with_automatic
+ scope
+ end
end
- def taxons_options_json_array
- taxons_scope.pluck(:id, :pretty_name).map { |id, pretty_name| { id: id, name: pretty_name } }.as_json
+ def taxons_options_json_array(with_automatic: false)
+ taxons_scope(with_automatic: with_automatic).pluck(:id, :pretty_name).map { |id, pretty_name| { id: id, name: pretty_name } }.as_json
end
def taxon_sort_options_for_select
diff --git a/admin/app/views/spree/admin/page_links/_linkable_type_dropdown.html.erb b/admin/app/views/spree/admin/page_links/_linkable_type_dropdown.html.erb
index 187f3697cc..8708fe1286 100644
--- a/admin/app/views/spree/admin/page_links/_linkable_type_dropdown.html.erb
+++ b/admin/app/views/spree/admin/page_links/_linkable_type_dropdown.html.erb
@@ -1,7 +1,7 @@
<% if page_link.linkable_type == 'Spree::Product' %>
<%= tom_select_tag "#{form_name}[linkable_id]", class: 'w-100', url: spree.select_options_admin_products_path(format: :json), active_option: page_link.linkable_id, select_data: { action: 'auto-submit#submit' } %>
<% elsif page_link.linkable_type == 'Spree::Taxon' %>
- <%= tom_select_tag "#{form_name}[linkable_id]", class: 'w-100', url: spree.admin_taxons_select_options_path(format: :json), active_option: page_link.linkable_id, select_data: { action: 'auto-submit#submit' } %>
+ <%= tom_select_tag "#{form_name}[linkable_id]", class: 'w-100', url: spree.admin_taxons_select_options_path(format: :json, with_automatic: true), active_option: page_link.linkable_id, select_data: { action: 'auto-submit#submit' } %>
<% elsif page_link.linkable_type == 'Spree::Post' %>
<%= tom_select_tag "#{form_name}[linkable_id]", class: 'w-100', url: spree.select_options_admin_posts_path(format: :json), active_option: page_link.linkable_id, select_data: { action: 'auto-submit#submit' } %>
<% elsif page_link.linkable_type == 'Spree::Page' %>
diff --git a/admin/app/views/spree/admin/page_sections/forms/_featured_taxon.html.erb b/admin/app/views/spree/admin/page_sections/forms/_featured_taxon.html.erb
index eced4ced5a..f24e748c6a 100644
--- a/admin/app/views/spree/admin/page_sections/forms/_featured_taxon.html.erb
+++ b/admin/app/views/spree/admin/page_sections/forms/_featured_taxon.html.erb
@@ -35,7 +35,7 @@
</div>
<div class="form-group">
<%= f.label :preferred_taxon, Spree.t('admin.page_builder.category') %>
- <%= tom_select_tag 'page_section[preferred_taxon_id]', active_option: @page_section.preferred_taxon_id, class: 'w-100', url: spree.admin_taxons_select_options_path(format: :json), select_data: {action: 'auto-submit#submit'} %>
+ <%= tom_select_tag 'page_section[preferred_taxon_id]', active_option: @page_section.preferred_taxon_id, class: 'w-100', url: spree.admin_taxons_select_options_path(format: :json, with_automatic: true), select_data: {action: 'auto-submit#submit'} %>
</div>
<div class="form-group">
<%= f.check_box :preferred_show_more_button, class: "checkbox-input", data: { action: 'auto-submit#submit' } %>
diff --git a/admin/spec/controllers/spree/admin/taxons_controller_spec.rb b/admin/spec/controllers/spree/admin/taxons_controller_spec.rb
index af9e595e54..2bf8f3b9ca 100644
--- a/admin/spec/controllers/spree/admin/taxons_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/taxons_controller_spec.rb
@@ -44,4 +44,33 @@ RSpec.describe Spree::Admin::TaxonsController, type: :controller do
expect { delete :destroy, params: { taxonomy_id: taxonomy.id, id: taxon.id }, format: :turbo_stream }.to change(Spree::Taxon, :count).by(-1)
end
end
+
+ describe 'GET #select_options' do
+ before do
+ Spree::Taxon.destroy_all
+ end
+
+ context 'with automatic taxons param' do
+ let!(:automatic_taxon) { create(:taxon, taxonomy: taxonomy, name: 'Automatic Taxon', automatic: true) }
+ let!(:manual_taxon) { create(:taxon, taxonomy: taxonomy, name: 'Manual Taxon', automatic: false) }
+
+ it 'returns all taxons' do
+ get :select_options, params: { with_automatic: true }
+ expect(JSON.parse(response.body)).to contain_exactly(
+ { 'id' => automatic_taxon.id, 'name' => automatic_taxon.pretty_name },
+ { 'id' => manual_taxon.id, 'name' => manual_taxon.pretty_name }
+ )
+ end
+ end
+
+ context 'without automatic taxons param' do
+ let!(:automatic_taxon) { create(:taxon, taxonomy: taxonomy, name: 'Automatic Taxon', automatic: true) }
+ let!(:manual_taxon) { create(:taxon, taxonomy: taxonomy, name: 'Manual Taxon', automatic: false) }
+
+ it 'returns only manual taxons' do
+ get :select_options
+ expect(JSON.parse(response.body)).to eq([{ 'id' => manual_taxon.id, 'name' => manual_taxon.pretty_name }])
+ end
+ end
+ end
end