Polymorphically associate SemanticLinks to variant

This commit is contained in:
Maikel Linke
2024-10-30 14:38:24 +11:00
parent 48e8ad3dd0
commit c07ec6cdfd
4 changed files with 36 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
# Link a Spree::Variant to an external DFC SuppliedProduct.
class SemanticLink < ApplicationRecord
belongs_to :subject, polymorphic: true
belongs_to :variant, class_name: "Spree::Variant"
validates :semantic_id, presence: true

View File

@@ -0,0 +1,11 @@
# frozen_string_literal: true
class CopySubjectOnSemanticLinks < ActiveRecord::Migration[7.0]
def up
execute <<~SQL.squish
UPDATE semantic_links SET
subject_id = variant_id,
subject_type = 'Spree::Variant'
SQL
end
end

View File

@@ -0,0 +1,23 @@
# frozen_string_literal: true
require 'spec_helper'
require_relative '../../db/migrate/20241030025540_copy_subject_on_semantic_links'
RSpec.describe CopySubjectOnSemanticLinks do
describe "#up" do
let(:original_variant) { create(:variant) }
let(:dummy_variant) { create(:variant) }
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",
)
expect { subject.up }.to change {
link.reload.subject
}.from(dummy_variant).to(original_variant)
end
end
end

View File

@@ -3,6 +3,7 @@
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