From d9672c00bd652add2b04d0ec19e625038f08cf74 Mon Sep 17 00:00:00 2001 From: Slawosz Slawinski Date: Mon, 27 Nov 2023 09:28:54 +0000 Subject: [PATCH] Order results by distance --- app/models/hub.rb | 6 +++++- spec/models/hub_spec.rb | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 spec/models/hub_spec.rb diff --git a/app/models/hub.rb b/app/models/hub.rb index 9d35b39..dcfa153 100644 --- a/app/models/hub.rb +++ b/app/models/hub.rb @@ -1,5 +1,9 @@ class Hub < ApplicationRecord def self.by_local_authority(*authorities) - where("areas && ARRAY[?]::varchar[]", authorities) + result = [] + authorities.each do |authority| + result << where("areas && ARRAY[?]::varchar[]", [authority]).to_a + end + result.flatten.uniq end end diff --git a/spec/models/hub_spec.rb b/spec/models/hub_spec.rb new file mode 100644 index 0000000..7328554 --- /dev/null +++ b/spec/models/hub_spec.rb @@ -0,0 +1,16 @@ +RSpec.describe Hub, type: :model do + before do + LocalAuthority::Importer.new.reload! + Hub::Importer.new.reload! + end + + let(:authorities) { ["Birmingham", "Sandwell", "Dudley", "Bromsgrove", "Wyre Forest"] } + let(:hub_names) do + ["Ark Teaching School Hub", "Arthur Terry Teaching School Hub - North Birmingham", + "Haybridge Teaching School Hub", "Tudor Grange Teaching School Hub", "Prince Henry's Teaching School Hub"] + end + + it "sorts results by distance" do + expect(described_class.by_local_authority(*authorities).map(&:name)).to eq(hub_names) + end +end