From e65efe0f851aa276753a3162222e573d3213198b Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 9 Aug 2023 18:24:46 +0100 Subject: [PATCH 1/2] Remove unused variant :position attribute --- app/models/spree/variant.rb | 5 ----- db/migrate/20230809172206_remove_variant_position.rb | 5 +++++ db/schema.rb | 3 +-- spec/models/spree/variant_spec.rb | 8 -------- 4 files changed, 6 insertions(+), 15 deletions(-) create mode 100644 db/migrate/20230809172206_remove_variant_position.rb diff --git a/app/models/spree/variant.rb b/app/models/spree/variant.rb index 52d26e67a6..3cbc350098 100644 --- a/app/models/spree/variant.rb +++ b/app/models/spree/variant.rb @@ -86,7 +86,6 @@ module Spree } after_create :create_stock_items - after_create :set_position around_destroy :destruction after_save :save_default_price @@ -231,10 +230,6 @@ module Spree end end - def set_position - update_column(:position, product.variants.maximum(:position).to_i + 1) - end - def update_weight_from_unit_value return unless product.variant_unit == 'weight' && unit_value.present? diff --git a/db/migrate/20230809172206_remove_variant_position.rb b/db/migrate/20230809172206_remove_variant_position.rb new file mode 100644 index 0000000000..2e1b908b81 --- /dev/null +++ b/db/migrate/20230809172206_remove_variant_position.rb @@ -0,0 +1,5 @@ +class RemoveVariantPosition < ActiveRecord::Migration[7.0] + def up + remove_column :spree_variants, :position + end +end diff --git a/db/schema.rb b/db/schema.rb index 0db30fefc1..5ed7ab1911 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_08_07_145022) do +ActiveRecord::Schema[7.0].define(version: 2023_08_09_172206) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "plpgsql" @@ -1035,7 +1035,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_08_07_145022) do t.datetime "deleted_at", precision: nil t.boolean "is_master", default: false t.integer "product_id" - t.integer "position" t.string "cost_currency", limit: 255 t.float "unit_value" t.string "unit_description", limit: 255, default: "" diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index 5dc310d167..f0d0e10473 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -168,14 +168,6 @@ describe Spree::Variant do end end - # Regression test for #2744 - describe "set_position" do - it "sets variant position after creation" do - variant = create(:variant) - expect(variant.position).to_not be_nil - end - end - describe '#in_stock?' do # Stock data can only be stored against a persisted variant. subject(:variant) { create(:variant) } From a33b6e0ec9cb3dbbf85d49e966929d8e1147a4c8 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 9 Aug 2023 18:30:56 +0100 Subject: [PATCH 2/2] Update variant sorting --- app/models/spree/product.rb | 8 +++----- spec/models/spree/product_spec.rb | 6 ++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/app/models/spree/product.rb b/app/models/spree/product.rb index fad2d606dd..606de8ceda 100755 --- a/app/models/spree/product.rb +++ b/app/models/spree/product.rb @@ -39,12 +39,10 @@ module Spree has_many :product_properties, dependent: :destroy has_many :properties, through: :product_properties - has_many :variants, -> { order("spree_variants.position ASC") }, class_name: 'Spree::Variant', - dependent: :destroy + has_many :variants, -> { order("spree_variants.id ASC") }, class_name: 'Spree::Variant', + dependent: :destroy - has_many :prices, -> { - order('spree_variants.position, spree_variants.id, currency') - }, through: :variants + has_many :prices, -> { order('spree_variants.id, currency') }, through: :variants has_many :stock_items, through: :variants has_many :supplier_properties, through: :supplier, source: :properties diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 77ad608b74..dc2be5b509 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -32,10 +32,8 @@ module Spree end describe 'Variants sorting' do - context 'without master variant' do - it 'sorts variants by position' do - expect(product.variants.to_sql).to match(/ORDER BY spree_variants.position ASC/) - end + it 'sorts variants by id' do + expect(product.variants.to_sql).to match(/ORDER BY spree_variants.id ASC/) end end end