Skip to content

Commit

Permalink
Add collaborator actions to Admin::Users#edit
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-l-brockwell committed Nov 27, 2024
1 parent 7d273e6 commit dd96fa1
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app/controllers/admin/admins_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
class Admin::AdminsController < Admin::UsersController
before_action :find_resource, except: [:index, :new, :create, :login_as_assessor, :login_as_user]

expose(:collaborators) do
nil
end

def index
params[:search] ||= AdminSearch::DEFAULT_SEARCH
params[:search].permit!
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/admin/assessors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class Admin::AssessorsController < Admin::UsersController
:bulk_deactivate_dt,
]

expose(:collaborators) do
nil
end

def index
params[:search] ||= AssessorSearch::DEFAULT_SEARCH
params[:search].permit!
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/admin/judges_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Admin::JudgesController < Admin::UsersController
expose(:collaborators) do
nil
end

def index
params[:search] ||= JudgeSearch::DEFAULT_SEARCH
params[:search].permit!
Expand Down
46 changes: 46 additions & 0 deletions app/controllers/admin/users/collaborators_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Admin::Users::CollaboratorsController < Admin::BaseController
expose(:user) do
User.find(params[:user_id])
end

expose(:collaborator) do
User.find(params[:collaborator_id])
end

expose(:search_users) do
AdminActions::SearchCollaboratorCandidates.new(existing_collaborators: user.account.users, params: search_params)
end

expose(:add_collaborator_interactor) do
AdminActions::AddCollaborator.new(account: user.account, collaborator:)
end

expose(:candidates) do
search_users.candidates
end

def search
authorize user, :can_add_collaborators_to_account?
search_users.run if search_users.valid?
end

def create
authorize user, :can_add_collaborators_to_account?

add_collaborator_interactor.run.tap do |result|
if result.success?
redirect_to edit_admin_user_path(user), notice: "#{collaborator.email} successfully added to Collaborators!"
else
redirect_to edit_admin_user_path(user), notice: "#{collaborator.email} could not be added to Collaborators!"
end
end
end

private

def search_params
return if params[:search].blank?

params.require(:search).permit(:query)
end
end
4 changes: 4 additions & 0 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class Admin::UsersController < Admin::BaseController
before_action :find_resource, except: [:index, :new, :create]

expose(:collaborators) do
@resource.account.collaborators_without(@resource)
end

def index
params[:search] ||= UserSearch::DEFAULT_SEARCH
params[:search].permit!
Expand Down
2 changes: 1 addition & 1 deletion app/policies/user_policy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class UserPolicy < ApplicationPolicy
%w[index? update? create? show? new?].each do |method|
%w[index? update? create? show? new? can_add_collaborators_to_account?].each do |method|
define_method method do
admin?
end
Expand Down
5 changes: 3 additions & 2 deletions app/views/admin/users/_fields_collaborators.html.slim
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/ TODO collaborators new/delete, also the collaborators variable
- if action_name == "edit"
- collaborators = resource.account.collaborators_without(resource)
- if collaborators.any?
= render "admin/collaborators/list", collaborators: collaborators
- else
Expand All @@ -11,3 +9,6 @@
br
p.p-empty This user has not added any collaborators.
br

- if policy(resource).can_add_collaborators_to_account?
= render "admin/users/collaborators/search_form", resource: resource
27 changes: 27 additions & 0 deletions app/views/admin/users/collaborators/_search_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
= simple_form_for :search,
url: search_admin_user_collaborators_url(resource),
remote: true,
method: :get,
as: nil,
html: { class: "admin-search-collaborators-form" } do |f|

.form-container
label.form-label for="admin-search-collaborators-query" Add collaborator

.alert.alert-danger.hidden.js-admin-search-collaborators-error-box role="alert"

ul.list-unstyled.list-actions.hidden.js-admin-search-collaborators-results-box

.form-block
.row
.col-md-12
= f.input :query,
as: :string,
label: false,
input_html: { class: "form-control", id: "admin-search-collaborators-query" },
wrapper_html: { class: 'pull-left col-md-10 admin-search-collaborators-query' },
placeholder: "Type part of email, first name or last name"

