Skip to content

Commit f3c9c28

Browse files
Merge pull request #174 from MITLibraries/tco-126-confirmation-scope
Add option to confirm only categorized terms
2 parents 6d08c5f + 5083a2e commit f3c9c28

File tree

6 files changed

+92
-2
lines changed

6 files changed

+92
-2
lines changed

app/controllers/term_controller.rb

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

6-
# This provides the list of terms that are awaiting confirmation.
6+
# This provides the list of terms that are awaiting confirmation. By default this shows only terms which have been
7+
# categorized automatically. Adding `type=all` to the querystring will show _all_ terms which the user has not yet
8+
# confirmed.
79
def unconfirmed
8-
@pagy, @records = pagy(Term.user_unconfirmed)
10+
terms = if params[:show] == 'all'
11+
authorize! :confirm_uncategorized, Term
12+
Term.user_unconfirmed
13+
else
14+
Term.categorized.user_unconfirmed
15+
end
16+
@pagy, @records = pagy(terms)
917
end
1018
end

app/models/ability.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def initialize(user)
4040
# Rules for admins
4141
return unless user.admin?
4242

43+
can :confirm_uncategorized, Term
4344
can :manage, :all
4445
end
4546
end

app/models/term.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Term < ApplicationRecord
2424
before_save :register_fingerprint
2525
after_destroy :check_fingerprint_count
2626

27+
scope :categorized, -> { where.associated(:categorizations).distinct }
2728
scope :user_confirmed, -> { where.associated(:confirmations).distinct }
2829
scope :user_unconfirmed, -> { where.missing(:confirmations).distinct }
2930

app/views/term/unconfirmed.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
<p>The terms listed here have not yet been reviewed by a person and placed into a category. Clicking on any term will
44
take you to the form for submitting this information.</p>
55

6+
<% if can? :confirm_uncategorized, Term %>
7+
<p class="wrap-filters">View:
8+
<a class="btn button-small button-secondary" href="<%= terms_unconfirmed_path %>">Categorized terms</a>
9+
<a class="btn button-small button-secondary" href="<%= terms_unconfirmed_path(show: 'all') %>">All terms</a>
10+
</p>
11+
<% end %>
12+
613
<p><%== pagy_info(@pagy) %></p>
714

815
<ul class="list-unbulleted">

test/controllers/term_controller_test.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,52 @@ class TermControllerTest < ActionDispatch::IntegrationTest
2525

2626
assert_response :success
2727
end
28+
29+
test 'basic users see only one confirmation option' do
30+
sign_in users(:basic)
31+
get terms_unconfirmed_path
32+
33+
assert_select 'p.wrap-filters', text: "View:\n Categorized terms\n All terms", count: 0
34+
end
35+
36+
test 'admin users see two confirmation options' do
37+
sign_in users(:admin)
38+
get terms_unconfirmed_path
39+
40+
assert_select 'p.wrap-filters', text: "View:\n Categorized terms\n All terms", count: 1
41+
end
42+
43+
test 'basic users cannot access the confirmation option for uncategorized terms' do
44+
sign_in users(:basic)
45+
get terms_unconfirmed_path(show: 'all')
46+
47+
assert_redirected_to '/'
48+
follow_redirect!
49+
50+
assert_select 'div.alert', text: 'Not authorized.', count: 1
51+
end
52+
53+
test 'admin users can access the confirmation option for uncategorized terms' do
54+
sign_in users(:admin)
55+
get terms_unconfirmed_path(show: 'all')
56+
57+
assert_response :success
58+
end
59+
60+
test 'confirmation index can show two different sets of terms for admin users' do
61+
sign_in users(:admin)
62+
get terms_unconfirmed_path
63+
64+
# default_pagy will be something like "Displaying 10 items"
65+
default_pagy = response.parsed_body.xpath('//main//span').first.text
66+
default_pagy_count = default_pagy.split.second.to_i
67+
68+
get terms_unconfirmed_path(show: 'all')
69+
70+
# The '?type=all' route asks for more records, so the count should be higher
71+
all_pagy = response.parsed_body.xpath('//main//span').first.text
72+
all_pagy_count = all_pagy.split.second.to_i
73+
74+
assert_operator all_pagy_count, :>, default_pagy_count
75+
end
2876
end

test/models/term_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,31 @@ class TermTest < ActiveSupport::TestCase
299299
end
300300
end
301301

302+
test 'categorized scope returns an active record relation' do
303+
assert_kind_of ActiveRecord::Relation, Term.categorized
304+
end
305+
306+
test 'categorized scope accounts for terms with multiple categorizations' do
307+
categorized_count = Term.categorized.count
308+
t = terms('doi')
309+
orig_categorization_count = t.categorizations.count
310+
311+
# term has been categorized already
312+
assert_operator 1, :<=, orig_categorization_count
313+
314+
new_record = {
315+
term: t,
316+
category: categories('navigational'),
317+
confidence: 0.5,
318+
detector_version: '1'
319+
}
320+
Categorization.create!(new_record)
321+
322+
# The term has gained a category, but the categorized scope has not changed size.
323+
assert_operator orig_categorization_count, :<, t.categorizations.count
324+
assert_equal categorized_count, Term.categorized.count
325+
end
326+
302327
test 'user_confirmed scope returns an active record relation' do
303328
assert_kind_of ActiveRecord::Relation, Term.user_confirmed
304329
end

0 commit comments

Comments
 (0)