mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Add Voucher model
A voucher belongs to an enterprise and an enterprise can have many vouchers
This commit is contained in:
@@ -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
7
app/models/voucher.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
# frozen_string_literal: false
|
||||
|
||||
class Voucher < ApplicationRecord
|
||||
belongs_to :enterprise
|
||||
|
||||
validates :code, presence: true, uniqueness: { scope: :enterprise_id }
|
||||
end
|
||||
12
db/migrate/20230215034821_create_vouchers.rb
Normal file
12
db/migrate/20230215034821_create_vouchers.rb
Normal 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
|
||||
11
db/schema.rb
11
db/schema.rb
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
18
spec/models/voucher_spec.rb
Normal file
18
spec/models/voucher_spec.rb
Normal 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
|
||||
Reference in New Issue
Block a user