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.

SD-1354 Add pagination to the view of product's variants (#11137)

Tomek Urbanbb90a43070ed
diff --git a/backend/app/controllers/spree/admin/variants_controller.rb b/backend/app/controllers/spree/admin/variants_controller.rb
index 823240f848..88a640ded6 100644
--- a/backend/app/controllers/spree/admin/variants_controller.rb
+++ b/backend/app/controllers/spree/admin/variants_controller.rb
@@ -39,15 +39,18 @@ module Spree
end
def collection
- @deleted = params.key?(:deleted) && params[:deleted] == 'on' ? 'checked' : ''
+ return @collection if @collection.present?
- @collection ||=
- if @deleted.blank?
- super.includes(:default_price, option_values: :option_type)
- else
- Variant.only_deleted.where(product_id: parent.id)
- end
- @collection
+ params[:q] ||= {}
+ @deleted = params.dig(:q, :deleted_at_null) || '0'
+
+ @collection = super
+ @collection = @collection.deleted if @deleted == '1'
+ # @search needs to be defined as this is passed to search_link
+ @search = @collection.ransack(params[:q])
+ @collection = @search.result.
+ page(params[:page]).
+ per(params[:per_page] || Spree::Backend::Config[:variants_per_page])
end
private
diff --git a/backend/app/models/spree/backend_configuration.rb b/backend/app/models/spree/backend_configuration.rb
index 4813d41926..258ae1686a 100644
--- a/backend/app/models/spree/backend_configuration.rb
+++ b/backend/app/models/spree/backend_configuration.rb
@@ -1,6 +1,7 @@
module Spree
class BackendConfiguration < Preferences::Configuration
preference :locale, :string, default: Rails.application.config.i18n.default_locale
+ preference :variants_per_page, :integer, default: Kaminari.config.default_per_page
ORDER_TABS ||= [:orders, :payments, :creditcard_payments,
:shipments, :credit_cards, :return_authorizations,
diff --git a/backend/app/views/spree/admin/variants/index.html.erb b/backend/app/views/spree/admin/variants/index.html.erb
index 9a9fff2ff3..f7d1d014ed 100644
--- a/backend/app/views/spree/admin/variants/index.html.erb
+++ b/backend/app/views/spree/admin/variants/index.html.erb
@@ -10,7 +10,7 @@
<tr>
<th colspan="2"><%= Spree.t(:options) %></th>
<th><%= Spree.t(:price) %></th>
- <th><%= Spree.t(:sku) %></th>
+ <th><%= sort_link @search, :sku, Spree.t(:sku) %></th>
<th class="actions"></th>
</tr>
</thead>
@@ -65,6 +65,7 @@
<% content_for :page_actions do %>
<%= product_preview_link(@product) %>
<%= button_link_to(Spree.t(:new_variant), spree.new_admin_product_variant_url(@product), { icon: 'add.svg', :'data-update' => 'new_variant', class: 'btn-success', id: 'new_var_link' }) if can? :create, Spree::Variant %>
- <%= button_link_to (@deleted.blank? ? Spree.t(:show_deleted) : Spree.t(:show_active)), spree.admin_product_variants_url(@product, deleted: @deleted.blank? ? "on" : "off"), { class: 'btn-outline-secondary', icon: 'filter.svg' } %>
+ <%= button_link_to (@deleted == '1' ? Spree.t(:show_active) : Spree.t(:show_deleted)), spree.admin_product_variants_url(@product, { q: { deleted_at_null: @deleted == '1' ? "0" : "1" } }), { class: 'btn-outline-secondary', icon: 'filter.svg' } %>
<% end %>
<% end %>
+<%= render partial: 'spree/admin/shared/index_table_options', locals: { collection: @collection } %>
diff --git a/backend/spec/controllers/spree/admin/variants_controller_spec.rb b/backend/spec/controllers/spree/admin/variants_controller_spec.rb
index 342167a095..c0edb3cf40 100644
--- a/backend/spec/controllers/spree/admin/variants_controller_spec.rb
+++ b/backend/spec/controllers/spree/admin/variants_controller_spec.rb
@@ -22,7 +22,7 @@ module Spree
before { variant_2.destroy }
it 'assigns only deleted variants for a requested product' do
- get :index, params: { product_id: product.slug, deleted: 'on' }
+ get :index, params: { product_id: product.slug, q: { deleted_at_null: '1' } }
expect(assigns(:collection)).not_to include variant_1
expect(assigns(:collection)).to include variant_2
end
diff --git a/backend/spec/features/admin/products/variant_spec.rb b/backend/spec/features/admin/products/variant_spec.rb
index e172e617f9..23566b1f76 100644
--- a/backend/spec/features/admin/products/variant_spec.rb
+++ b/backend/spec/features/admin/products/variant_spec.rb
@@ -1,4 +1,3 @@
-# coding: utf-8
require 'spec_helper'
describe 'Variants', type: :feature do
@@ -27,6 +26,35 @@ describe 'Variants', type: :feature do
end
context 'listing variants' do
+ context "there's fewer than 26 variants" do
+ it 'displays pagination' do
+ visit spree.admin_product_variants_path(product)
+ expect(product.variants.size < 26).to be_truthy
+ expect(page).not_to have_css('.pagination.d-inline-flex')
+ expect(page).to have_css('select[name="per_page"]')
+ end
+ end
+
+ context "there's variants than in preference variants_per_page" do
+ before { create_list(:variant, (default_per_page + 1), product: product) }
+ let(:default_per_page) { Spree::Backend::Config[:variants_per_page] }
+
+ it 'allows to change number of displayed variants', js: true do
+ second_page_link_selector = "a[href='#{spree.admin_product_variants_path(product, page: 2)}']"
+ visit spree.admin_product_variants_path(product)
+ expect(page).to have_css(second_page_link_selector)
+ page.select('50', from: 'per_page')
+ expect(page).to have_current_path(spree.admin_product_variants_path(product, per_page: 50))
+ expect(page).not_to have_css(second_page_link_selector)
+ end
+
+ it 'allows to go to the second page' do
+ visit spree.admin_product_variants_path(product)
+ page.click_link('2')
+ expect(page).to have_current_path(spree.admin_product_variants_path(product, page: 2))
+ end
+ end
+
context 'currency displaying' do
context 'using Russian Rubles' do
before do