Skip to content

Commit be6b551

Browse files
authored
Merge pull request #26 from dxw/288-coda-reference-against-frameworks
Add coda_reference to the Framework model
2 parents a87c2fe + 17cf22b commit be6b551

5 files changed

Lines changed: 36 additions & 1 deletion

File tree

app/models/framework.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ class Framework < ApplicationRecord
66
has_many :suppliers, through: :agreements
77

88
validates :short_name, presence: true, uniqueness: true
9+
validates :coda_reference, allow_nil: true, format: {
10+
with: /\A40\d{4}\z/,
11+
message: 'must start with “40” and have four additional numbers, for example: “401234”'
12+
}
913
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
SHORT_NAME_TO_CODA_REFERENCE_MAPPING = {
3+
'RM3786' => '401108',
4+
'RM3756' => '401148',
5+
'RM3787' => '401149'
6+
}.freeze
7+
8+
SHORT_NAME_TO_CODA_REFERENCE_MAPPING.each_pair do |short_name, coda_reference|
9+
Framework.find_by!(short_name: short_name).update!(coda_reference: coda_reference)
10+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddCodaReferenceToFrameworks < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :frameworks, :coda_reference, :integer
4+
end
5+
end

db/schema.rb

Lines changed: 2 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_154052) do
13+
ActiveRecord::Schema.define(version: 2018_08_06_160648) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "pgcrypto"
@@ -52,6 +52,7 @@
5252
create_table "frameworks", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
5353
t.string "name"
5454
t.string "short_name", null: false
55+
t.integer "coda_reference"
5556
t.index ["short_name"], name: "index_frameworks_on_short_name", unique: true
5657
end
5758

spec/models/framework_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,20 @@
1010
subject { Framework.create(short_name: 'test') }
1111
it { is_expected.to validate_presence_of(:short_name) }
1212
it { is_expected.to validate_uniqueness_of(:short_name) }
13+
14+
it 'validates coda_reference is a 7 digit number, beginning with 40' do
15+
valid_coda_references = %w[401234 400292 409999]
16+
invalid_coda_references = %w[4012 501234 40AB12]
17+
18+
valid_coda_references.each do |coda_reference|
19+
expect(FactoryBot.create(:framework, coda_reference: coda_reference)).to be_valid
20+
end
21+
22+
invalid_coda_references.each do |coda_reference|
23+
framework = FactoryBot.build(:framework, coda_reference: coda_reference)
24+
expect(framework).not_to be_valid
25+
expect(framework.errors[:coda_reference]).to be_present
26+
end
27+
end
1328
end
1429
end

0 commit comments

Comments
 (0)