mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-17 04:34:24 +00:00
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:
8
app/models/semantic_link.rb
Normal file
8
app/models/semantic_link.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
|
||||
12
db/migrate/20240105043228_create_semantic_links.rb
Normal file
12
db/migrate/20240105043228_create_semantic_links.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
8
spec/models/semantic_link_spec.rb
Normal file
8
spec/models/semantic_link_spec.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user