From d400e19c52e7b5dee4c4aa40260c4ff80b0a1237 Mon Sep 17 00:00:00 2001 From: JG Date: Thu, 26 May 2022 09:28:33 +0100 Subject: [PATCH] rubocop --- app/models/case_log.rb | 8 +++++--- spec/features/log_spec.rb | 8 ++++---- spec/models/case_log_spec.rb | 16 ++++++++-------- spec/requests/case_logs_controller_spec.rb | 10 +++++----- .../exports/case_log_export_service_spec.rb | 2 +- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index e8108c3b1..9b23c067e 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -51,14 +51,16 @@ class CaseLog < ApplicationRecord end } - scope :filter_by_id, ->(id) { where(id: id) } + scope :filter_by_id, ->(id) { where(id:) } scope :filter_by_tenancy_code, ->(code) { where(tenancy_code: code) } scope :filter_by_propcode, ->(code) { where(propcode: code) } scope :filter_by_postcode, ->(code) { where(postcode_full: code) } - scope :filter_by, ->(param) { filter_by_id(param) + scope :filter_by, lambda { |param| + filter_by_id(param) .or(filter_by_tenancy_code(param)) .or(filter_by_propcode(param)) - .or(filter_by_postcode(param.upcase.gsub(/\s+/, ""))) } + .or(filter_by_postcode(param.upcase.gsub(/\s+/, ""))) + } AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze OPTIONAL_FIELDS = %w[first_time_property_let_as_social_housing tenant_code propcode].freeze diff --git a/spec/features/log_spec.rb b/spec/features/log_spec.rb index d120d4119..0fdecb003 100644 --- a/spec/features/log_spec.rb +++ b/spec/features/log_spec.rb @@ -1,8 +1,8 @@ require "rails_helper" RSpec.describe "Log Features" do - context "Searching for specific logs" do - context "I am logged in and there are logs in the database" do + context "when searching for specific logs" do + context "when I am logged in and there are logs in the database" do let(:user) { FactoryBot.create(:user, last_sign_in_at: Time.zone.now) } let(:log_to_search) { FactoryBot.create(:case_log, owning_organisation: user.organisation, tenancy_code: "111") } let(:same_organisation_log) { FactoryBot.create(:case_log, owning_organisation: user.organisation, tenancy_code: "222") } @@ -15,14 +15,14 @@ RSpec.describe "Log Features" do click_button("Sign in") end - context "I can search for a specific log" do + context "when I search for a specific log" do it "there is a search bar with a message and search button for logs" do expect(page).to have_field("search-field") expect(page).to have_content("Search by log ID, tenant code, property reference or postcode") expect(page).to have_button("Search") end - it "it displays log matching the log ID" do + it "displays log matching the log ID" do fill_in("search-field", with: log_to_search.id) click_button("Search") expect(page).to have_content(log_to_search.id) diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 9718f0093..91103060b 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1863,7 +1863,7 @@ RSpec.describe CaseLog do end end - describe "scopes" do + describe "Searching scopes" do let!(:case_log_1) { FactoryBot.create(:case_log, :in_progress, startdate: Time.utc(2021, 5, 3), created_by: created_by_user, postcode_full: "XX12YY") } let!(:case_log_2) { FactoryBot.create(:case_log, :completed, startdate: Time.utc(2021, 5, 3), created_by: created_by_user) } @@ -1871,38 +1871,38 @@ RSpec.describe CaseLog do FactoryBot.create(:case_log, startdate: Time.utc(2022, 6, 3)) end - context "searching scopes" do + context "when searching scopes" do let!(:case_log_3) { FactoryBot.create(:case_log, :completed, startdate: Time.utc(2021, 5, 3), created_by: created_by_user, postcode_full: "XX12YY") } - context "#filter_by_id" do + describe "#filter_by_id" do it "allows searching by a log ID" do expect(described_class.filter_by_id(case_log_1.id.to_s).count).to eq(1) expect(described_class.filter_by_id(case_log_1.id.to_s).first.id).to eq case_log_1.id end end - context "#filter_by_tenancy_code" do + describe "#filter_by_tenancy_code" do it "allows searching by a Tenancy Code" do expect(described_class.filter_by_tenancy_code(case_log_2.tenancy_code).count).to eq(1) expect(described_class.filter_by_tenancy_code(case_log_2.tenancy_code).first.id).to eq case_log_2.id end end - context "#filter_by_propcode" do + describe "#filter_by_propcode" do it "allows searching by a Property Reference" do expect(described_class.filter_by_propcode(case_log_2.propcode).count).to eq(1) expect(described_class.filter_by_propcode(case_log_2.propcode).first.id).to eq case_log_2.id end end - context "#filter_by_postcode" do + describe "#filter_by_postcode" do it "allows searching by a Property Postcode" do expect(described_class.filter_by_postcode(case_log_2.postcode_full).count).to eq(1) expect(described_class.filter_by_postcode(case_log_2.postcode_full).first.id).to eq case_log_2.id end end - context "#filter_by" do + describe "#filter_by" do it "allows searching using ID" do expect(described_class.filter_by(case_log_1.id.to_s).count).to eq(1) expect(described_class.filter_by(case_log_1.id.to_s).first.id).to eq case_log_1.id @@ -1924,7 +1924,7 @@ RSpec.describe CaseLog do expect(described_class.filter_by(case_log_1.postcode_full).last.id).to eq case_log_3.id end - context "postcode has spaces and lower case letters" do + context "when postcode has spaces and lower case letters" do let(:matching_postcode_lower_case_with_spaces) { case_log_2.postcode_full.downcase.chars.insert(3, " ").join } it "allows searching by a Property Postcode" do diff --git a/spec/requests/case_logs_controller_spec.rb b/spec/requests/case_logs_controller_spec.rb index ca64fde73..772d61b7d 100644 --- a/spec/requests/case_logs_controller_spec.rb +++ b/spec/requests/case_logs_controller_spec.rb @@ -308,8 +308,8 @@ RSpec.describe CaseLogsController, type: :request do expect(page).not_to have_content("Managing organisation") end - context "using a search query" do - let(:logs) { FactoryBot.create_list(:case_log, 3, :completed, owning_organisation: user.organisation) } + context "when using a search query" do + let(:logs) { FactoryBot.create_list(:case_log, 3, :completed, owning_organisation: user.organisation) } it "shows case logs matching the id" do get "/logs?search-field=#{logs[0].id}", headers: headers, params: {} @@ -332,7 +332,7 @@ RSpec.describe CaseLogsController, type: :request do expect(page).not_to have_content(logs[2].id) end - context "matching postcode" do + context "when matching postcode" do it "shows case logs matching the post code" do get "/logs?search-field=#{logs[1].postcode_full}", headers: headers, params: {} expect(page).not_to have_content(logs[0].id) @@ -341,7 +341,7 @@ RSpec.describe CaseLogsController, type: :request do end end - context "search query doesn't match any logs" do + context "when search query doesn't match any logs" do it "doesn't display any logs" do get "/logs?search-field=foobar", headers: headers, params: {} expect(page).not_to have_content(logs[0].id) @@ -350,7 +350,7 @@ RSpec.describe CaseLogsController, type: :request do end end - context "search query is empty" do + context "when search query is empty" do it "doesn't display any logs" do get "/logs?search-field=", headers: headers, params: {} expect(page).not_to have_content(logs[0].id) diff --git a/spec/services/exports/case_log_export_service_spec.rb b/spec/services/exports/case_log_export_service_spec.rb index 49d2a9fae..2e3585f79 100644 --- a/spec/services/exports/case_log_export_service_spec.rb +++ b/spec/services/exports/case_log_export_service_spec.rb @@ -226,7 +226,7 @@ RSpec.describe Exports::CaseLogExportService do let(:csv_export_file) { File.open("spec/fixtures/exports/case_logs.csv", "r:UTF-8") } let(:expected_csv_filename) { "export_2022_05_01.csv" } - let(:case_log) { FactoryBot.create(:case_log, :completed, tenancy_code: "BZ757", propcode: "123", ppostcode_full: "SE2 6RT", postcode_full: "NW1 5TY" ) } + let(:case_log) { FactoryBot.create(:case_log, :completed, tenancy_code: "BZ757", propcode: "123", ppostcode_full: "SE2 6RT", postcode_full: "NW1 5TY") } it "generates an CSV export file with the expected content" do expected_content = replace_entity_ids(case_log, csv_export_file.read)