Skip to content

Commit 4336c22

Browse files
authored
Merge pull request #27 from dxw/290-add-customers-model
Add new Customer model (including import script)
2 parents be6b551 + 9337b63 commit 4336c22

10 files changed

Lines changed: 50492 additions & 1 deletion

File tree

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Rails:
1313
Bundler/OrderedGems:
1414
Enabled: false
1515

16+
Rails/Output:
17+
Exclude:
18+
- 'db/data_migrate/**/*'
19+
1620
Style/Alias:
1721
Enabled: false
1822

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ gem 'auth0', require: false
3232

3333
gem 'rubocop'
3434

35+
gem 'progress_bar', require: false
36+
3537
group :development, :test do
3638
gem 'byebug', platforms: %i[mri mingw x64_mingw]
3739
gem 'dotenv-rails'

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ GEM
8989
ffi (1.9.23)
9090
globalid (0.4.1)
9191
activesupport (>= 4.2.0)
92+
highline (1.7.10)
9293
http-cookie (1.0.3)
9394
domain_name (~> 0.5)
9495
i18n (1.0.1)
@@ -134,11 +135,15 @@ GEM
134135
nio4r (2.3.1)
135136
nokogiri (1.8.2)
136137
mini_portile2 (~> 2.3.0)
138+
options (2.3.2)
137139
parallel (1.12.1)
138140
parser (2.5.1.0)
139141
ast (~> 2.4.0)
140142
pg (1.0.0)
141143
powerpack (0.1.1)
144+
progress_bar (1.2.0)
145+
highline (~> 1.6)
146+
options (~> 2.3.0)
142147
pry (0.11.3)
143148
coderay (~> 1.1.0)
144149
method_source (~> 0.9.0)
@@ -274,6 +279,7 @@ DEPENDENCIES
274279
jsonapi-rspec
275280
listen (>= 3.0.5, < 3.2)
276281
pg (>= 0.18, < 2.0)
282+
progress_bar
277283
pry-rails
278284
puma (~> 3.11)
279285
rails (~> 5.2.0)

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

db/data_migrate/20180807095900_customers.csv

Lines changed: 50407 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'csv'
2+
require 'progress_bar'
3+
4+
puts 'Reading CSV file...'
5+
customers_csv_path = Rails.root.join('db', 'data_migrate', '20180807095900_customers.csv')
6+
csv = CSV.read(customers_csv_path, headers: true, header_converters: :symbol)
7+
8+
puts 'Importing customers...'
9+
bar = ProgressBar.new(csv.count)
10+
csv.each do |customer_row|
11+
postcode = customer_row[:postcode] == 'XXXX' ? nil : customer_row[:postcode].strip
12+
13+
Customer.find_or_create_by(urn: customer_row[:urn]) do |customer|
14+
customer.postcode = postcode
15+
customer.sector = customer_row[:sector].strip.parameterize.underscore
16+
customer.name = customer_row[:customername].strip
17+
end
18+
19+
bar.increment!
20+
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)