mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Switch SemanticLink to use new association
And ActiveRecord magic does the rest when used correctly.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user