generated from dxw/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fworks): sync frameworks from faf
- Loading branch information
Showing
13 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Frameworks | ||
def self.table_name_prefix | ||
"frameworks_" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Frameworks::Framework < ApplicationRecord | ||
include FafImportable | ||
include StatusChangeable | ||
|
||
belongs_to :provider | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Frameworks::Framework::FafImportable | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def import_from_faf(framework_details) | ||
framework = find_or_initialize_by(provider_reference: framework_details.provider_reference) | ||
|
||
provider = Frameworks::Provider.find_or_create_by!(name: framework_details.provider_name) | ||
|
||
framework.update!( | ||
status: :dfe_approved, | ||
name: framework_details.name, | ||
provider_url: framework_details.provider_url, | ||
ends_at: framework_details.ends_at, | ||
description: framework_details.description, | ||
published_on_faf: true, | ||
provider:, | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Frameworks::Framework::StatusChangeable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
enum status: { | ||
pending_evaluation: 0, | ||
not_approved: 1, | ||
dfe_approved: 2, | ||
cab_approved: 3, | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class Frameworks::Provider < ApplicationRecord | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class CreateFrameworksFrameworks < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :frameworks_frameworks, id: :uuid do |t| | ||
t.integer :status, default: 0 | ||
t.string :provider_url | ||
t.string :provider_reference | ||
t.string :name | ||
t.string :short_name | ||
t.string :description | ||
t.datetime :starts_at | ||
t.datetime :ends_at | ||
t.boolean :published_on_faf | ||
t.uuid :provider_id, null: false | ||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class CreateFrameworksProviders < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :frameworks_providers, id: :uuid do |t| | ||
t.string :name | ||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FactoryBot.define do | ||
factory :frameworks_framework, class: "Frameworks::Framework" do | ||
association :provider, factory: :frameworks_provider | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FactoryBot.define do | ||
factory :frameworks_provider, class: "Frameworks::Provider" do | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require "rails_helper" | ||
|
||
describe Frameworks::Framework::FafImportable do | ||
describe ".import_from_faf" do | ||
subject(:frameworks) { Frameworks::Framework } | ||
|
||
let(:framework_data) do | ||
OpenStruct.new( | ||
name: "Books, stationery and education supplies", | ||
provider_reference: "books", | ||
provider_name: "Super Books LTD", | ||
provider_url: "https://my.framework.com", | ||
ends_at: 2.weeks.from_now, | ||
description: "Description of books" | ||
) | ||
end | ||
|
||
it "creates frameworks with a status of dfe_approved already set" do | ||
frameworks.import_from_faf(framework_data) | ||
expect(Frameworks::Framework.all.all?(&:dfe_approved?)).to eq(true) | ||
expect(Frameworks::Framework.all.all?(&:published_on_faf?)).to eq(true) | ||
end | ||
|
||
context "when the Framework already exists for the given provider_reference" do | ||
let!(:existing_framework) { create(:frameworks_framework, provider_reference: "books") } | ||
|
||
it "updates the Framework details only" do | ||
frameworks.import_from_faf(framework_data) | ||
|
||
existing_framework.reload | ||
expect(existing_framework.provider_url).to eq("https://my.framework.com") | ||
expect(existing_framework.name).to eq("Books, stationery and education supplies") | ||
expect(existing_framework.ends_at).to eq(framework_data.ends_at) | ||
expect(existing_framework.description).to eq("Description of books") | ||
end | ||
end | ||
|
||
context "when the Framework does not already exist for the given provider_reference" do | ||
it "creates a new Framework with the given details" do | ||
expect { frameworks.import_from_faf(framework_data) }.to \ | ||
change { Frameworks::Framework.where(provider_reference: "books").count }.from(0).to(1) | ||
|
||
new_framework = Frameworks::Framework.where(provider_reference: "books").first | ||
expect(new_framework.provider_url).to eq("https://my.framework.com") | ||
expect(new_framework.name).to eq("Books, stationery and education supplies") | ||
expect(new_framework.ends_at).to eq(framework_data.ends_at) | ||
expect(new_framework.description).to eq("Description of books") | ||
end | ||
end | ||
|
||
context "when the Provider with the given name does not already exist" do | ||
it "creates a new Provider linked to the Framework" do | ||
expect { frameworks.import_from_faf(framework_data) }.to \ | ||
change { Frameworks::Provider.where(name: "Super Books LTD").count }.from(0).to(1) | ||
|
||
framework = Frameworks::Framework.where(provider_reference: "books").first | ||
expect(framework.provider.name).to eq("Super Books LTD") | ||
end | ||
end | ||
|
||
context "when the Provider with the given name already exists" do | ||
let!(:existing_provider) { create(:frameworks_provider, name: "Super Books LTD") } | ||
|
||
it "links the existing Provider to the Framework" do | ||
expect { frameworks.import_from_faf(framework_data) }.not_to \ | ||
change { Frameworks::Provider.where(name: "Super Books LTD").count }.from(1) | ||
|
||
framework = Frameworks::Framework.where(provider_reference: "books").first | ||
expect(framework.provider).to eq(existing_provider) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters