Skip to content

Commit

Permalink
Order results by distance
Browse files Browse the repository at this point in the history
  • Loading branch information
slawosz committed Dec 1, 2023
1 parent 1934737 commit d9672c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/models/hub.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions spec/models/hub_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d9672c0

Please sign in to comment.