.text-right
= f.submit "Search", class: "btn btn-primary pull-right"
.clear
10 changes: 10 additions & 0 deletions app/views/admin/users/collaborators/_search_results.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- if candidates.present?
- candidates.each do |candidate|
li id="user_#{candidate.id}"
= link_to "#{candidate.full_name} (#{candidate.email})", edit_admin_user_path(candidate)
- if candidate.can_be_added_to_collaborators_to_another_account?
= link_to "Add", admin_user_collaborators_url(collaborator_id: candidate.id), method: :post, class: "pull-right btn btn-default"
- else
br
i.cant_add_to_collaborators_message
| can not be added as linked with another account!
6 changes: 6 additions & 0 deletions app/views/admin/users/collaborators/search.js.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- if search_users.valid?
| $('.js-admin-search-collaborators-results-box').html("#{j render("search_results")}").removeClass("hidden");
| $(".js-admin-search-collaborators-error-box").addClass("hidden");
- else
| $('.js-admin-search-collaborators-results-box').addClass("hidden");
| $(".js-admin-search-collaborators-error-box").text("#{search_users.error.html_safe}").removeClass("hidden");
10 changes: 10 additions & 0 deletions app/views/admin/users/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@
= "Edit #{controller_name == "users" ? "applicant" : controller_name.singularize}"

= render 'form', resource: @resource

- if collaborators
.panel.panel-default[data-controller="element-focus"]
.panel-heading id="section-collaborators-header"
h2.panel-title
a.collapsed data-toggle="collapse" data-parent="#user-form-panel" href="#section-collaborators" aria-expanded="false" aria-controls="section-collaborators" data-element-focus-target="reveal"
' Collaborators
#section-collaborators.section-collaborators.panel-collapse.collapse[aria-labelledby="section-collaborators-header" data-element-scroll-target="accordion"]
.panel-body
= render "fields_collaborators", resource: @resource
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@
patch :unlock
post :scan_via_debounce_api
end

resources :collaborators, only: [:create], module: :users do
get :search, on: :collection
end
end

resources :collaborator_deletion, only: [:destroy]
Expand Down
76 changes: 76 additions & 0 deletions spec/features/admin/users/collaborators_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require "rails_helper"

describe "Collaborators", '
As a an Admin
I want to be able to add collaborators to any account
So that they can collaborate applications
' do
include ActiveJob::TestHelper

let!(:admin) { create(:admin) }
let(:existing_user) { create(:user, :completed_profile) }

before do
login_admin admin
visit edit_admin_user_path(existing_user)
end

describe "Add new Collaborator" do
describe "Invalid Attempts", js: true do
describe "Attempt to add person, which is already associated with another account which has application" do
let!(:user_associated_with_another_account) do
create :user,
:completed_profile,
first_name: "Applicant with account",
role: "account_admin"
end

let!(:another_form_answer) do
create :form_answer,
:innovation,
:submitted,
user: user_associated_with_another_account
end

before do
find("a[aria-controls='section-collaborators']").click

within(".admin-search-collaborators-form") do
fill_in "search[query]", with: "plicant with acc"
first("input[type='submit']").click
end
end

it "can't add" do
within(".js-admin-search-collaborators-results-box") do
expect_to_see(user_associated_with_another_account.first_name)
expect_to_see("can not be added as linked with another account!")
expect(page).to have_no_link("Add")
end
end
end
end

describe "Success Add to Collaborators", js: true do
let(:email) { generate(:email) }
let!(:user) { create(:user, email: email) }

before do
find("a[aria-controls='section-collaborators']").click

within(".admin-search-collaborators-form") do
fill_in "search[query]", with: email.to_s[2..-2]
first("input[type='submit']").click
end
end

it "should add user to collaborators with regular role" do
within(".js-admin-search-collaborators-results-box") do
expect_to_see(user.email)
expect_to_see_no("can not be added as linked with another account!")
expect(page).to have_link("Add")
end
end
end
end
end

0 comments on commit dd96fa1

Please sign in to comment.