Skip to content

Commit

Permalink
Merge pull request #83 from co-cddo/add-update-record
Browse files Browse the repository at this point in the history
Add record of updates to the system
  • Loading branch information
RobNicholsGDS authored May 29, 2024
2 parents 8036c90 + 528c406 commit 8155aca
Show file tree
Hide file tree
Showing 15 changed files with 362 additions and 8 deletions.
5 changes: 5 additions & 0 deletions app/controllers/update_logs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class UpdateLogsController < ApplicationController
def index
@update_logs = UpdateLog.order(updated_on: :desc, created_at: :desc)
end
end
2 changes: 2 additions & 0 deletions app/models/update_log.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class UpdateLog < ApplicationRecord
end
50 changes: 50 additions & 0 deletions app/services/log_updates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class LogUpdates
def self.after(time)
new(time:).update_log
end

attr_reader :time

def initialize(time:)
@time = time
end

def update_log
return unless changes?

@update_log ||= UpdateLog.create!(updated_on: time, comment: report)
end

def report
return unless changes?
return created_text if updated_ids.empty?

[
created_text,
"Additionally, entries with the following ID numbers have been updated:",
updated_ids.to_sentence,
].join(" ")
end

def created_text
[
number_created,
"Agreement".pluralize(number_created),
"created.",
].join(" ")
end

def changes?
number_created.positive? || updated_ids.present?
end

def number_created
@number_created ||= Agreement.where(created_at: time..).count
end

def updated_ids
@updated_ids ||= Agreement.where(updated_at: time..)
.where.not(created_at: time..)
.pluck(Arel.sql("fields->'ID'"))
end
end
3 changes: 2 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
Agreements: root_path,
Powers: powers_path,
Controllers: control_people_path,
Processors: processors_path
Processors: processors_path,
Updates: update_logs_path,
}.each do |text, href|
header.with_navigation_item(text:, href:, active: current_page?(href))
end
Expand Down
14 changes: 14 additions & 0 deletions app/views/update_logs/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<% page_title = "Update Log" %>

<h1 class="govuk-heading-l">Record of updates</h1>

<%=
govuk_summary_list do |summary_list|
@update_logs.each do |update_log|
summary_list.with_row do |row|
row.with_key(text: update_log.updated_on.strftime("%d %B %Y"))
row.with_value(text: update_log.comment)
end
end
end
%>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
resources :processors, only: %i[index show]
resources :control_people, only: %i[index show], path: :controllers
resources :search, only: [:index]
resources :update_logs, only: [:index]
end
11 changes: 11 additions & 0 deletions db/migrate/20240528130310_create_update_logs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateUpdateLogs < ActiveRecord::Migration[7.0]
def change
create_table :update_logs do |t|
t.date :updated_on
t.string :comment
t.boolean :from_seeds, default: false

t.timestamps
end
end
end
10 changes: 9 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 18 additions & 4 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
results = [
start_time = Time.zone.now

[
Agreement,
ControlPerson,
Processor,
Expand All @@ -17,9 +19,21 @@
hash[model.to_s] = model.count
end

report = ["The following have been created:"]
results.each { |n, c| report << "\t#{n} - #{c}" }
report = report.join("\n")
# Load historic update logs from previous system
update_records = YAML.load_file(
Rails.root.join("db/seeds/update_record.yml"),
symbolize_names: true,
)

update_records.each do |record|
date = Date.parse(record[:date])
UpdateLog.where(from_seeds: true).find_or_create_by!(updated_on: date) do |update_log|
update_log.comment = record[:text]
update_log.from_seeds = true
end
end

report = LogUpdates.after(start_time)&.comment || "No changes made on seeding"

# rubocop:disable Rails/Output
puts report # This is sent to STOUT to provide feedback when seeding run at console
Expand Down
95 changes: 95 additions & 0 deletions db/seeds/update_record.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
- date: 14 May 2024
text: A new searchable version of the register has been uploaded replacing the spreadsheet attachment

- date: 5 March 2024
text: new entries added

- date: 20 February 2024
text: new entries added

- date: 27 November 2023
text: register updated

- date: 30 October 2023
text: New entries added.

- date: 28 September 2023
text: Register updated

- date: 2 August 2023
text: "New register entries added. Additionally, entries with the following ID numbers have been updated: 60,64,92,96 and 215."

- date: 31 March 2023
text: New entries added

- date: 9 February 2023
text: new entries amended

- date: 9 February 2023
text: New entries added

- date: 9 January 2023
text: Entries added

- date: 8 December 2022
text: New entries added

- date: 9 August 2022
text: New entries added. End date of debt pilots on lines 97 - 137 updated.

- date: 1 July 2022
text: New entries added.

- date: 17 May 2022
text: New entry added.

- date: 10 May 2022
text: New entries added. The end date for three water poverty ISAs 94,95,96 have also been amended.

- date: 27 April 2022
text: New entry added

- date: 8 April 2022
text: new entry added.

- date: 30 March 2022
text: New entries added

- date: 13 January 2022
text: Register entry no.88 removed.

- date: 7 January 2022
text: Register updated. New entries added. ID ref 97, new controllers added.

- date: 9 November 2021
text: Register updated. New entries added.

- date: 18 October 2021
text: Register updated

- date: 17 September 2021
text: Register updated

- date: 14 September 2021
text: Register updated.

- date: 1 September 2021
text: Register updated

- date: 28 July 2021
text: Register updated

- date: 14 July 2021
text: Register updated

- date: 24 June 2021
text: Register updated.

- date: 27 May 2021
text: Register update

- date: 12 May 2021
text: Register updated

- date: 30 April 2021
text: First published.
4 changes: 3 additions & 1 deletion spec/factories/agreements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
factory :agreement do
name { Faker::Name.name }
record_id { SecureRandom.uuid }
fields { { name: } }
sequence :fields do |n|
{ name:, ID: n }
end
end
end
6 changes: 6 additions & 0 deletions spec/factories/update_logs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :update_log do
updated_on { 2.days.ago.to_date }
comment { Faker::Lorem.sentence }
end
end
2 changes: 1 addition & 1 deletion spec/models/agreement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
end

context "when id missing" do
let(:agreement) { create :agreement }
let(:agreement) { create :agreement, fields: {} }
it "just returns the name" do
expect(agreement.id_and_name).to eq(agreement.name)
end
Expand Down
12 changes: 12 additions & 0 deletions spec/requests/update_logs_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "rails_helper"

RSpec.describe "UpdateLogs", type: :request do
describe "GET /index" do
let!(:update_log) { create :update_log }

it "displays log" do
get update_logs_path
expect(response.body).to include(update_log.comment)
end
end
end
Loading

0 comments on commit 8155aca

Please sign in to comment.