From 7c998e094201659bce12592ac8955e870f073b1c Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Tue, 2 Aug 2022 11:05:27 +0100 Subject: [PATCH] Version schemes and locations (#799) --- app/models/location.rb | 2 ++ app/models/scheme.rb | 2 ++ spec/models/location_spec.rb | 14 ++++++++++++++ spec/models/scheme_spec.rb | 14 ++++++++++++++ 4 files changed, 32 insertions(+) diff --git a/app/models/location.rb b/app/models/location.rb index c21917b70..754e63b33 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -3,6 +3,8 @@ class Location < ApplicationRecord validates :units, :type_of_unit, :mobility_type, presence: true belongs_to :scheme + has_paper_trail + before_save :lookup_postcode!, if: :postcode_changed? attr_accessor :add_another_location diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 5052b171c..82d259704 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -4,6 +4,8 @@ class Scheme < ApplicationRecord has_many :locations has_many :case_logs + has_paper_trail + scope :filter_by_id, ->(id) { where(id: (id.start_with?("S") ? id[1..] : id)) } scope :search_by_service_name, ->(name) { where("service_name ILIKE ?", "%#{name}%") } scope :search_by_postcode, ->(postcode) { joins("LEFT JOIN locations ON locations.scheme_id = schemes.id").where("locations.postcode ILIKE ?", "%#{postcode.delete(' ')}%") } diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index 764a780d6..acf3bd618 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -56,6 +56,20 @@ RSpec.describe Location, type: :model do end end + describe "paper trail" do + let(:location) { FactoryBot.create(:location) } + let!(:name) { location.name } + + it "creates a record of changes to a log" do + expect { location.update!(name: "new test name") }.to change(location.versions, :count).by(1) + end + + it "allows case logs to be restored to a previous version" do + location.update!(name: "new test name") + expect(location.paper_trail.previous_version.name).to eq(name) + end + end + describe "scopes" do before do FactoryBot.create(:location, name: "ABC", postcode: "NW18RR") diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index dddf8edb4..14d60cf1f 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -8,6 +8,20 @@ RSpec.describe Scheme, type: :model do expect(scheme.owning_organisation).to be_a(Organisation) end + describe "paper trail" do + let(:scheme) { FactoryBot.create(:scheme) } + let!(:name) { scheme.service_name } + + it "creates a record of changes to a log" do + expect { scheme.update!(service_name: "new test name") }.to change(scheme.versions, :count).by(1) + end + + it "allows case logs to be restored to a previous version" do + scheme.update!(service_name: "new test name") + expect(scheme.paper_trail.previous_version.service_name).to eq(name) + end + end + describe "scopes" do let!(:scheme_1) { FactoryBot.create(:scheme) } let!(:scheme_2) { FactoryBot.create(:scheme) }