Add Voucher model

A voucher belongs to an enterprise and an enterprise can have many
vouchers
This commit is contained in:
Gaetan Craig-Riou
2023-02-15 16:25:43 +11:00
parent 6cfb86e578
commit a48fd0828c
6 changed files with 50 additions and 0 deletions

View File

@@ -65,6 +65,7 @@ class Enterprise < ApplicationRecord
has_many :inventory_items
has_many :tag_rules
has_one :stripe_account, dependent: :destroy
has_many :vouchers
delegate :latitude, :longitude, :city, :state_name, to: :address

7
app/models/voucher.rb Normal file
View File

@@ -0,0 +1,7 @@
# frozen_string_literal: false
class Voucher < ApplicationRecord
belongs_to :enterprise
validates :code, presence: true, uniqueness: { scope: :enterprise_id }
end

View File

@@ -0,0 +1,12 @@
class CreateVouchers < ActiveRecord::Migration[6.1]
def change
create_table :vouchers do |t|
t.string :code, null: false, limit: 255
t.datetime :expiry_date
t.timestamps
end
add_reference :vouchers, :enterprise, foreign_key: true
add_index :vouchers, [:code, :enterprise_id], unique: true
end
end

View File

@@ -1197,6 +1197,16 @@ ActiveRecord::Schema[7.0].define(version: 2023_02_13_160135) do
t.index ["user_id"], name: "index_webhook_endpoints_on_user_id"
end
create_table "vouchers", force: :cascade do |t|
t.string "code", limit: 255, null: false
t.datetime "expiry_date"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "enterprise_id"
t.index ["code", "enterprise_id"], name: "index_vouchers_on_code_and_enterprise_id", unique: true
t.index ["enterprise_id"], name: "index_vouchers_on_enterprise_id"
end
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "adjustment_metadata", "enterprises", name: "adjustment_metadata_enterprise_id_fk"
@@ -1302,4 +1312,5 @@ ActiveRecord::Schema[7.0].define(version: 2023_02_13_160135) do
add_foreign_key "variant_overrides", "enterprises", column: "hub_id", name: "variant_overrides_hub_id_fk"
add_foreign_key "variant_overrides", "spree_variants", column: "variant_id", name: "variant_overrides_variant_id_fk"
add_foreign_key "webhook_endpoints", "spree_users", column: "user_id"
add_foreign_key "vouchers", "enterprises"
end

View File

@@ -24,6 +24,7 @@ describe Enterprise do
it { is_expected.to have_many(:distributed_orders) }
it { is_expected.to belong_to(:address) }
it { is_expected.to belong_to(:business_address) }
it { is_expected.to have_many(:vouchers) }
it "destroys enterprise roles upon its own demise" do
e = create(:enterprise)

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
require 'spec_helper'
describe Voucher do
describe 'associations' do
it { is_expected.to belong_to(:enterprise) }
end
describe 'validations' do
subject { Voucher.new(code: 'new_code', enterprise: enterprise) }
let(:enterprise) { build(:enterprise) }
it { is_expected.to validate_presence_of(:code) }
it { is_expected.to validate_uniqueness_of(:code).scoped_to(:enterprise_id) }
end
end