diff --git a/app/controllers/confirmation_controller.rb b/app/controllers/confirmation_controller.rb new file mode 100644 index 0000000..fe34ec8 --- /dev/null +++ b/app/controllers/confirmation_controller.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +class ConfirmationController < ApplicationController + def new + @term = Term.find(params[:term_id]) + @categories = Category.all + @confirmation = Confirmation.new + end + + def create + confirmation = Confirmation.new({ + term_id: params[:term_id], + user: current_user + }) + set_category(confirmation, params[:confirmation][:category]) + result = confirmation.save + feedback_for(result) + end + + private + + def feedback_for(result) + if result == true && params[:confirmation][:category] == 'flag' + flash[:success] = 'Term flagged for review' + redirect_to terms_unconfirmed_path + elsif result == true + flash[:success] = "Term confirmed as #{Category.find_by(id: params[:confirmation][:category]).name}" + redirect_to terms_unconfirmed_path + else + flash[:warning] = 'Unable to finish confirming this term. Please try again, or try a different term.' + redirect_back_or_to terms_unconfirmed_path + end + end + + def set_category(confirmation, category) + if category == 'flag' + confirmation.flag = true + else + confirmation.category_id = category + end + end +end diff --git a/app/controllers/term_controller.rb b/app/controllers/term_controller.rb index fe9bd8e..08e8ec1 100644 --- a/app/controllers/term_controller.rb +++ b/app/controllers/term_controller.rb @@ -3,12 +3,7 @@ class TermController < ApplicationController include Pagy::Backend - def confirm_index + def unconfirmed @pagy, @records = pagy(Term.user_unconfirmed) end - - def confirm_term - @term = Term.find(params[:id]) - @categories = Category.all - end end diff --git a/app/views/confirmation/new.html.erb b/app/views/confirmation/new.html.erb new file mode 100644 index 0000000..19a165b --- /dev/null +++ b/app/views/confirmation/new.html.erb @@ -0,0 +1,36 @@ +

Term confirmation

+ +
+

<%= @term.phrase %>

+
+<%= form_for @confirmation, url: term_confirmation_index_path(@term) do |f| %> +
+ Please put the above term into one of the following categories: + +
+ <%= f.submit %> +<% end %> + + diff --git a/app/views/layouts/_site_nav.html.erb b/app/views/layouts/_site_nav.html.erb index c6978ee..d712437 100644 --- a/app/views/layouts/_site_nav.html.erb +++ b/app/views/layouts/_site_nav.html.erb @@ -11,7 +11,7 @@ <%= nav_link_to("Home", root_path) %> <% if user_signed_in? %> <% if can? :manage, Confirmation %> - <%= link_to('Confirm terms', confirm_index_path, class: 'nav-item') %> + <%= link_to('Confirm terms', terms_unconfirmed_path, class: 'nav-item') %> <% end %> <% if can? :index, Term %> <%= link_to('Admin', admin_root_path, class: 'nav-item') %> diff --git a/app/views/term/confirm_term.html.erb b/app/views/term/confirm_term.html.erb deleted file mode 100644 index bcc6185..0000000 --- a/app/views/term/confirm_term.html.erb +++ /dev/null @@ -1,19 +0,0 @@ -

Term confirmation

- -
-

<%= @term.phrase %>

-
-

Please put the above term into one of the following categories:

- -

Submit button here

diff --git a/app/views/term/confirm_index.html.erb b/app/views/term/unconfirmed.html.erb similarity index 83% rename from app/views/term/confirm_index.html.erb rename to app/views/term/unconfirmed.html.erb index 9d6edc6..056444e 100644 --- a/app/views/term/confirm_index.html.erb +++ b/app/views/term/unconfirmed.html.erb @@ -7,7 +7,7 @@ take you to the form for submitting this information.

diff --git a/config/routes.rb b/config/routes.rb index 8e10e91..664b6a7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -39,8 +39,10 @@ get '/report/algorithm_metrics', to: 'report#algorithm_metrics' # Confirmation interface - get '/confirm', to: 'term#confirm_index', as: 'confirm_index' - get '/confirm/:id', to: 'term#confirm_term', as: 'confirm_term' + get '/terms/unconfirmed', to: 'term#unconfirmed', as: 'terms_unconfirmed' + resources :terms do + resources :confirmation + end # Defines the root path route ("/") root to: 'static#index' diff --git a/test/controllers/term_controller_test.rb b/test/controllers/term_controller_test.rb index 412f748..a02b758 100644 --- a/test/controllers/term_controller_test.rb +++ b/test/controllers/term_controller_test.rb @@ -4,7 +4,7 @@ class TermControllerTest < ActionDispatch::IntegrationTest test 'confirmation index is not accessible without authentication' do - get confirm_index_path + get terms_unconfirmed_path assert_redirected_to '/' follow_redirect! @@ -14,20 +14,20 @@ class TermControllerTest < ActionDispatch::IntegrationTest test 'confirmation index is accessible to basic users when authenticated' do sign_in users(:basic) - get confirm_index_path + get terms_unconfirmed_path assert_response :success end test 'confirmation index is accessible to admin users when authenticated' do sign_in users(:admin) - get confirm_index_path + get terms_unconfirmed_path assert_response :success end test 'confirmation form is not accessible without authentication' do - get confirm_term_path(terms(:doi)) + get new_term_confirmation_path(terms(:doi)) assert_redirected_to '/' follow_redirect! @@ -37,14 +37,14 @@ class TermControllerTest < ActionDispatch::IntegrationTest test 'confirmation form is accessible to basic users when authenticated' do sign_in users(:basic) - get confirm_term_path(terms(:doi)) + get new_term_confirmation_path(terms(:doi)) assert_response :success end test 'confirmation form is accessible to admin users when authenticated' do sign_in users(:admin) - get confirm_term_path(terms(:doi)) + get new_term_confirmation_path(terms(:doi)) assert_response :success end