Browse Source
* Add local authority links * Display all local authorities for location * set correct LA for log based on year * Format local authorities for locations * Rename variable * Add import task for la links * look at form start date because log start date might not be given * Update app/models/lettings_log.rb Co-authored-by: James Rose <james@jbpr.net> * Update app/helpers/locations_helper.rb Co-authored-by: James Rose <james@jbpr.net> * Refactor * Seed review app * Change dates format * Update the local authority link data * Typo * update mapping in Lettings logs --------- Co-authored-by: James Rose <james@jbpr.net>revert-1378-CLDC-1917-startdate-validation
kosiakkatrina
2 years ago
committed by
GitHub
18 changed files with 280 additions and 14 deletions
@ -1,4 +1,7 @@ |
|||||||
class LocalAuthority < ApplicationRecord |
class LocalAuthority < ApplicationRecord |
||||||
|
has_many :local_authority_links, dependent: :destroy |
||||||
|
has_many :linked_local_authorities, class_name: "LocalAuthority", through: :local_authority_links |
||||||
|
|
||||||
scope :active, ->(date) { where("start_date <= ? AND (end_date IS NULL OR end_date >= ?)", date, date) } |
scope :active, ->(date) { where("start_date <= ? AND (end_date IS NULL OR end_date >= ?)", date, date) } |
||||||
scope :england, -> { where("code LIKE ?", "E%") } |
scope :england, -> { where("code LIKE ?", "E%") } |
||||||
end |
end |
||||||
|
@ -0,0 +1,4 @@ |
|||||||
|
class LocalAuthorityLink < ApplicationRecord |
||||||
|
belongs_to :local_authority, class_name: "LocalAuthority" |
||||||
|
belongs_to :linked_local_authority, class_name: "LocalAuthority" |
||||||
|
end |
@ -0,0 +1,22 @@ |
|||||||
|
require "csv" |
||||||
|
|
||||||
|
module Imports |
||||||
|
class LocalAuthorityLinksService |
||||||
|
attr_reader :path, :count |
||||||
|
|
||||||
|
def initialize(path:) |
||||||
|
@path = path |
||||||
|
@count = 0 |
||||||
|
end |
||||||
|
|
||||||
|
def call |
||||||
|
CSV.foreach(path, headers: true) do |row| |
||||||
|
LocalAuthorityLink.upsert( |
||||||
|
{ local_authority_id: LocalAuthority.find_by(code: row["local_authority_code"]).id, |
||||||
|
linked_local_authority_id: LocalAuthority.find_by(code: row["linked_local_authority_code"]).id }, |
||||||
|
) |
||||||
|
@count += 1 |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
|
@ -0,0 +1,12 @@ |
|||||||
|
class AddLocalAuthorityLink < ActiveRecord::Migration[7.0] |
||||||
|
def change |
||||||
|
create_table :local_authority_links do |t| |
||||||
|
t.references :local_authority |
||||||
|
t.references :linked_local_authority |
||||||
|
|
||||||
|
t.timestamps |
||||||
|
end |
||||||
|
add_foreign_key :local_authority_links, :local_authorities, column: :local_authority_id |
||||||
|
add_foreign_key :local_authority_links, :local_authorities, column: :linked_local_authority_id |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,13 @@ |
|||||||
|
namespace :data_import do |
||||||
|
desc "Import local authority links data" |
||||||
|
task :local_authority_links, %i[path] => :environment do |_task, args| |
||||||
|
path = args[:path] |
||||||
|
|
||||||
|
raise "Usage: rake data_import:local_authority_links['path/to/csv_file']" if path.blank? |
||||||
|
|
||||||
|
service = Imports::LocalAuthorityLinksService.new(path:) |
||||||
|
service.call |
||||||
|
|
||||||
|
pp "Created/updated #{service.count} local authority link records" unless Rails.env.test? |
||||||
|
end |
||||||
|
end |
|
@ -0,0 +1,40 @@ |
|||||||
|
require "rails_helper" |
||||||
|
require "rake" |
||||||
|
|
||||||
|
RSpec.describe "data_import" do |
||||||
|
describe ":local_authority_links", type: :task do |
||||||
|
subject(:task) { Rake::Task["data_import:local_authority_links"] } |
||||||
|
|
||||||
|
before do |
||||||
|
LocalAuthorityLink.destroy_all |
||||||
|
Rake.application.rake_require("tasks/local_authority_links") |
||||||
|
Rake::Task.define_task(:environment) |
||||||
|
task.reenable |
||||||
|
end |
||||||
|
|
||||||
|
context "when the rake task is run" do |
||||||
|
let(:local_authority_links_file_path) { "./spec/fixtures/files/local_authority_links_2023.csv" } |
||||||
|
let(:wrong_file_path) { "/test/no_csv_here.csv" } |
||||||
|
|
||||||
|
it "creates new local authority links records" do |
||||||
|
expect { task.invoke(local_authority_links_file_path) }.to change(LocalAuthorityLink, :count).by(5) |
||||||
|
expect(LocalAuthorityLink.where(local_authority_id: LocalAuthority.find_by(code: "E06000063").id).exists?).to be true |
||||||
|
end |
||||||
|
|
||||||
|
it "raises an error when no path is given" do |
||||||
|
expect { task.invoke(nil) }.to raise_error(RuntimeError, "Usage: rake data_import:local_authority_links['path/to/csv_file']") |
||||||
|
end |
||||||
|
|
||||||
|
it "raises an error when no file exists at the given path" do |
||||||
|
expect { task.invoke(wrong_file_path) }.to raise_error(Errno::ENOENT) |
||||||
|
end |
||||||
|
|
||||||
|
context "when a record already exists with a matching ids" do |
||||||
|
it "does not create a new link" do |
||||||
|
task.invoke(local_authority_links_file_path) |
||||||
|
expect { task.invoke(local_authority_links_file_path) }.to change(LocalAuthorityLink, :count).by(0) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue