Skip to content

Commit 8e4fefe

Browse files
Merge pull request #144 from MITLibraries/tco-101
Finish implementing confirmation workflow
2 parents 5fbfd28 + 07a9189 commit 8e4fefe

22 files changed

+431
-71
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Rails/I18nLocaleTexts:
112112
Exclude:
113113
- 'app/controllers/admin/application_controller.rb'
114114
- 'app/controllers/application_controller.rb'
115+
- 'app/controllers/confirmation_controller.rb'
115116

116117
# Offense count: 25
117118
# Configuration parameters: AllowedConstants.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

app/controllers/term_controller.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
class TermController < ApplicationController
44
include Pagy::Backend
55

6-
def confirm_index
6+
# This provides the list of terms that are awaiting confirmation.
7+
def unconfirmed
78
@pagy, @records = pagy(Term.user_unconfirmed)
89
end
9-
10-
def confirm_term
11-
@term = Term.find(params[:id])
12-
@categories = Category.all
13-
end
1410
end

app/models/confirmation.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
# during the categorization workflow. Most of the time, we anticipate that the
66
# user will place the term into one of the primary categories. If no category
77
# seems appropriate, there is an "undefined" option available (which is not
8-
# used by TACOS normally).
9-
#
10-
# There is also a "flag" boolean field available, in case the user feels that
11-
# the term should be reviewed and removed from the dataset entirely. This flag
12-
# is why the category field is not a required value.
8+
# used by TACOS' automated categorization workflow). There is also a "flagged"
9+
# category, in case the user feels that the term should be reviewed and removed
10+
# from the dataset entirely.
1311
#
1412
# == Schema Information
1513
#
@@ -18,13 +16,12 @@
1816
# id :integer not null, primary key
1917
# user_id :integer not null
2018
# term_id :integer not null
21-
# category_id :integer
22-
# flag :boolean
19+
# category_id :integer not null
2320
# created_at :datetime not null
2421
# updated_at :datetime not null
2522
#
2623
class Confirmation < ApplicationRecord
2724
belongs_to :user
2825
belongs_to :term
29-
belongs_to :category, optional: true
26+
belongs_to :category
3027
end

app/models/term.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# phrase :string
1212
# created_at :datetime not null
1313
# updated_at :datetime not null
14+
# flag :boolean
1415
#
1516
class Term < ApplicationRecord
1617
has_many :search_events, dependent: :destroy
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<h3>Term confirmation form</h3>
2+
<p>Please put the following term into one of the categories below.</p>
3+
<div class="well">
4+
<p><%= @term.phrase %></p>
5+
</div>
6+
<%= form_for @confirmation, url: term_confirmation_index_path(@term) do |f| %>
7+
<%= hidden_field(:confirmation, :term_id, :value => @term.id) %>
8+
<%= hidden_field(:confirmation, :user_id, :value => current_user.id) %>
9+
<fieldset>
10+
<legend>Available categories</legend>
11+
<ul class="list-unbulleted categories">
12+
<% @categories.all.each do |cat| %>
13+
<li>
14+
<%= f.radio_button :category_id, cat.id, required: 'required' %>
15+
<div>
16+
<%= f.label(:category_id, cat.name, :value => cat.id) do
17+
content_tag(:p, cat.name)
18+
end %>
19+
<p><%= cat.description %></p>
20+
</div>
21+
</li>
22+
<% end %>
23+
</ul>
24+
</fieldset>
25+
<%= f.submit %>
26+
<% end %>
27+
28+
<style type="text/css">
29+
ul.categories li {
30+
display: flex;
31+
flex-flow: row nowrap;
32+
align-items: baseline;
33+
column-gap: 1rem;
34+
}
35+
ul.categories li label p {
36+
font-weight: bold;
37+
}
38+
</style>

app/views/layouts/_site_nav.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<%= nav_link_to("Home", root_path) %>
1212
<% if user_signed_in? %>
1313
<% if can? :manage, Confirmation %>
14-
<%= link_to('Confirm terms', confirm_index_path, class: 'nav-item') %>
14+
<%= link_to('Confirm terms', terms_unconfirmed_path, class: 'nav-item') %>
1515
<% end %>
1616
<% if can? :index, Term %>
1717
<%= link_to('Admin', admin_root_path, class: 'nav-item') %>

app/views/term/confirm_term.html.erb

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ take you to the form for submitting this information.</p>
77

88
<ul class="list-unbulleted">
99
<% @records.each do |t| %>
10-
<li><%= link_to(t.phrase, confirm_term_path(t)) %></li>
10+
<li><%= link_to(t.phrase, new_term_confirmation_path(t)) %></li>
1111
<% end %>
1212
</ul>
1313

config/routes.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
get '/report/algorithm_metrics', to: 'report#algorithm_metrics'
4040

4141
# Confirmation interface
42-
get '/confirm', to: 'term#confirm_index', as: 'confirm_index'
43-
get '/confirm/:id', to: 'term#confirm_term', as: 'confirm_term'
42+
get '/terms/unconfirmed', to: 'term#unconfirmed', as: 'terms_unconfirmed'
43+
resources :terms do
44+
resources :confirmation, only: [:new, :create]
45+
end
4446

4547
# Defines the root path route ("/")
4648
root to: 'static#index'

0 commit comments

Comments
 (0)