Add SemanticLink model for variants

We want to link variants/products to external DFC SuppliedProducts to
trigger supplier orders when local stock is exhausted. This is the first
step to enable the link.
This commit is contained in:
Maikel Linke
2024-01-05 16:17:37 +11:00
parent 2e101c5fe6
commit d47d3eba8f
6 changed files with 40 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
# frozen_string_literal: true
# Link a Spree::Variant to an external DFC SuppliedProduct.
class SemanticLink < ApplicationRecord
belongs_to :variant, class_name: "Spree::Variant"
validates :semantic_id, presence: true
end

View File

@@ -56,6 +56,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
localize_number :price, :weight

View File

@@ -0,0 +1,12 @@
# frozen_string_literal: true
class CreateSemanticLinks < ActiveRecord::Migration[7.0]
def change
create_table :semantic_links do |t|
t.references :variant, null: false, foreign_key: { to_table: :spree_variants }
t.string :semantic_id, null: false
t.timestamps
end
end
end

View File

@@ -400,6 +400,14 @@ ActiveRecord::Schema[7.0].define(version: 2024_02_13_044159) do
t.datetime "updated_at", precision: nil, null: false
end
create_table "semantic_links", force: :cascade do |t|
t.bigint "variant_id", null: false
t.string "semantic_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["variant_id"], name: "index_semantic_links_on_variant_id"
end
create_table "sessions", id: :serial, force: :cascade do |t|
t.string "session_id", limit: 255, null: false
t.text "data"
@@ -1168,6 +1176,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_02_13_044159) do
add_foreign_key "proxy_orders", "spree_orders", column: "order_id", name: "order_id_fk"
add_foreign_key "proxy_orders", "subscriptions", name: "proxy_orders_subscription_id_fk"
add_foreign_key "report_rendering_options", "spree_users", column: "user_id"
add_foreign_key "semantic_links", "spree_variants", column: "variant_id"
add_foreign_key "spree_addresses", "spree_countries", column: "country_id", name: "spree_addresses_country_id_fk"
add_foreign_key "spree_addresses", "spree_states", column: "state_id", name: "spree_addresses_state_id_fk"
add_foreign_key "spree_inventory_units", "spree_orders", column: "order_id", name: "spree_inventory_units_order_id_fk", on_delete: :cascade

View File

@@ -0,0 +1,8 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe SemanticLink, type: :model do
it { is_expected.to belong_to :variant }
it { is_expected.to validate_presence_of(:semantic_id) }
end

View File

@@ -6,6 +6,8 @@ require 'spree/localized_number'
describe Spree::Variant do
subject(:variant) { build(:variant) }
it { is_expected.to have_many :semantic_links }
context "validations" do
it "should validate price is greater than 0" do
variant.price = -1