Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order results by distance #83

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +3 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense but how is it ordering them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The list of authorities is ordered.

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
Loading