Merge pull request #12449 from chahmedejaz/feature/11058-change-products-order-by-name

Feature/11058 change products order by name
This commit is contained in:
Rachel Arnould
2024-05-13 15:29:33 +02:00
committed by GitHub
5 changed files with 33 additions and 8 deletions

View File

@@ -48,6 +48,7 @@ module Admin
# prority is given to element dataset (if present) over url params
@page = params[:page].presence || 1
@per_page = params[:per_page].presence || 15
@q = params.permit(q: {})[:q] || { s: 'name asc' }
end
def producers
@@ -89,6 +90,8 @@ module Admin
query.merge!(Spree::Variant::SEARCH_KEY => @search_term)
end
query.merge!(variants_primary_taxon_id_in: @category_id) if @category_id.present?
query.merge!(@q) if @q
query
end

View File

@@ -1,6 +1,7 @@
= form_with url: admin_products_path, id: "filters", method: :get, data: { "search-target": "form", 'turbo-frame': "_self" } do
= hidden_field_tag :page, nil, class: "page"
= hidden_field_tag :per_page, nil, class: "per-page"
= hidden_field_tag '[q][s]', params.dig(:q, :s) || 'name asc', class: 'sort', 'data-default': 'name asc'
.query
.search-input

View File

@@ -49,7 +49,8 @@
= form.submit t('.save'), class: "medium"
%tr
%th.align-left= # image
%th.align-left.with-input= t('admin.products_page.columns.name')
= render partial: 'spree/admin/shared/stimulus_sortable_header',
locals: { column: :name, sorted: params.dig(:q, :s), default: 'name asc' }
%th.align-left.with-input= t('admin.products_page.columns.sku')
%th.align-left.with-input= t('admin.products_page.columns.unit_scale')
%th.align-left.with-input= t('admin.products_page.columns.unit')

View File

@@ -1,5 +1,5 @@
%th
%a{ "data-action": "click->search#changeSorting", "data-column": "#{column}", "data-current": sorted.to_s }
%a{ "data-controller": "search", "data-action": "click->search#changeSorting", "data-column": "#{column}", "data-current": (sorted || default).to_s }
= t("spree.admin.shared.sortable_header.#{column.to_s}")
- if sorted == "#{column} asc" || sorted.blank? && local_assigns[:default] == "#{column} asc"

View File

@@ -26,18 +26,34 @@ RSpec.describe 'As an enterprise user, I can manage my products', feature: :admi
describe "sorting" do
let!(:product_b) { create(:simple_product, name: "Bananas") }
let!(:product_a) { create(:simple_product, name: "Apples") }
let(:products_table) { "table.products" }
before do
visit admin_products_url
end
it "Should sort products alphabetically by default" do
within "table.products" do
# Gather input values, because page.content doesn't include them.
input_content = page.find_all('input[type=text]').map(&:value).join
it "Should sort products alphabetically by default in ascending order" do
within products_table do
# Products are in correct order.
expect(input_content).to match /Apples.*Bananas/
expect(all_input_values).to match /Apples.*Bananas/
end
end
context "when clicked on 'Name' column header" do
it "Should sort products alphabetically in descending/ascending order" do
within products_table do
name_header = page.find('th > a[data-column="name"]')
# Sort in descending order
name_header.click
expect(page).to have_content("Name ▼") # this indicates the re-sorted content has loaded
expect(all_input_values).to match /Bananas.*Apples/
# Sort in ascending order
name_header.click
expect(page).to have_content("Name ▲") # this indicates the re-sorted content has loaded
expect(all_input_values).to match /Apples.*Bananas/
end
end
end
end
@@ -1176,4 +1192,8 @@ RSpec.describe 'As an enterprise user, I can manage my products', feature: :admi
Spree::TaxCategory
.pluck(:name).sample
end
def all_input_values
page.find_all('input[type=text]').map(&:value).join
end
end