Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/11058 change products order by name #12449

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/controllers/admin/products_v3_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def init_pagination_params
# 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
Expand Down Expand Up @@ -89,6 +90,8 @@ def ransack_query
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

Expand Down
1 change: 1 addition & 0 deletions app/views/admin/products_v3/_filters.html.haml
Original file line number Diff line number Diff line change
@@ -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'
dacook marked this conversation as resolved.
Show resolved Hide resolved

.query
.search-input
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/products_v3/_table.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }
Copy link
Collaborator Author

@chahmedejaz chahmedejaz May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to associate the search controller here because it doesn't have to depend upon the parent to use it.

= t("spree.admin.shared.sortable_header.#{column.to_s}")

- if sorted == "#{column} asc" || sorted.blank? && local_assigns[:default] == "#{column} asc"
Expand Down
32 changes: 26 additions & 6 deletions spec/system/admin/products_v3/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,34 @@
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
Expand Down Expand Up @@ -1176,4 +1192,8 @@ def random_tax_category
Spree::TaxCategory
.pluck(:name).sample
end

def all_input_values
page.find_all('input[type=text]').map(&:value).join
end
end
Loading