Skip to content

Commit 058e8a7

Browse files
committed
Add Customer model
This is the model that represents the part of government a supplier is selling to. This model has become necessary so that we can generate the report for finance that groups invoice totals and levys according to the customers' sectors, i.e. "central government", or "wider public sector". I'm keeping the attributes on this model to a minimum for now to avoid having to add code we don't need just yet. Right now, all we really need are the URN number and the sector. That said, I've additionally included the name and postcode, mainly because we are expecting to need these two fields very soon when we add the URN look-up tool to the supplier-facing application. I've also avoided adding too many validations or constraints on fields such as the URN number. We can define and implement those when we get to the backend administrative part of the system.
1 parent be6b551 commit 058e8a7

5 files changed

Lines changed: 53 additions & 1 deletion

File tree

app/models/customer.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Customer < ApplicationRecord
2+
enum sector: { central_government: 0, wider_public_sector: 1 }
3+
4+
validates :name, :sector, presence: true
5+
validates :urn, presence: true, uniqueness: true
6+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class CreateCustomers < ActiveRecord::Migration[5.2]
2+
def change
3+
create_table :customers, id: :uuid do |t|
4+
t.string :name, null: false
5+
t.string :postcode
6+
t.integer :urn, null: false
7+
t.integer :sector, null: false
8+
9+
t.timestamps
10+
end
11+
12+
add_index :customers, :urn, unique: true
13+
add_index :customers, :postcode
14+
add_index :customers, :sector
15+
end
16+
end

db/schema.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2018_08_06_160648) do
13+
ActiveRecord::Schema.define(version: 2018_08_07_093650) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "pgcrypto"
@@ -23,6 +23,18 @@
2323
t.index ["supplier_id"], name: "index_agreements_on_supplier_id"
2424
end
2525

26+
create_table "customers", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
27+
t.string "name", null: false
28+
t.string "postcode"
29+
t.integer "urn", null: false
30+
t.integer "sector", null: false
31+
t.datetime "created_at", null: false
32+
t.datetime "updated_at", null: false
33+
t.index ["postcode"], name: "index_customers_on_postcode"
34+
t.index ["sector"], name: "index_customers_on_sector"
35+
t.index ["urn"], name: "index_customers_on_urn", unique: true
36+
end
37+
2638
create_table "event_store_events", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
2739
t.string "event_type", null: false
2840
t.text "metadata"

spec/factories/customers.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FactoryBot.define do
2+
factory :customer do
3+
name 'Department for Silly Hats'
4+
sequence(:urn)
5+
postcode 'W1 7ZX'
6+
sector Customer.sectors[:central_government]
7+
end
8+
end

spec/models/customer_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe Customer do
4+
subject { FactoryBot.create(:customer) }
5+
6+
it { is_expected.to validate_presence_of(:name) }
7+
it { is_expected.to validate_presence_of(:urn) }
8+
it { is_expected.to validate_presence_of(:sector) }
9+
it { is_expected.to validate_uniqueness_of(:urn) }
10+
end

0 commit comments

Comments
 (0)