Skip to content

Commit

Permalink
Update the frameworks table in preparation for the other model changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-s-ccs committed Sep 5, 2024
1 parent 0651ded commit a15adb9
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 37 deletions.
6 changes: 3 additions & 3 deletions app/models/framework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class Framework < ApplicationRecord
validate :dates_are_valid, on: :update

def self.frameworks
pluck(:framework)
pluck(:id)
end

def self.live_frameworks
where('live_at <= ? AND expires_at > ?', Time.now.in_time_zone('London'), Time.now.in_time_zone('London')).pluck(:framework)
where('live_at <= ? AND expires_at > ?', Time.now.in_time_zone('London'), Time.now.in_time_zone('London')).pluck(:id)
end

def self.current_framework
Expand All @@ -28,7 +28,7 @@ def self.recognised_framework?(framework)
end

def status
if self.class.send(service).live_framework?(framework)
if self.class.send(service).live_framework?(id)
:live
else
live_at <= Time.now.in_time_zone('London') ? :expired : :coming
Expand Down
4 changes: 4 additions & 0 deletions data/frameworks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id,service,live_at,expires_at
RM6238,supply_teachers,"2022-09-12","2026-09-01"
RM6187,management_consultancy,"2021-09-04","2025-09-04"
RM6240,legal_services,"2022-10-03","2026-10-01"
55 changes: 55 additions & 0 deletions db/migrate/20240704110527_refactor_frameworks_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class RefactorFrameworksTable < ActiveRecord::Migration[7.1]
class FrameworkOld < ApplicationRecord
self.table_name = 'frameworks_old'
end

class Framework < ApplicationRecord
self.table_name = 'frameworks'
end

def up
rename_table :frameworks, :frameworks_old

create_table :frameworks, id: :string, limit: 6 do |t|
t.string :service, limit: 25
t.date :live_at
t.date :expires_at

t.timestamps
end

FrameworkOld.find_each do |framework|
Framework.create(
id: framework.framework,
service: framework.service,
live_at: framework.live_at,
expires_at: framework.expires_at
)
end

drop_table :frameworks_old
end

def down
create_table 'frameworks_old', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t|
t.string 'service', limit: 25
t.string 'framework', limit: 6
t.date 'live_at'
t.timestamps
t.date 'expires_at'
end

Framework.find_each do |framework|
FrameworkOld.create(
framework: framework.id,
service: framework.service,
live_at: framework.live_at,
expires_at: framework.expires_at
)
end

drop_table :frameworks

rename_table :frameworks_old, :frameworks
end
end
7 changes: 3 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2023_03_14_113131) do
ActiveRecord::Schema[7.1].define(version: 2024_07_04_110527) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -43,13 +43,12 @@
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end

create_table "frameworks", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
create_table "frameworks", id: { type: :string, limit: 6 }, force: :cascade do |t|
t.string "service", limit: 25
t.string "framework", limit: 6
t.date "live_at"
t.date "expires_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.date "expires_at"
end

create_table "legal_services_rm6240_admin_uploads", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
Expand Down
25 changes: 4 additions & 21 deletions lib/tasks/frameworks.rake
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
module Frameworks
def self.rm6238_expires_at
if Rails.env.test?
1.year.from_now
else
# This is not correct but it is far in the future and we can update it with another migration later on
Time.new(2026, 9, 1).in_time_zone('London')
end
end

def self.rm6240_expires_at
if Rails.env.test?
1.year.from_now
else
# This is not correct but it is far in the future and we can update it with another migration later on
Time.new(2026, 10, 1).in_time_zone('London')
end
end

def self.add_frameworks
ActiveRecord::Base.connection.truncate_tables(:frameworks)
Framework.create(service: 'supply_teachers', framework: 'RM6238', live_at: Time.new(2022, 9, 12).in_time_zone('London'), expires_at: rm6238_expires_at)
Framework.create(service: 'management_consultancy', framework: 'RM6187', live_at: Time.new(2021, 9, 4).in_time_zone('London'), expires_at: Time.new(2025, 9, 4).in_time_zone('London'))
Framework.create(service: 'legal_services', framework: 'RM6240', live_at: Time.new(2022, 10, 3).in_time_zone('London'), expires_at: rm6240_expires_at)

CSV.foreach('data/frameworks.csv', headers: true) do |row|
Framework.create(id: row['id'], service: row['service'], live_at: Time.parse(row['live_at']).in_time_zone('London'), expires_at: Rails.env.test? ? 1.year.from_now : Time.parse(row['expires_at']).in_time_zone('London'))
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/factories/frameworks.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FactoryBot.define do
factory :legacy_framework, class: 'Framework' do
id { "FK#{Array.new(4) { rand(10) }.join}" }
service { ('a'..'z').to_a.sample(8).join }
framework { "FK#{Array.new(4) { rand(10) }.join}" }
live_at { 1.year.from_now }
expires_at { 2.years.from_now }
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/framework_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
end

describe '.status' do
let(:result) { described_class.find_by(framework:).status }
let(:result) { described_class.find(framework).status }

context 'when considering supply_teacher frameworks' do
context 'and RM6238 goes live tomorrow' do
Expand Down
14 changes: 7 additions & 7 deletions spec/support/framework_status.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
RSpec.shared_context 'and RM6238 is live in the future' do
before { Framework.find_by(framework: 'RM6238').update(live_at: 1.day.from_now) }
before { Framework.find('RM6238').update(live_at: 1.day.from_now) }
end

RSpec.shared_context 'and RM6238 is live today' do
before { Framework.find_by(framework: 'RM6238').update(live_at: Time.zone.now) }
before { Framework.find('RM6238').update(live_at: Time.zone.now) }
end

RSpec.shared_context 'and RM6238 has expired' do
before { Framework.find_by(framework: 'RM6238').update(expires_at: Time.zone.now) }
before { Framework.find('RM6238').update(expires_at: Time.zone.now) }
end

RSpec.shared_context 'and RM6240 is live in the future' do
before { Framework.find_by(framework: 'RM6240').update(live_at: 1.day.from_now) }
before { Framework.find('RM6240').update(live_at: 1.day.from_now) }
end

RSpec.shared_context 'and RM6240 is live today' do
before { Framework.find_by(framework: 'RM6240').update(live_at: Time.zone.now) }
before { Framework.find('RM6240').update(live_at: Time.zone.now) }
end

RSpec.shared_context 'and RM6240 has expired' do
before { Framework.find_by(framework: 'RM6240').update(expires_at: Time.zone.now) }
before { Framework.find('RM6240').update(expires_at: Time.zone.now) }
end

RSpec.shared_context 'and RM6187 has expired' do
before { Framework.find_by(framework: 'RM6187').update(expires_at: Time.zone.now) }
before { Framework.find('RM6187').update(expires_at: Time.zone.now) }
end

0 comments on commit a15adb9

Please sign in to comment.