From 79ed7f6a8f843676a4aa47520f21ad1a5fc8f2f1 Mon Sep 17 00:00:00 2001 From: JG Date: Wed, 29 Jun 2022 10:44:43 +0100 Subject: [PATCH] further refactorings --- app/models/scheme.rb | 16 ++++++--------- app/views/schemes/_scheme_list.html.erb | 2 +- spec/features/schemes_spec.rb | 10 +++++----- .../requests/organisations_controller_spec.rb | 20 +++++++++---------- spec/requests/schemes_controller_spec.rb | 14 ++++++------- 5 files changed, 29 insertions(+), 33 deletions(-) diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 63b5adffb..3c5347438 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -1,6 +1,4 @@ class Scheme < ApplicationRecord - before_create :create_code - belongs_to :organisation has_many :locations has_many :case_logs @@ -87,9 +85,13 @@ class Scheme < ApplicationRecord enum intended_stay: INTENDED_STAY, _suffix: true enum has_other_client_group: HAS_OTHER_CLIENT_GROUP, _suffix: true + def id_to_display + "S#{id}" + end + def check_details_attributes [ - { name: "Service code", value: code }, + { name: "Service code", value: id_to_display }, { name: "Name", value: service_name }, { name: "Confidential information", value: sensitive }, { name: "Managed by", value: organisation.name }, @@ -126,7 +128,7 @@ class Scheme < ApplicationRecord def display_attributes [ - { name: "Service code", value: code }, + { name: "Service code", value: id_to_display }, { name: "Name", value: service_name }, { name: "Confidential information", value: sensitive }, { name: "Managed by", value: organisation.name }, @@ -139,10 +141,4 @@ class Scheme < ApplicationRecord { name: "Intended length of stay", value: intended_stay }, ] end - -private - - def create_code - self.code = Scheme.last.nil? ? "S1" : "S#{Scheme.last.code[1..].to_i + 1}" - end end diff --git a/app/views/schemes/_scheme_list.html.erb b/app/views/schemes/_scheme_list.html.erb index a5b2ab06b..8b5c6c85c 100644 --- a/app/views/schemes/_scheme_list.html.erb +++ b/app/views/schemes/_scheme_list.html.erb @@ -22,7 +22,7 @@ <% @schemes.each do |scheme| %> <%= table.body do |body| %> <%= body.row do |row| %> - <% row.cell(text: scheme.code) %> + <% row.cell(text: scheme.id_to_display) %> <% row.cell(text: simple_format(scheme_cell(scheme), { class: "govuk-!-font-weight-bold" }, wrapper_tag: "div")) %> <% row.cell(text: scheme.organisation.name) %> <% row.cell(text: scheme.created_at.to_formatted_s(:govuk_date)) %> diff --git a/spec/features/schemes_spec.rb b/spec/features/schemes_spec.rb index 767ef7c8c..5ebffdc42 100644 --- a/spec/features/schemes_spec.rb +++ b/spec/features/schemes_spec.rb @@ -25,7 +25,7 @@ RSpec.describe "Schemes scheme Features" do it "shows list of schemes" do schemes.each do |scheme| - expect(page).to have_content(scheme.code) + expect(page).to have_content(scheme.id) end end @@ -53,9 +53,9 @@ RSpec.describe "Schemes scheme Features" do it "displays all schemes after I clear the search results" do click_link("Clear search") - expect(page).to have_content(scheme_to_search.code) + expect(page).to have_content(scheme_to_search.id) schemes.each do |scheme| - expect(page).to have_content(scheme.code) + expect(page).to have_content(scheme.id) end end end @@ -100,7 +100,7 @@ RSpec.describe "Schemes scheme Features" do it "shows list of schemes" do schemes.each do |scheme| - expect(page).to have_content(scheme.code) + expect(page).to have_content(scheme.id) end end @@ -130,7 +130,7 @@ RSpec.describe "Schemes scheme Features" do click_link("Clear search") expect(page).to have_content(scheme_to_search.code) schemes.each do |scheme| - expect(page).to have_content(scheme.code) + expect(page).to have_content(scheme.id) end end end diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index 01a45f2b1..e2237a673 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -65,15 +65,15 @@ RSpec.describe OrganisationsController, type: :request do end it "shows only schemes belonging to the same organisation" do - expect(page).to have_content(same_org_scheme.code) + expect(page).to have_content(same_org_scheme.id) schemes.each do |scheme| - expect(page).not_to have_content(scheme.code) + expect(page).not_to have_content(scheme.id) end end context "when searching" do let!(:searched_scheme) { FactoryBot.create(:scheme, organisation: user.organisation) } - let(:search_param) { searched_scheme.code } + let(:search_param) { searched_scheme.id } before do FactoryBot.create(:location, scheme: searched_scheme) @@ -82,9 +82,9 @@ RSpec.describe OrganisationsController, type: :request do end it "returns matching results" do - expect(page).to have_content(searched_scheme.code) + expect(page).to have_content(searched_scheme.id) schemes.each do |scheme| - expect(page).not_to have_content(scheme.code) + expect(page).not_to have_content(scheme.id) end end @@ -122,9 +122,9 @@ RSpec.describe OrganisationsController, type: :request do end it "shows only schemes belonging to the same organisation" do - expect(page).to have_content(same_org_scheme.code) + expect(page).to have_content(same_org_scheme.id) schemes.each do |scheme| - expect(page).not_to have_content(scheme.code) + expect(page).not_to have_content(scheme.id) end end @@ -142,7 +142,7 @@ RSpec.describe OrganisationsController, type: :request do context "when searching" do let!(:searched_scheme) { FactoryBot.create(:scheme, organisation: user.organisation) } - let(:search_param) { searched_scheme.code } + let(:search_param) { searched_scheme.id } before do FactoryBot.create(:location, scheme: searched_scheme) @@ -150,9 +150,9 @@ RSpec.describe OrganisationsController, type: :request do end it "returns matching results" do - expect(page).to have_content(searched_scheme.code) + expect(page).to have_content(searched_scheme.id) schemes.each do |scheme| - expect(page).not_to have_content(scheme.code) + expect(page).not_to have_content(scheme.id) end end diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 8fc459ffd..682332011 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -56,7 +56,7 @@ RSpec.describe SchemesController, type: :request do it "shows all schemes" do schemes.each do |scheme| - expect(page).to have_content(scheme.code) + expect(page).to have_content(scheme.id) end end @@ -145,9 +145,9 @@ RSpec.describe SchemesController, type: :request do end it "returns matching results" do - expect(page).to have_content(searched_scheme.code) + expect(page).to have_content(searched_scheme.id) schemes.each do |scheme| - expect(page).not_to have_content(scheme.code) + expect(page).not_to have_content(scheme.id) end end @@ -196,11 +196,11 @@ RSpec.describe SchemesController, type: :request do it "has page heading" do get "/schemes/#{specific_scheme.id}" - expect(page).to have_content(specific_scheme.code) + expect(page).to have_content(specific_scheme.id) expect(page).to have_content(specific_scheme.service_name) expect(page).to have_content(specific_scheme.organisation.name) expect(page).to have_content(specific_scheme.sensitive) - expect(page).to have_content(specific_scheme.code) + expect(page).to have_content(specific_scheme.id) expect(page).to have_content(specific_scheme.service_name) expect(page).to have_content(specific_scheme.sensitive) expect(page).to have_content(specific_scheme.scheme_type) @@ -230,11 +230,11 @@ RSpec.describe SchemesController, type: :request do end it "has page heading" do - expect(page).to have_content(specific_scheme.code) + expect(page).to have_content(specific_scheme.id) expect(page).to have_content(specific_scheme.service_name) expect(page).to have_content(specific_scheme.organisation.name) expect(page).to have_content(specific_scheme.sensitive) - expect(page).to have_content(specific_scheme.code) + expect(page).to have_content(specific_scheme.id) expect(page).to have_content(specific_scheme.service_name) expect(page).to have_content(specific_scheme.sensitive) expect(page).to have_content(specific_scheme.scheme_type)