mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Polymorphically associate SemanticLinks to variant
This commit is contained in:
@@ -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
|
||||
|
||||
11
db/migrate/20241030025540_copy_subject_on_semantic_links.rb
Normal file
11
db/migrate/20241030025540_copy_subject_on_semantic_links.rb
Normal 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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user