From d142a97c5042f64915570e22466a46a300f8d723 Mon Sep 17 00:00:00 2001 From: Christian Sutter Date: Sun, 26 Nov 2023 16:38:43 +0000 Subject: [PATCH] Enable ranking submission --- app/assets/stylesheets/search.scss | 8 ++++---- app/controllers/feedbacks_controller.rb | 2 +- app/controllers/searches_controller.rb | 5 ++++- app/models/feedback.rb | 11 +++++++++++ app/models/result_rating.rb | 10 ++++++---- app/views/searches/_result.html.erb | 25 +++++++++---------------- app/views/searches/show.html.erb | 4 ++-- 7 files changed, 37 insertions(+), 28 deletions(-) diff --git a/app/assets/stylesheets/search.scss b/app/assets/stylesheets/search.scss index 57e9a1f..3e30863 100644 --- a/app/assets/stylesheets/search.scss +++ b/app/assets/stylesheets/search.scss @@ -129,19 +129,19 @@ box-shadow: 0 0 0 8px $govuk-focus-colour; } -.search-results__ranking-option--perfect input:checked { +.search-results__ranking-option--3 input:checked { background-color: govuk-colour("blue"); } -.search-results__ranking-option--good input:checked { +.search-results__ranking-option--2 input:checked { background-color: govuk-colour("green"); } -.search-results__ranking-option--ok input:checked { +.search-results__ranking-option--1 input:checked { background-color: govuk-colour("orange"); } -.search-results__ranking-option--bad input:checked { +.search-results__ranking-option--0 input:checked { background-color: govuk-colour("red"); } diff --git a/app/controllers/feedbacks_controller.rb b/app/controllers/feedbacks_controller.rb index e1ef185..103bf98 100644 --- a/app/controllers/feedbacks_controller.rb +++ b/app/controllers/feedbacks_controller.rb @@ -15,7 +15,7 @@ def create def feedback_params params.require(:feedback).permit( - :search_query, :suggested_url, :comments, result_ratings: %i[content_id url position rating] + :search_query, :suggested_url, :comments, result_ratings_attributes: {} ) end end diff --git a/app/controllers/searches_controller.rb b/app/controllers/searches_controller.rb index 68d942a..1d44f17 100644 --- a/app/controllers/searches_controller.rb +++ b/app/controllers/searches_controller.rb @@ -1,7 +1,10 @@ class SearchesController < ApplicationController def show @search = Search.new(query_params) - @feedback = Feedback.new(search_query: @search.query) + @feedback = Feedback.new( + search_query: @search.query, + result_ratings: @search.results.map.with_index { |r, i| ResultRating.new(content_id: r.content_id, link: r.link, position: i) }, + ) end private diff --git a/app/models/feedback.rb b/app/models/feedback.rb index 51d6b56..231c390 100644 --- a/app/models/feedback.rb +++ b/app/models/feedback.rb @@ -6,6 +6,17 @@ class Feedback attr_accessor :search_query, :result_ratings, :suggested_url, :comments + def result_ratings_attributes=(attributes) + @result_ratings = attributes.map do |_, v| + ResultRating.new( + content_id: v["content_id"], + link: v["link"], + position: v["position"].to_i, + rating: v["rating"].presence&.to_i, # nil rating is legitimate, don't convert to zero + ) + end + end + def save return false unless valid? diff --git a/app/models/result_rating.rb b/app/models/result_rating.rb index e6d5e21..f501d6b 100644 --- a/app/models/result_rating.rb +++ b/app/models/result_rating.rb @@ -1,16 +1,18 @@ class ResultRating RATING_OPTIONS = { - 0 => "Bad", - 1 => "OK", - 2 => "Good", 3 => "Perfect", + 2 => "Good", + 1 => "OK", + 0 => "Bad", }.freeze include ActiveModel::Model - attr_accessor :content_id, :url, :position, :rating + attr_accessor :content_id, :link, :position, :rating def self.rating_options RATING_OPTIONS end + + def id = content_id end diff --git a/app/views/searches/_result.html.erb b/app/views/searches/_result.html.erb index a22b9a3..f609abe 100644 --- a/app/views/searches/_result.html.erb +++ b/app/views/searches/_result.html.erb @@ -1,4 +1,7 @@
  • + <%= form.hidden_field :content_id %> + <%= form.hidden_field :link %> + <%= form.hidden_field :position %>

    <%= govuk_link_to result.title, result.link %> @@ -26,21 +29,11 @@ How relevant is this result? - - - - + <% ResultRating.rating_options.each do |value, label| %> + + <% end %>

  • diff --git a/app/views/searches/show.html.erb b/app/views/searches/show.html.erb index b2bb804..08de0c3 100644 --- a/app/views/searches/show.html.erb +++ b/app/views/searches/show.html.erb @@ -44,8 +44,8 @@
      - <% @search.results.each do |result| %> - <%= render partial: "searches/result", locals: { result:, form: } %> + <%= form.fields_for :result_ratings do |ff| %> + <%= render partial: "searches/result", locals: { form: ff, result: @search.results[ff.object.position] } %> <% end %>