From 7fda68974a394a1108bf314106e66f2dcb66c235 Mon Sep 17 00:00:00 2001 From: CatalinVoineag <11318084+CatalinVoineag@users.noreply.github.com> Date: Thu, 4 Jan 2024 15:58:34 +0000 Subject: [PATCH] Wip --- Gemfile | 1 + Gemfile.lock | 4 + .../gias_school_suggestions_controller.rb | 19 +++++ .../claims/support/schools_controller.rb | 28 +++++++ app/javascript/application.js | 3 + app/javascript/gias_school_autocomplete.js | 28 +++++++ app/models/gias_school.rb | 21 +++++ .../claims/support/schools/check.html.erb | 83 +++++++++++++++++++ .../claims/support/schools/index.html.erb | 2 + app/views/claims/support/schools/new.html.erb | 30 +++++++ config/routes/claims.rb | 3 + 11 files changed, 222 insertions(+) create mode 100644 app/controllers/claims/support/gias_school_suggestions_controller.rb create mode 100644 app/javascript/gias_school_autocomplete.js create mode 100644 app/views/claims/support/schools/check.html.erb create mode 100644 app/views/claims/support/schools/new.html.erb diff --git a/Gemfile b/Gemfile index f18e363a74..38db031a93 100644 --- a/Gemfile +++ b/Gemfile @@ -64,6 +64,7 @@ gem "down", "~> 5.0" # HTTP Request gem "httparty" +gem 'pg_search' group :development do gem "annotate", require: false diff --git a/Gemfile.lock b/Gemfile.lock index 1c648957b4..985f6aa275 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -287,6 +287,9 @@ GEM ast (~> 2.4.1) racc pg (1.5.4) + pg_search (2.3.6) + activerecord (>= 5.2) + activesupport (>= 5.2) prettier_print (1.2.1) propshaft (0.8.0) actionpack (>= 7.0.0) @@ -551,6 +554,7 @@ DEPENDENCIES omniauth-rails_csrf_protection omniauth_openid_connect pg (~> 1.1) + pg_search prettier_print propshaft pry diff --git a/app/controllers/claims/support/gias_school_suggestions_controller.rb b/app/controllers/claims/support/gias_school_suggestions_controller.rb new file mode 100644 index 0000000000..f4a355bfee --- /dev/null +++ b/app/controllers/claims/support/gias_school_suggestions_controller.rb @@ -0,0 +1,19 @@ +class Claims::Support::GiasSchoolSuggestionsController < ApplicationController + def index + gias_schools = GiasSchool.search_scope(query_params) + render json: gias_schools.map { |school| formatted_school(school) } + end + + private + + def query_params + params.require(:query).downcase + end + + def formatted_school(school) + { + urn: school.urn, + name: school.name, + } + end +end diff --git a/app/controllers/claims/support/schools_controller.rb b/app/controllers/claims/support/schools_controller.rb index 2aaa18fe9f..ca43e0de9d 100644 --- a/app/controllers/claims/support/schools_controller.rb +++ b/app/controllers/claims/support/schools_controller.rb @@ -1,2 +1,30 @@ class Claims::Support::SchoolsController < Claims::Support::ApplicationController + def new + @school = Claims::School.new + end + + def create + school = gias_school.school || gias_school.build_school + school.claims = true + + if school.save + redirect_to claims_support_schools_path + else + render :new + end + end + + def check + @school = gias_school + end + + private + + def gias_school + @gias_school ||= GiasSchool.find_by_urn(gias_school_urn) + end + + def gias_school_urn + params[:gias_school_urn] || params.dig(:gias_school, :urn) + end end diff --git a/app/javascript/application.js b/app/javascript/application.js index 44aa21966c..f9285bd168 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -1,7 +1,10 @@ import { initAll } from "govuk-frontend"; import autocompleteSetup from "./autocomplete"; import accreditedProviderAutocompleteSetup from "./accredited_provider"; +import giasSchoolAutocompleteSetup from "./gias_school_autocomplete"; initAll(); autocompleteSetup(); accreditedProviderAutocompleteSetup(); +accreditedProviderAutocompleteSetup(); +giasSchoolAutocompleteSetup(); diff --git a/app/javascript/gias_school_autocomplete.js b/app/javascript/gias_school_autocomplete.js new file mode 100644 index 0000000000..ede497f729 --- /dev/null +++ b/app/javascript/gias_school_autocomplete.js @@ -0,0 +1,28 @@ +import initAutocomplete from "./autocomplete"; + +const giasSchoolTemplate = (result) => result && result.name; +const giasSchoolSuggestionTemplate = (result) => + result && `${result.name}`; +const onConfirm = (input) => (option) => + (input.value = option ? option.urn : ""); + +function init() { + const options = { + path: `/support/gias_school_suggestions`, + template: { + inputValue: giasSchoolTemplate, + suggestion: giasSchoolSuggestionTemplate, + }, + minLength: 2, + inputName: "gias_school_urn", + onConfirm, + }; + + initAutocomplete( + "gias-school-autocomplete", + "gias-school-search-form-query-field", + options, + ); +} + +export default init; diff --git a/app/models/gias_school.rb b/app/models/gias_school.rb index 922e51e1a3..0046323b47 100644 --- a/app/models/gias_school.rb +++ b/app/models/gias_school.rb @@ -21,9 +21,30 @@ # index_gias_schools_on_urn (urn) UNIQUE # class GiasSchool < ApplicationRecord + include PgSearch::Model has_one :school, foreign_key: :urn, primary_key: :urn validates :urn, presence: true validates :urn, uniqueness: { case_sensitive: false } validates :name, presence: true + + + #pg_search_scope :search_scope, + # against: [:code, :name], + # using: { + # tsearch: {only: [:code, :name]}, + # trigram: { + # only: [:code, :name], + # threshold: 0.7, + # word_similarity: true + # } + # } + pg_search_scope :search_scope, + against: [:name, :postcode, :urn], + using: { + tsearch: {only: [:name, :postcode, :urn]}, + #trigram: { + # only: [:name, :postcode, :urn, :ukprn] + #}, + } end diff --git a/app/views/claims/support/schools/check.html.erb b/app/views/claims/support/schools/check.html.erb new file mode 100644 index 0000000000..ca66b72f4b --- /dev/null +++ b/app/views/claims/support/schools/check.html.erb @@ -0,0 +1,83 @@ +<%= content_for(:before_content) do %> + <%= govuk_back_link(href: new_claims_support_school_path) %> +<% end %> + +
+ <%= @school.address1 %>
+
+ <%= @school.address2 %>
+
+ <%= @school.address3 %>
+
+ <%= @school.town %>
+
+ <%= @school.postcode %>
+
+ <%= govuk_link_to(t(".cancel"), claims_support_schools_path) %> +
+ <% end %> ++ <%= govuk_link_to(t("cancel"), root_path) %> +
+