|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class ConfirmationController < ApplicationController |
| 4 | + rescue_from ActiveRecord::RecordNotUnique, with: :error_not_unique |
| 5 | + |
| 6 | + # new populates instance variables used by the new confirmation form. |
| 7 | + def new |
| 8 | + @term = Term.find(params[:term_id]) |
| 9 | + @categories = Category.all |
| 10 | + @confirmation = Confirmation.new |
| 11 | + end |
| 12 | + |
| 13 | + # create receives the submission from the new confirmation form, creating the needed record with the help of various |
| 14 | + # private methods. |
| 15 | + def create |
| 16 | + new_record = Confirmation.new(confirmation_params) |
| 17 | + |
| 18 | + if new_record.save |
| 19 | + # Now that the confirmation has saved, set the flag on the related Term record. |
| 20 | + flag_term(params[:term_id]) if confirmation_flag?(params[:confirmation][:category_id]) |
| 21 | + |
| 22 | + # By this point, saving has succeeded |
| 23 | + feedback_for(confirmation_flag?(params[:confirmation][:category_id])) |
| 24 | + redirect_to terms_unconfirmed_path |
| 25 | + else |
| 26 | + error_cannot_save |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + private |
| 31 | + |
| 32 | + def confirmation_params |
| 33 | + params.require(:confirmation).permit(:term_id, :category_id, :user_id) |
| 34 | + end |
| 35 | + |
| 36 | + # feedback_for defines an appropriate flash message, based on whether the user has placed the term in the "flagged" |
| 37 | + # category or not. |
| 38 | + def feedback_for(flagged) |
| 39 | + if flagged == true |
| 40 | + flash[:success] = 'Term flagged for review' |
| 41 | + else |
| 42 | + flash[:success] = "Term confirmed as #{Category.find_by(id: params[:confirmation][:category_id]).name}" |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + # error_cannot_save catches cases where the received Confirmation record cannot be saved, for whatever reason. |
| 47 | + def error_cannot_save |
| 48 | + flash[:error] = 'Unable to finish confirming this term. Please try again, or try a different term.' |
| 49 | + Sentry.capture_message('Unable to confirm term in a category') |
| 50 | + redirect_to terms_unconfirmed_path |
| 51 | + end |
| 52 | + |
| 53 | + # error_not_unique catches the RecordNotUnique error in case a duplicate confirmation is ever submitted, and shows an |
| 54 | + # appropriate error message to the user. |
| 55 | + def error_not_unique(exception) |
| 56 | + flash[:error] = 'Duplicate confirmations are not supported' |
| 57 | + Sentry.capture_message(exception.message) |
| 58 | + redirect_to terms_unconfirmed_path |
| 59 | + end |
| 60 | + |
| 61 | + # flag_term sets the "flagged" boolean field on a Term record when a Confirmation comes in for that category. |
| 62 | + def flag_term(term_id) |
| 63 | + t = Term.find_by(id: term_id) |
| 64 | + t.flag = true |
| 65 | + t.save |
| 66 | + end |
| 67 | + |
| 68 | + # confirmation_flag? compares the submitted category (coerced to an integer) to the ID value for the "flagged" |
| 69 | + # category. |
| 70 | + # |
| 71 | + # @param submission e.g. params[:confirmation][:category] |
| 72 | + # @return boolean |
| 73 | + def confirmation_flag?(submission) |
| 74 | + submission.to_i == Category.find_by(name: 'Flagged').id |
| 75 | + end |
| 76 | +end |
0 commit comments