From 1ce0b25bb047e1f59abc25e3fecfe59c5c5cf48b Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 30 Oct 2024 14:53:03 +1100 Subject: [PATCH] Switch SemanticLink to use new association And ActiveRecord magic does the rest when used correctly. --- app/jobs/backorder_job.rb | 2 +- app/jobs/stock_sync_job.rb | 2 +- app/models/semantic_link.rb | 3 ++- app/models/spree/variant.rb | 2 +- .../20241030033956_change_null_on_semantic_links.rb | 8 ++++++++ db/schema.rb | 8 ++++---- .../20241030025540_copy_subject_on_semantic_links_spec.rb | 2 +- spec/models/semantic_link_spec.rb | 1 - 8 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20241030033956_change_null_on_semantic_links.rb diff --git a/app/jobs/backorder_job.rb b/app/jobs/backorder_job.rb index 73e3f51eba..ef696056df 100644 --- a/app/jobs/backorder_job.rb +++ b/app/jobs/backorder_job.rb @@ -13,7 +13,7 @@ class BackorderJob < ApplicationJob sidekiq_options retry: 0 def self.check_stock(order) - links = SemanticLink.where(variant_id: order.line_items.select(:variant_id)) + links = SemanticLink.where(subject: order.variants) perform_later(order) if links.exists? rescue StandardError => e diff --git a/app/jobs/stock_sync_job.rb b/app/jobs/stock_sync_job.rb index 009bdb5b5e..cea16a8d14 100644 --- a/app/jobs/stock_sync_job.rb +++ b/app/jobs/stock_sync_job.rb @@ -36,7 +36,7 @@ class StockSyncJob < ApplicationJob def self.catalog_ids(order) stock_controlled_variants = order.variants.reject(&:on_demand) - links = SemanticLink.where(variant_id: stock_controlled_variants.map(&:id)) + links = SemanticLink.where(subject: stock_controlled_variants) semantic_ids = links.pluck(:semantic_id) semantic_ids.map do |product_id| FdcUrlBuilder.new(product_id).catalog_url diff --git a/app/models/semantic_link.rb b/app/models/semantic_link.rb index 86a6e821e6..33cb42d383 100644 --- a/app/models/semantic_link.rb +++ b/app/models/semantic_link.rb @@ -2,8 +2,9 @@ # Link a Spree::Variant to an external DFC SuppliedProduct. class SemanticLink < ApplicationRecord + self.ignored_columns += [:variant_id] + belongs_to :subject, polymorphic: true - belongs_to :variant, class_name: "Spree::Variant" validates :semantic_id, presence: true end diff --git a/app/models/spree/variant.rb b/app/models/spree/variant.rb index fefd41ac0d..72a180c7ed 100644 --- a/app/models/spree/variant.rb +++ b/app/models/spree/variant.rb @@ -60,7 +60,7 @@ module Spree has_many :exchanges, through: :exchange_variants has_many :variant_overrides, dependent: :destroy has_many :inventory_items, dependent: :destroy - has_many :semantic_links, dependent: :delete_all + has_many :semantic_links, as: :subject, dependent: :delete_all has_many :supplier_properties, through: :supplier, source: :properties localize_number :price, :weight diff --git a/db/migrate/20241030033956_change_null_on_semantic_links.rb b/db/migrate/20241030033956_change_null_on_semantic_links.rb new file mode 100644 index 0000000000..bfd8a4c908 --- /dev/null +++ b/db/migrate/20241030033956_change_null_on_semantic_links.rb @@ -0,0 +1,8 @@ +class ChangeNullOnSemanticLinks < ActiveRecord::Migration[7.0] + def change + change_column_null :semantic_links, :subject_id, false + change_column_null :semantic_links, :subject_type, false + + change_column_null :semantic_links, :variant_id, true + end +end diff --git a/db/schema.rb b/db/schema.rb index d8b2d37159..da955587b8 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: 2024_10_30_023153) do +ActiveRecord::Schema[7.0].define(version: 2024_10_30_033956) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "plpgsql" @@ -401,12 +401,12 @@ ActiveRecord::Schema[7.0].define(version: 2024_10_30_023153) do end create_table "semantic_links", force: :cascade do |t| - t.bigint "variant_id", null: false + t.bigint "variant_id" t.string "semantic_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.string "subject_type" - t.bigint "subject_id" + t.string "subject_type", null: false + t.bigint "subject_id", null: false t.index ["subject_type", "subject_id"], name: "index_semantic_links_on_subject" t.index ["variant_id"], name: "index_semantic_links_on_variant_id" end diff --git a/spec/migrations/20241030025540_copy_subject_on_semantic_links_spec.rb b/spec/migrations/20241030025540_copy_subject_on_semantic_links_spec.rb index 3ec4346757..6ab69e950a 100644 --- a/spec/migrations/20241030025540_copy_subject_on_semantic_links_spec.rb +++ b/spec/migrations/20241030025540_copy_subject_on_semantic_links_spec.rb @@ -10,10 +10,10 @@ RSpec.describe CopySubjectOnSemanticLinks do it "copies the original data" do link = SemanticLink.create!( - variant: original_variant, subject: dummy_variant, # This would be NULL when migration runs. semantic_id: "some-url", ) + SemanticLink.update_all("variant_id = #{original_variant.id}") expect { subject.up }.to change { link.reload.subject diff --git a/spec/models/semantic_link_spec.rb b/spec/models/semantic_link_spec.rb index 3dcac99db5..6d881980ea 100644 --- a/spec/models/semantic_link_spec.rb +++ b/spec/models/semantic_link_spec.rb @@ -4,6 +4,5 @@ require 'spec_helper' RSpec.describe SemanticLink, type: :model do it { is_expected.to belong_to :subject } - it { is_expected.to belong_to :variant } it { is_expected.to validate_presence_of(:semantic_id) } end