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.

Add the ability to filter and search menus in admin UI.

Matthew Kennedy264c8e80a1c3
diff --git a/backend/app/controllers/spree/admin/menus_controller.rb b/backend/app/controllers/spree/admin/menus_controller.rb
index 394e545199..c7223ffebd 100644
--- a/backend/app/controllers/spree/admin/menus_controller.rb
+++ b/backend/app/controllers/spree/admin/menus_controller.rb
@@ -5,6 +5,16 @@ module Spree
private
+ def collection
+ return @collection if @collection.present?
+
+ params[:q] ||= {}
+ @collection = super
+
+ @search = @collection.ransack(params[:q])
+ @collection = @search.result.page(params[:page]).per(params[:per_page])
+ end
+
def location_after_save
spree.edit_admin_menu_path(@menu)
end
diff --git a/backend/app/views/spree/admin/menus/index.html.erb b/backend/app/views/spree/admin/menus/index.html.erb
index f2f975bc2e..fe35c0cdc0 100644
--- a/backend/app/views/spree/admin/menus/index.html.erb
+++ b/backend/app/views/spree/admin/menus/index.html.erb
@@ -6,6 +6,47 @@
<%= button_link_to Spree.t('admin.navigation.add_new_menu'), new_object_url, { class: "btn-success", icon: 'add.svg', id: 'new_menu' } %>
<% end if can?(:create, Spree::Menu) %>
+<% content_for :table_filter do %>
+ <div data-hook="admin_menus_sidebar">
+ <%= search_form_for [:admin, @search] do |f| %>
+ <%- locals = {f: f} %>
+ <div class="row">
+ <div class="col-12 col-lg-6">
+ <div class="form-group">
+ <%= f.label :name_cont, Spree.t('name') %>
+ <%= f.text_field :name_cont, size: 15, class: "form-control js-quick-search-target js-filterable" %>
+ </div>
+ </div>
+ <div class="col-12 col-lg-6">
+ <div class="form-group">
+ <%= label_tag :q_location_eq, Spree.t(:location) %>
+ <%= f.select :location_eq, menu_location_formatted_for_select, { include_blank: true }, class: 'select2-clear js-filterable' %>
+ </div>
+ </div>
+ </div>
+
+ <div class="row">
+ <div class="col-12 col-lg-6">
+ <div class="form-group">
+ <%= label_tag :q_locale_eq, Spree.t(:language) %>
+ <%= f.select :locale_eq, all_locales_options, { include_blank: true }, class: 'select2-clear js-filterable' %>
+ </div>
+ </div>
+ <div class="col-12 col-lg-6">
+ <div class="form-group">
+ <%= label_tag :q_store_id_in, Spree.t(:store) %>
+ <%= f.select :store_id_in, @stores.pluck(:code, :id), { include_blank: true }, class: 'select2-clear js-filterable' %>
+ </div>
+ </div>
+ </div>
+
+ <div data-hook="admin_menus_index_search_buttons" class="form-actions">
+ <%= button Spree.t(:search), 'search.svg' %>
+ </div>
+ <% end %>
+ </div>
+<% end %>
+
<% if @menus.any? %>
<div class="table-responsive">
<table class="table" id="menusTable">
diff --git a/backend/spec/features/admin/menus/index_menu_spec.rb b/backend/spec/features/admin/menus/index_menu_spec.rb
index 3f7179aff2..563c9f78db 100644
--- a/backend/spec/features/admin/menus/index_menu_spec.rb
+++ b/backend/spec/features/admin/menus/index_menu_spec.rb
@@ -14,30 +14,37 @@ describe 'Menus Index', type: :feature do
end
context 'when menus are present' do
- let!(:store_1) { create(:store) }
- let!(:store_2) { create(:store) }
- let!(:store_3) { create(:store) }
+ let!(:store_1) { create(:store, name: 'AbCd') }
+ let!(:store_2) { create(:store, name: 'EfGh') }
+ let!(:store_3) { create(:store, name: 'IjKl') }
+ let!(:store_4) { create(:store, name: 'MnOp') }
let!(:main_menu) { create(:menu, name: 'Main Menu', store_id: store_1.id) }
- let!(:footer_menu) { create(:menu, name: 'Footer Menu', store_id: store_3.id) }
+ let!(:main_menu_fr) { create(:menu, name: 'Main Menu FR', store_id: store_1.id, locale: 'fr') }
+
+ let!(:main_menu_a) { create(:menu, name: 'MA', store_id: store_2.id) }
+ let!(:main_menu_b) { create(:menu, name: 'MB', store_id: store_3.id) }
+
+ let!(:footer_menu) { create(:menu, name: 'Footer Menu', location: 'footer', store_id: store_3.id) }
+ let!(:footer_menu_a) { create(:menu, name: 'FA', location: 'footer', store_id: store_1.id) }
+ let!(:footer_menu_b) { create(:menu, name: 'FB', location: 'footer', store_id: store_2.id) }
before do
visit spree.admin_menus_path
end
- it 'lists all of the menus in the table' do
- within_table('menusTable') do
- expect(page).to have_text 'Main Menu'
- expect(page).to have_text 'Footer Menu'
- end
- end
-
it 'lists each menu with its store ' do
within_table('menusTable') do
expect(page).to have_text 'Main Menu'
expect(page).to have_text 'Footer Menu'
+ expect(page).to have_text 'MA'
+ expect(page).to have_text 'MB'
+ expect(page).to have_text 'FA'
+ expect(page).to have_text 'FB'
expect(page).to have_text store_1.unique_name
- expect(page).not_to have_text store_2.unique_name
+ expect(page).to have_text store_2.unique_name
+ expect(page).to have_text store_3.unique_name
+ expect(page).not_to have_text store_4.unique_name
end
end
@@ -48,6 +55,50 @@ describe 'Menus Index', type: :feature do
it 'has no missing translations' do
expect(page).not_to have_css('.translation_missing', visible: :all)
end
+
+ describe 'when filtering menus', js: true do
+ it 'is able to filter by name' do
+ click_on 'Filter'
+ fill_in 'Name', with: 'Main Menu FR'
+ click_on 'Search'
+
+ expect(page).to have_text 'Main Menu FR'
+ expect(page).not_to have_text 'Footer Menu'
+ end
+
+ it 'is able to filter by language' do
+ click_on 'Filter'
+ select2 'fr', from: 'Language'
+ click_on 'Search'
+
+ expect(page).to have_text 'Main Menu FR'
+ expect(page).not_to have_text 'Footer Menu'
+ end
+
+ it 'is able to filter by menu location' do
+ click_on 'Filter'
+ select2 'Footer', from: 'Location'
+ click_on 'Search'
+
+ expect(page).to have_text 'Footer Menu'
+ expect(page).to have_text 'FA'
+ expect(page).to have_text 'FB'
+ expect(page).not_to have_text 'Main Menu'
+ expect(page).not_to have_text 'MA'
+ expect(page).not_to have_text 'MB'
+ end
+
+ it 'is able to filter by store' do
+ click_on 'Filter'
+ select2 store_2.code, from: 'Store'
+ click_on 'Search'
+
+ expect(page).to have_text 'MA'
+ expect(page).to have_text 'FB'
+ expect(page).not_to have_text 'Main Menu'
+ expect(page).not_to have_text 'MB'
+ end
+ end
end
context 'when a user clicks Add New Menu' do
diff --git a/core/app/models/spree/menu.rb b/core/app/models/spree/menu.rb
index 8ac944df79..754a419333 100644
--- a/core/app/models/spree/menu.rb
+++ b/core/app/models/spree/menu.rb
@@ -21,6 +21,8 @@ module Spree
has_one :root, -> { where(parent_id: nil) }, class_name: 'Spree::MenuItem', dependent: :destroy
+ self.whitelisted_ransackable_attributes = %w[name location locale store_id]
+
MENU_LOCATIONS_PARAMETERIZED.each do |name|
define_singleton_method("for_#{name}") do |locale|
menu = find_by(location: name, locale: locale.to_s) || find_by(location: name)
@@ -33,6 +35,8 @@ module Spree
private
def paremeterize_location
+ return unless location.present?
+
self.location = location.parameterize(separator: '_')
end