Skip to content

Commit

Permalink
Results page
Browse files Browse the repository at this point in the history
  • Loading branch information
BroiSatse committed Jul 6, 2023
1 parent d20a1be commit 893271a
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 69 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jobs:
with:
node-version: '16.20.0'

- name: Install dependencies
run: sudo apt-get install -y libproj-dev proj-bin

- name: Set up ruby gem cache
uses: actions/cache@v3
with:
Expand Down Expand Up @@ -102,6 +105,9 @@ jobs:
restore-keys: |
${{ runner.os }}-gems-
- name: Install dependencies
run: sudo apt-get install -y libproj-dev proj-bin

- name: Install gems
run: |
bundle config path vendor/bundle
Expand Down Expand Up @@ -139,6 +145,9 @@ jobs:
restore-keys: |
${{ runner.os }}-gems-
- name: Install dependencies
run: sudo apt-get install -y libproj-dev proj-bin

- name: Install gems
run: |
bundle config path vendor/bundle
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RUN apk add --update --no-cache tzdata && \
# build-base: dependencies for bundle
# yarn: node package manager
# postgresql-dev: postgres driver and libraries
RUN apk add --no-cache build-base yarn postgresql13-dev
RUN apk add --no-cache build-base yarn postgresql13-dev libproj-dev proj-bin

# Install gems defined in Gemfile
COPY .ruby-version Gemfile Gemfile.lock ./
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ ruby "3.1.2"
gem "activerecord-postgis-adapter"
gem "bootsnap", require: false
gem "bundler", "~> 2.4"
gem 'geocoder'
gem "pg", "~> 1.1"
gem "puma", "~> 5.0"
gem "rails", "~> 7.0.4", ">= 7.0.4.3"
gem "rgeo"
gem "rgeo-geojson"
gem "rgeo-proj4"

gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]

gem "cssbundling-rails"
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ GEM
dotenv (= 2.8.1)
railties (>= 3.2)
erubi (1.12.0)
geocoder (1.8.2)
globalid (1.1.0)
activesupport (>= 5.0)
govuk-components (4.0.0)
Expand Down Expand Up @@ -207,6 +208,8 @@ GEM
rgeo (>= 1.0.0)
rgeo-geojson (2.1.1)
rgeo (>= 1.0.0)
rgeo-proj4 (4.0.0)
rgeo (~> 3.0.0)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
Expand Down Expand Up @@ -305,6 +308,7 @@ DEPENDENCIES
cssbundling-rails
debug
dotenv-rails
geocoder
govuk-components
govuk_design_system_formbuilder
jsbundling-rails
Expand All @@ -315,6 +319,7 @@ DEPENDENCIES
rails (~> 7.0.4, >= 7.0.4.3)
rgeo
rgeo-geojson
rgeo-proj4
rspec
rspec-rails
rubocop-govuk
Expand Down
9 changes: 9 additions & 0 deletions app/assets/stylesheets/application.sass.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ $govuk-new-link-styles: true;
$govuk-assets-path: "/";

@import "govuk-frontend/govuk/all";

.hub-results {
border-top: 1px solid govuk-colour("mid-grey");

.hub-result {
padding-top: 20px;
border-bottom: 1px solid govuk-colour("mid-grey");
}
}
6 changes: 1 addition & 5 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ def form
def results
@search = SearchForm.new(params.permit(:location) )

if @search.valid?
raise NotImplementedError
else
render 'form', status: :unprocessable_entity
end
render 'form', status: :unprocessable_entity unless @search.valid?
end
end
2 changes: 1 addition & 1 deletion app/lib/hub/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get_local_authorities(string)
inversed_map = @authorities_with_comas_map.invert

mapped.split(', ').map do |str|
inversed_map[str] || ALIASES[str] || str
inversed_map[str] || str
end
end

Expand Down
42 changes: 42 additions & 0 deletions app/lib/local_authority/search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class LocalAuthority::Search
def initialize(scope, query)
@scope = scope
@query = query
end

def call
result = @scope.order_by_distance_from(search_centre)

if search_polygon
result.where("ST_Intersects(geometry, ?)", search_polygon)
else
result.limit(3)
end
end

def geo_result
@geo_result ||= Geocoder.search(@query, params: {limit: 1, polygon_geojson: 1, countrycodes: 'gb'}).first
end

def search_polygon
return @search_polygon if defined? @search_polygon

json = geo_result && geo_result.data['geojson']
return @search_polygon = nil unless json

transform RGeo::GeoJSON.decode(json.to_json)
end

def search_centre
transform RGeo::Geographic.spherical_factory.point(geo_result.longitude, geo_result.latitude)
end

def transform(geo)
RGeo::CoordSys::Proj4.transform(
RGeo::CoordSys::Proj4.new("EPSG:4326"),
geo,
RGeo::CoordSys::Proj4.new("EPSG:27700"),
RGeo::Cartesian.preferred_factory
)
end
end
3 changes: 3 additions & 0 deletions app/models/hub.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class Hub < ApplicationRecord
def self.by_local_authority(*authorities)
where("areas && ARRAY[?]::varchar[]", authorities)
end
end
13 changes: 13 additions & 0 deletions app/models/local_authority.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
class LocalAuthority < ApplicationRecord
include RGeo::ActiveRecord::GeometryMixin

def self.search(query)
Search.new(self, query).call
end

def self.order_by_distance_from(point)
order(Arel.sql("geometry <-> ST_Geomfromtext('#{point.as_text}')"))
end

def self.by_name(name)
where("name ILIKE '%#{name}%'")
end
end
5 changes: 5 additions & 0 deletions app/models/search_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ class SearchForm
attribute :location

validates :location, presence: true

def hubs
authorities = LocalAuthority.search(location)
Hub.by_local_authority(*authorities.pluck(:name))
end
end
38 changes: 38 additions & 0 deletions app/views/search/results.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl">Teaching school hubs near <%= @search.location %></h1>
</div>
</div>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<p>
<%= link_to 'Change location', { action: :form } %>
</p>
</div>
</div>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds hub-results">
<% @search.hubs.each do |hub| %>
<div class="hub-result">
<h3 class="govuk-heading-m">
<% if hub.website %>
<%
address = hub.website
address = "//#{address}" unless address.starts_with? /https?:\/\//
%>
<%= link_to hub.name, address %>
<% else %>
<%= hub.name %>
<% end %>
</h3>
<p>Email: <%= mail_to hub.email %></p>
<% if hub.phone.present? %>
<p>Phone: <%= phone_to hub.phone %></p>
<% end %>
<p>Area: <%= hub.areas.join(', ') %></p>
</div>
<% end %>
</div>
</div>
2 changes: 1 addition & 1 deletion db/migrate/20230626105700_create_local_authorities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class CreateLocalAuthorities < ActiveRecord::Migration[7.0]
def change
create_table :local_authorities do |t|
t.string :name
t.geometry :geometry, srid: 27_700
t.geometry :geometry

t.timestamps
end
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

create_table "local_authorities", force: :cascade do |t|
t.string "name"
t.geometry "geometry", limit: {:srid=>27700, :type=>"geometry"}
t.geometry "geometry", limit: {:srid=>0, :type=>"geometry"}
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["geometry"], name: "index_local_authorities_on_geometry", using: :gist
Expand Down
1 change: 1 addition & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
LocalAuthority::Importer.new.reload!
Hub::Importer.new.reload!
60 changes: 0 additions & 60 deletions lib/tasks/auto_annotate_models.rake

This file was deleted.

0 comments on commit 893271a

Please sign in to comment.