diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index dbd54ffd..44625964 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -10,6 +10,17 @@ module Admin @users = User.all end + # This action allow admins to manually toggle confirmation state of another user + def toggle_confirmation + if user_params[:to_confirm] == 'true' + @user.confirm + elsif user_params[:to_confirm] == 'false' + @user.confirmed_at = nil + @user.save + end + head :ok + end + def show # Variable @show_attributes holds the attributes that are visible for the 'show' action # If you want to change the attributes that are shown in the 'show' action of users @@ -40,7 +51,7 @@ module Admin def user_params params.require(:user).permit(:email, :name, :email_public, :biography, :nickname, :affiliation, :is_admin, :username, :login, :is_disabled, - :tshirt, :mobile, :volunteer_experience, :languages, role_ids: []) + :tshirt, :mobile, :volunteer_experience, :languages, :to_confirm, role_ids: []) end end end diff --git a/app/views/admin/conference/_recent_users.html.haml b/app/views/admin/conference/_recent_users.html.haml index 5bdd0218..1a7a81e2 100644 --- a/app/views/admin/conference/_recent_users.html.haml +++ b/app/views/admin/conference/_recent_users.html.haml @@ -6,7 +6,7 @@ %th # %th E-Mail %th Date registered - %th Status + %th Confirmed? - recent_users.each_with_index do |user, index| %tbody %tr @@ -14,10 +14,12 @@ %td= link_to user.email, admin_user_path(user.id) %td= user.created_at.strftime('%m/%d/%Y') %td - - if user.confirmed? - %span.label.label-success Confirmed - -else - %span.label.label-danger Unconfirmed + = check_box_tag user.id, user.id, user.confirmed?, + method: :patch, + url: "/admin/users/#{user.id}/toggle_confirmation?user[to_confirm]=", + class: 'switch-checkbox', + readonly: true, + data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' } - else %h5.text-warning.text-center No sign ups! diff --git a/app/views/admin/users/_form.html.haml b/app/views/admin/users/_form.html.haml index a2d8cf97..b0074a71 100644 --- a/app/views/admin/users/_form.html.haml +++ b/app/views/admin/users/_form.html.haml @@ -1,5 +1,22 @@ = semantic_form_for [:admin, @user] do |f| = f.inputs 'Basic Information' do + .pull-right + %b + Confirmed? + - if can? :toggle_confirmation, @user + = check_box_tag @user.id, @user.id, @user.confirmed?, + method: :patch, + url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=", + class: 'switch-checkbox', + readonly: false, + data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' } + - else + = check_box_tag @user.id, @user.id, @user.confirmed?, + method: :patch, + url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=", + class: 'switch-checkbox', + readonly: true, + data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' } = f.input :is_admin, hint: 'An admin can create a new conference, manage users and make other users admins.' = f.input :name, :as => :string = f.input :email diff --git a/app/views/admin/users/index.html.haml b/app/views/admin/users/index.html.haml index c3d7b672..eb26d644 100644 --- a/app/views/admin/users/index.html.haml +++ b/app/views/admin/users/index.html.haml @@ -10,7 +10,7 @@ %table.table.table-striped.table-bordered.table-hover.datatable %thead %th ID - %th State + %th Confirmed? %th Email %th Name %th Attended Conferences @@ -23,10 +23,20 @@ %td = user.id %td - - if user.confirmed? - confirmed + - if can? :toggle_confirmation, user + = check_box_tag user.id, user.id, user.confirmed?, + method: :patch, + url: "/admin/users/#{user.id}/toggle_confirmation?user[to_confirm]=", + class: 'switch-checkbox', + readonly: false, + data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' } - else - unconfirmed + = check_box_tag user.id, user.id, user.confirmed?, + method: :patch, + url: "/admin/users/#{user.id}/toggle_confirmation?user[to_confirm]=", + class: 'switch-checkbox', + readonly: true, + data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' } %td = user.email %td diff --git a/app/views/admin/users/show.html.haml b/app/views/admin/users/show.html.haml index 48f2908b..46dfe12a 100644 --- a/app/views/admin/users/show.html.haml +++ b/app/views/admin/users/show.html.haml @@ -14,12 +14,6 @@ - if can? :edit, @user .pull-right = link_to 'Edit', edit_admin_user_path(@user), class: 'btn btn-primary' - - if @user.confirmed? - %span.label.label-success - confirmed - - else - %span.label.label-warning - unconfirmed %table.table - @show_attributes.each do |attr| %tr @@ -32,6 +26,28 @@ - elsif attr == 'biography' %td = markdown(@user.biography) + - elsif attr == 'email' + %td + = @user.send(attr) + %tr + %td + %b + Confirmed? + %td + - if can? :toggle_confirmation, @user + = check_box_tag @user.id, @user.id, @user.confirmed?, + method: :patch, + url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=", + class: 'switch-checkbox', + readonly: false, + data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' } + - else + = check_box_tag @user.id, @user.id, @user.confirmed?, + method: :patch, + url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=", + class: 'switch-checkbox', + readonly: true, + data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' } - else %td= @user.send(attr) #submissions-content.tab-pane{class: "#{'active' if params[:tab] == 'submissions-content'}"} diff --git a/config/routes.rb b/config/routes.rb index fdcf5e99..9a653b27 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,6 +19,11 @@ Osem::Application.routes.draw do namespace :admin do resources :users + resources :users do + member do + patch :toggle_confirmation + end + end resources :comments, only: [:index] resources :conference do resource :contact, except: [:index, :new, :create, :show, :destroy] diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb index 979d0ba1..be9e4cef 100644 --- a/spec/controllers/admin/users_controller_spec.rb +++ b/spec/controllers/admin/users_controller_spec.rb @@ -18,6 +18,19 @@ describe Admin::UsersController do expect(response).to render_template :index end end + describe 'PATCH #toggle_confirmation' do + it 'confirms user' do + user_to_confirm = create(:user, email: 'unconfirmed_user@osem.io', confirmed_at: nil) + patch :toggle_confirmation, id: user_to_confirm.id, user: { to_confirm: 'true' } + user_to_confirm.reload + expect(user_to_confirm.confirmed?).to eq true + end + it 'undo confirmation of user' do + patch :toggle_confirmation, id: user.id, user: { to_confirm: 'false' } + user.reload + expect(user.confirmed?).to eq false + end + end describe 'PATCH #update' do context 'valid attributes' do before :each do