Browse Source

Version schemes and locations (#799)

pull/800/head
baarkerlounger 2 years ago committed by GitHub
parent
commit
7c998e0942
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/models/location.rb
  2. 2
      app/models/scheme.rb
  3. 14
      spec/models/location_spec.rb
  4. 14
      spec/models/scheme_spec.rb

2
app/models/location.rb

@ -3,6 +3,8 @@ class Location < ApplicationRecord
validates :units, :type_of_unit, :mobility_type, presence: true validates :units, :type_of_unit, :mobility_type, presence: true
belongs_to :scheme belongs_to :scheme
has_paper_trail
before_save :lookup_postcode!, if: :postcode_changed? before_save :lookup_postcode!, if: :postcode_changed?
attr_accessor :add_another_location attr_accessor :add_another_location

2
app/models/scheme.rb

@ -4,6 +4,8 @@ class Scheme < ApplicationRecord
has_many :locations has_many :locations
has_many :case_logs has_many :case_logs
has_paper_trail
scope :filter_by_id, ->(id) { where(id: (id.start_with?("S") ? id[1..] : id)) } 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_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(' ')}%") } scope :search_by_postcode, ->(postcode) { joins("LEFT JOIN locations ON locations.scheme_id = schemes.id").where("locations.postcode ILIKE ?", "%#{postcode.delete(' ')}%") }

14
spec/models/location_spec.rb

@ -56,6 +56,20 @@ RSpec.describe Location, type: :model do
end end
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 describe "scopes" do
before do before do
FactoryBot.create(:location, name: "ABC", postcode: "NW18RR") FactoryBot.create(:location, name: "ABC", postcode: "NW18RR")

14
spec/models/scheme_spec.rb

@ -8,6 +8,20 @@ RSpec.describe Scheme, type: :model do
expect(scheme.owning_organisation).to be_a(Organisation) expect(scheme.owning_organisation).to be_a(Organisation)
end 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 describe "scopes" do
let!(:scheme_1) { FactoryBot.create(:scheme) } let!(:scheme_1) { FactoryBot.create(:scheme) }
let!(:scheme_2) { FactoryBot.create(:scheme) } let!(:scheme_2) { FactoryBot.create(:scheme) }

Loading…
Cancel
Save