From 6c894f72154e36cc8c2cf06f605d99f3589011bc Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 10:57:29 -0500 Subject: [PATCH 01/14] change belongs_to attribute name to reflect a new relationship type between drafts & people --- app/models/application_draft.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/application_draft.rb b/app/models/application_draft.rb index e5789a4f..95f69309 100644 --- a/app/models/application_draft.rb +++ b/app/models/application_draft.rb @@ -4,7 +4,7 @@ class ApplicationDraft < ApplicationRecord has_many :questions, dependent: :destroy accepts_nested_attributes_for :questions belongs_to :application_template - belongs_to :user + belongs_to :locked_by, class_name: 'User', foreign_key: :user_id delegate :position, to: :application_template validates :application_template, uniqueness: { scope: :user_id } From 94a5baa6d5db761a6f5c93fee1ea02041d044d5a Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 10:59:13 -0500 Subject: [PATCH 02/14] remove unnecessary validations user doesn't need to be validated, belongs_to validates presence by default. application templates will have only one draft going forward --- app/models/application_draft.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/application_draft.rb b/app/models/application_draft.rb index 95f69309..f3c734c0 100644 --- a/app/models/application_draft.rb +++ b/app/models/application_draft.rb @@ -7,8 +7,7 @@ class ApplicationDraft < ApplicationRecord belongs_to :locked_by, class_name: 'User', foreign_key: :user_id delegate :position, to: :application_template - validates :application_template, uniqueness: { scope: :user_id } - validates :application_template, :user, presence: true + validates :application_template, presence: true def move_question(question_number, direction) transaction do From 98cea91c3bcdb24e111f48f5e406bcb34fd3e3c1 Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 10:59:54 -0500 Subject: [PATCH 03/14] application templates can have only one draft --- app/models/application_template.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/models/application_template.rb b/app/models/application_template.rb index 619d7800..b1fff1fd 100644 --- a/app/models/application_template.rb +++ b/app/models/application_template.rb @@ -5,9 +5,8 @@ class ApplicationTemplate < ApplicationRecord friendly_id :department_and_position, use: :slugged has_many :questions, dependent: :destroy - has_many :drafts, class_name: 'ApplicationDraft', - dependent: :destroy, - inverse_of: :application_template + has_one :draft, class_name: 'ApplicationDraft', dependent: :destroy, + inverse_of: :application_template accepts_nested_attributes_for :questions belongs_to :position From 20c74a7ca96c023185b826b9c3dcac171f31036c Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 11:00:24 -0500 Subject: [PATCH 04/14] validate templates only have one draft --- app/models/application_template.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/models/application_template.rb b/app/models/application_template.rb index b1fff1fd..61635646 100644 --- a/app/models/application_template.rb +++ b/app/models/application_template.rb @@ -14,6 +14,7 @@ class ApplicationTemplate < ApplicationRecord validates :position, presence: true, uniqueness: true validates :active, inclusion: { in: [true, false] } + validate :just_one_draft def create_draft(user) return false if draft_belonging_to?(user) @@ -46,4 +47,10 @@ def department_and_position [department.name.parameterize, position.name.parameterize].join('-') end + + def just_one_draft + if ApplicationDraft.where(application_template: self).count > 1 + errors.add(:draft, 'Applications cannot have more than one draft') + end + end end From 03f13f7f9e98f8f3243af44100e739545f59ce3d Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 11:01:00 -0500 Subject: [PATCH 05/14] create draft for that user using new attribute name --- app/models/application_template.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/application_template.rb b/app/models/application_template.rb index 61635646..2ec63017 100644 --- a/app/models/application_template.rb +++ b/app/models/application_template.rb @@ -16,10 +16,10 @@ class ApplicationTemplate < ApplicationRecord validates :active, inclusion: { in: [true, false] } validate :just_one_draft - def create_draft(user) - return false if draft_belonging_to?(user) + def create_draft(locked_by) + return false if draft_belonging_to? locked_by - draft = ApplicationDraft.create user: user, application_template: self + draft = ApplicationDraft.create application_template: self, locked_by: locked_by draft_attributes = draft.attributes.keys template_attributes = attributes.keys excluded = %w[id created_at updated_at] From 7f3c3d3b9f4b7f7dc329ea59633daab14e2202ac Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 11:02:22 -0500 Subject: [PATCH 06/14] reflect that templates will only have one draft when checking in the controller --- app/controllers/application_drafts_controller.rb | 3 +-- app/controllers/application_templates_controller.rb | 9 +++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/app/controllers/application_drafts_controller.rb b/app/controllers/application_drafts_controller.rb index 506b3a17..2c450e8f 100644 --- a/app/controllers/application_drafts_controller.rb +++ b/app/controllers/application_drafts_controller.rb @@ -15,8 +15,7 @@ def edit def new template = ApplicationTemplate.find(params.require :application_template_id) - @draft = template.create_draft(@current_user) || - template.draft_belonging_to(@current_user) + @draft = template.create_draft(@current_user) || template.draft redirect_to edit_draft_path(@draft) end diff --git a/app/controllers/application_templates_controller.rb b/app/controllers/application_templates_controller.rb index 9d1df690..98d9a27e 100644 --- a/app/controllers/application_templates_controller.rb +++ b/app/controllers/application_templates_controller.rb @@ -48,8 +48,7 @@ def toggle_eeo_enabled default: 'EEO data requests disabled on this application.' end if @template.draft_belonging_to? @current_user - draft = @template.draft_belonging_to @current_user - back_path = edit_draft_path(draft) + back_path = edit_draft_path(@template.draft) else back_path = application_path(@template) end @@ -71,8 +70,7 @@ def toggle_unavailability_enabled this application.' end if @template.draft_belonging_to? @current_user - draft = @template.draft_belonging_to @current_user - back_path = edit_draft_path(draft) + back_path = edit_draft_path(@template.draft) else back_path = application_path(@template) end @@ -93,8 +91,7 @@ def toggle_resume_upload_enabled this application.' end if @template.draft_belonging_to? @current_user - draft = @template.draft_belonging_to @current_user - back_path = edit_draft_path(draft) + back_path = edit_draft_path(@template.draft) else back_path = application_path(@template) end From 60d7b6e30e8c8e65bc3787626d7971d67cbc0907 Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 11:03:07 -0500 Subject: [PATCH 07/14] check if the draft belongs to a certain user in a way that reflects the new relationship --- app/models/application_draft.rb | 4 ++++ app/models/application_template.rb | 6 +----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/application_draft.rb b/app/models/application_draft.rb index f3c734c0..b93e7ea7 100644 --- a/app/models/application_draft.rb +++ b/app/models/application_draft.rb @@ -9,6 +9,10 @@ class ApplicationDraft < ApplicationRecord validates :application_template, presence: true + def unlocked_for?(user) + locked_by == user + end + def move_question(question_number, direction) transaction do question = questions.find_by number: question_number diff --git a/app/models/application_template.rb b/app/models/application_template.rb index 2ec63017..e7e52ccb 100644 --- a/app/models/application_template.rb +++ b/app/models/application_template.rb @@ -35,12 +35,8 @@ def create_draft(locked_by) draft end - def draft_belonging_to(user) - drafts.find_by user_id: user.id - end - def draft_belonging_to?(user) - draft_belonging_to(user).present? + draft.present? && draft.unlocked_for?(user) end def department_and_position From c3d47afe250d4a9a49a38b8325c1f4cd980aadaa Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 11:03:37 -0500 Subject: [PATCH 08/14] set up other side of belongs_to relationship --- app/models/user.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/user.rb b/app/models/user.rb index 024b870d..fcbc8e24 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,6 +5,7 @@ class User < ApplicationRecord has_many :application_submissions, dependent: :destroy has_many :subscriptions, dependent: :destroy has_many :positions, through: :subscriptions + has_many :application_drafts, dependent: :nullify validates :email, :first_name, From 893220a3daf0d5ec97b08bcc497229632c2a7d2f Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 11:04:02 -0500 Subject: [PATCH 09/14] put multi-line code on one line --- app/models/user.rb | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index fcbc8e24..3e1ab1c1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,18 +7,11 @@ class User < ApplicationRecord has_many :positions, through: :subscriptions has_many :application_drafts, dependent: :nullify - validates :email, - :first_name, - :last_name, - :spire, - presence: true + validates :email, :first_name, :last_name, :spire, presence: true validates :email, format: { with: /\A([^@\s]+)@((?:[-a-zA-Z0-9]+\.)+[a-zA-Z]{2,})\Z/ } - validates :staff, inclusion: { in: [true, false], - message: 'must be true or false' } - validates :spire, - uniqueness: { case_sensitive: false }, - format: { with: /\A\d{8}@umass\.edu\z/ } + validates :staff, inclusion: { in: [true, false], message: 'must be true or false' } + validates :spire, uniqueness: { case_sensitive: false }, format: { with: /\A\d{8}@umass\.edu\z/ } default_scope { order :last_name, :first_name } scope :staff, -> { where staff: true } From dbb4fc32b1ca967c97fa79d419f5609ca8988f06 Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 11:04:36 -0500 Subject: [PATCH 10/14] don't allow editing of draft unless it's unlocked that is, no one else is currently editing it --- app/views/application_drafts/edit.haml | 16 +++++++++++++--- app/views/application_drafts/show.haml | 4 ++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/views/application_drafts/edit.haml b/app/views/application_drafts/edit.haml index 034dcb3d..3a1d86ac 100644 --- a/app/views/application_drafts/edit.haml +++ b/app/views/application_drafts/edit.haml @@ -1,3 +1,13 @@ -= render 'action_items' -%h1 Editing #{@draft.position.name_and_department} -= render partial: 'form', locals: {draft: @draft} +- if @draft.unlocked_for? @current_user + %p + %i.text-danger + This application is now locked for editing to your user profile. + No one else may edit it until you save and publish the application. + = render 'action_items' + %h1 Editing #{@draft.position.name_and_department} + = render partial: 'form', locals: {draft: @draft} +- else + %p + %i.text-danger + This application is locked for editing by #{@draft.locked_by.full_name}. + No one else may edit it until they save and publish, or discard their draft. diff --git a/app/views/application_drafts/show.haml b/app/views/application_drafts/show.haml index c4e33c62..d22233be 100644 --- a/app/views/application_drafts/show.haml +++ b/app/views/application_drafts/show.haml @@ -1,4 +1,8 @@ %i This is a preview of your changes. This form, as shown here, is not live. +%p +%i.text-danger + This application is now locked for editing to your user profile. + No one else may edit it until you save and publish the application. %h1 Application for #{@draft.position.name_and_department} - if @draft.email.present? From eb1be8a5be539b35c2d0f5e3fb851ec857fd814f Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 11:04:49 -0500 Subject: [PATCH 11/14] multi-line code to one line --- app/views/dashboard/_application_templates.haml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/views/dashboard/_application_templates.haml b/app/views/dashboard/_application_templates.haml index 41363d8c..af173e21 100644 --- a/app/views/dashboard/_application_templates.haml +++ b/app/views/dashboard/_application_templates.haml @@ -7,8 +7,7 @@ %ul - if @templates.key? position - template = @templates[position].first - %li= link_to 'View application', - application_path(template) + %li= link_to 'View application', application_path(template) %li - if template.draft_belonging_to? @current_user - draft = template.draft_belonging_to @current_user From 9f090288cd050ce6af4cfe5b850e70247a5bb375 Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 11:05:42 -0500 Subject: [PATCH 12/14] only show edit/discard buttons if the draft is unlocked --- app/views/dashboard/_application_templates.haml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/views/dashboard/_application_templates.haml b/app/views/dashboard/_application_templates.haml index af173e21..5fd977a3 100644 --- a/app/views/dashboard/_application_templates.haml +++ b/app/views/dashboard/_application_templates.haml @@ -9,13 +9,13 @@ - template = @templates[position].first %li= link_to 'View application', application_path(template) %li - - if template.draft_belonging_to? @current_user - - draft = template.draft_belonging_to @current_user - = link_to 'Resume editing saved draft', - edit_draft_path(draft) - = button_to 'Discard saved draft', - draft_path(draft), method: :delete, - class: 'btn btn-secondary' + - if template.draft.present? + - if template.draft.unlocked_for? @current_user + = link_to 'Resume editing saved draft', edit_draft_path(template.draft) + = button_to 'Discard saved draft', draft_path(template.draft), method: :delete, + class: 'btn btn-secondary' + - else + This application is currently being edited by #{template.draft.locked_by.full_name} - else = link_to 'Edit application', new_draft_path(application_template_id: template.id) From 7c0227446840fbf73740244337a6f2e78046f248 Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 11:06:04 -0500 Subject: [PATCH 13/14] multi-line code to one line --- app/views/dashboard/_application_templates.haml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/views/dashboard/_application_templates.haml b/app/views/dashboard/_application_templates.haml index 5fd977a3..16b9e2e5 100644 --- a/app/views/dashboard/_application_templates.haml +++ b/app/views/dashboard/_application_templates.haml @@ -17,8 +17,6 @@ - else This application is currently being edited by #{template.draft.locked_by.full_name} - else - = link_to 'Edit application', - new_draft_path(application_template_id: template.id) + = link_to 'Edit application', new_draft_path(application_template_id: template.id) - else # position has no template... yet - %li= link_to 'Create application', - new_application_path(position_id: position.id) + %li= link_to 'Create application', new_application_path(position_id: position.id) From 20dfc8b0b2d82f2d29cbb297e8e070da2fe7d8f1 Mon Sep 17 00:00:00 2001 From: Anbranin Date: Fri, 26 Nov 2021 11:07:12 -0500 Subject: [PATCH 14/14] update tests --- .../application_drafts_controller_spec.rb | 6 +- .../application_templates_controller_spec.rb | 17 ++--- spec/factories/application_drafts.rb | 2 +- spec/models/application_template_spec.rb | 26 ++------ spec/system/application_draft_edit_spec.rb | 20 ++---- spec/system/application_draft_review_spec.rb | 6 +- ...toggle_active_application_template_spec.rb | 5 +- .../toggle_unavailability_enabled_spec.rb | 5 +- .../viewing_application_templates_spec.rb | 65 ++++++++++++------- 9 files changed, 69 insertions(+), 83 deletions(-) diff --git a/spec/controllers/application_drafts_controller_spec.rb b/spec/controllers/application_drafts_controller_spec.rb index 9373a00b..2e84a77c 100644 --- a/spec/controllers/application_drafts_controller_spec.rb +++ b/spec/controllers/application_drafts_controller_spec.rb @@ -83,14 +83,12 @@ context 'no pre-existing draft' do it 'creates a draft for the correct application template' do expect { submit } - .to change { @template.drafts.count } - .by 1 + .to change { ApplicationDraft.where(application_template: @template).count }.by 1 end end context 'pre-existing draft' do it 'finds the pre-existing draft' do - draft = create :application_draft, - application_template: @template, user: @user + draft = create :application_draft, application_template: @template, locked_by: @user submit expect(assigns.fetch :draft).to eql draft end diff --git a/spec/controllers/application_templates_controller_spec.rb b/spec/controllers/application_templates_controller_spec.rb index e75b3267..cbb0d395 100644 --- a/spec/controllers/application_templates_controller_spec.rb +++ b/spec/controllers/application_templates_controller_spec.rb @@ -26,7 +26,7 @@ it 'creates a draft for that application template for the current user' do submit draft = assigns.fetch :draft - expect(draft.user).to eql @user + expect(draft.locked_by).to eql @user end it 'assigns the created draft to a draft variable' do draft = create :application_draft @@ -177,10 +177,9 @@ context 'draft belonging to current user' do it 'redirects to the edit path' do @template.create_draft @user + @template.reload submit - expect(response).to redirect_to( - edit_draft_path(@template.draft_belonging_to @user) - ) + expect(response).to redirect_to(edit_draft_path(@template.draft)) end end context 'no draft belonging to current user' do @@ -244,10 +243,9 @@ context 'draft belonging to current user' do it 'redirects to the edit path' do @template.create_draft @user + @template.reload submit - expect(response).to redirect_to( - edit_draft_path(@template.draft_belonging_to @user) - ) + expect(response).to redirect_to(edit_draft_path(@template.draft)) end end context 'no draft belonging to current user' do @@ -311,10 +309,9 @@ context 'draft belonging to current user' do it 'redirects to the edit path' do @template.create_draft @user + @template.reload submit - expect(response).to redirect_to( - edit_draft_path(@template.draft_belonging_to @user) - ) + expect(response).to redirect_to(edit_draft_path(@template.draft)) end end context 'no draft belonging to current user' do diff --git a/spec/factories/application_drafts.rb b/spec/factories/application_drafts.rb index 48bb05c4..4c7f6bdf 100644 --- a/spec/factories/application_drafts.rb +++ b/spec/factories/application_drafts.rb @@ -3,6 +3,6 @@ FactoryBot.define do factory :application_draft do application_template - user + association :locked_by, factory: :user end end diff --git a/spec/models/application_template_spec.rb b/spec/models/application_template_spec.rb index 18f68aa8..578d7a35 100644 --- a/spec/models/application_template_spec.rb +++ b/spec/models/application_template_spec.rb @@ -17,7 +17,7 @@ before :each do create :application_draft, application_template: @application_template, - user: @user + locked_by: @user end it 'returns false' do expect(call).to be false @@ -30,8 +30,8 @@ it 'returns the draft' do expect(call).to be_a ApplicationDraft end - it 'sets the user of the draft to the user argument' do - expect(call.user).to eql @user + it 'sets the locked_by aspect of the draft to the user argument' do + expect(call.locked_by).to eql @user end it 'sets the email of the draft to be same as template email' do expect(call.email).to eql @application_template.email @@ -44,23 +44,6 @@ end end end - describe 'draft_belonging_to' do - before :each do - @user = create :user - other_user = create :user - @application_template = create :application_template - @draft = create :application_draft, - application_template: @application_template, - user: @user - # other draft - create :application_draft, - application_template: @application_template, - user: other_user - end - it 'returns the application template draft belonging to the user' do - expect(@application_template.draft_belonging_to @user).to eql @draft - end - end describe 'draft_belonging_to?' do before :each do @@ -74,8 +57,7 @@ expect(call).to be false end it 'returns true if a draft does exist for the user in question' do - create :application_draft, user: @user, - application_template: @application_template + create :application_draft, locked_by: @user, application_template: @application_template expect(call).to be true end end diff --git a/spec/system/application_draft_edit_spec.rb b/spec/system/application_draft_edit_spec.rb index f0d906cc..b53d6862 100644 --- a/spec/system/application_draft_edit_spec.rb +++ b/spec/system/application_draft_edit_spec.rb @@ -3,21 +3,13 @@ require 'rails_helper' describe 'editing application draft' do - let!(:draft) { create :application_draft } - let!(:top_question) do - create :question, number: 1, - application_draft: draft - end - let!(:middle_question) do - create :question, number: 2, - application_draft: draft - end - let!(:bottom_question) do - create :question, number: 3, - application_draft: draft - end + let(:user) { create :user, staff: true } + let!(:draft) { create :application_draft, locked_by: user } + let!(:top_question) { create :question, number: 1, application_draft: draft } + let!(:middle_question) { create :question, number: 2, application_draft: draft } + let!(:bottom_question) { create :question, number: 3, application_draft: draft } before :each do - when_current_user_is :staff + when_current_user_is user visit edit_draft_path(draft) end diff --git a/spec/system/application_draft_review_spec.rb b/spec/system/application_draft_review_spec.rb index 28d018a4..ee3c615f 100644 --- a/spec/system/application_draft_review_spec.rb +++ b/spec/system/application_draft_review_spec.rb @@ -21,13 +21,13 @@ it 'has a button to save the application' do click_button('Save application') expect(template.reload.questions).to include question - expect(template.drafts).to be_empty + expect(template.draft).to be_nil end it 'has a button to discard changes' do - expect(template.drafts.first).to eql draft + expect(template.draft).to eql draft click_button('Discard changes') template.reload - expect(template.drafts).to be_empty + expect(template.draft).to be_nil expect(page.current_path).to eql staff_dashboard_path end it 'has a disabled submit button' do diff --git a/spec/system/toggle_active_application_template_spec.rb b/spec/system/toggle_active_application_template_spec.rb index 581306b7..000ec53d 100644 --- a/spec/system/toggle_active_application_template_spec.rb +++ b/spec/system/toggle_active_application_template_spec.rb @@ -3,10 +3,11 @@ require 'rails_helper' describe 'toggling the active attribute of application templates' do + let(:user) { create :user, staff: true } let(:application) { create :application_template } - let(:draft) { create :application_draft, application_template: application } + let(:draft) { create :application_draft, application_template: application, locked_by: user } before :each do - when_current_user_is :staff + when_current_user_is user end context 'on application template page' do before :each do diff --git a/spec/system/toggle_unavailability_enabled_spec.rb b/spec/system/toggle_unavailability_enabled_spec.rb index d7bf34f4..d29f9054 100644 --- a/spec/system/toggle_unavailability_enabled_spec.rb +++ b/spec/system/toggle_unavailability_enabled_spec.rb @@ -3,10 +3,11 @@ require 'rails_helper' describe 'toggle the unavailability_enabled attribute of templates' do + let(:user) { create :user, staff: true } let(:application) { create :application_template } - let(:draft) { create :application_draft, application_template: application } + let(:draft) { create :application_draft, application_template: application, locked_by: user } before :each do - when_current_user_is :staff + when_current_user_is user end context 'on editing application drafts page' do before :each do diff --git a/spec/system/viewing_application_templates_spec.rb b/spec/system/viewing_application_templates_spec.rb index 8845840a..284f4022 100644 --- a/spec/system/viewing_application_templates_spec.rb +++ b/spec/system/viewing_application_templates_spec.rb @@ -8,46 +8,61 @@ let!(:app_with_draft) { create :application_template } let!(:app_without_draft) { create :application_template } let!(:draft) do - create :application_draft, - application_template: app_with_draft, - user: user + create :application_draft, application_template: app_with_draft, locked_by: user end before :each do when_current_user_is user visit staff_dashboard_path end it 'has links to view the applications' do - click_link 'View application', - href: application_path(app_with_draft) - expect(page.current_path) - .to eql application_path(app_with_draft) + click_link 'View application', href: application_path(app_with_draft) + expect(page.current_path).to eql application_path(app_with_draft) visit staff_dashboard_path - click_link 'View application', - href: application_path(app_without_draft) - expect(page.current_path) - .to eql application_path(app_without_draft) + click_link 'View application', href: application_path(app_without_draft) + expect(page.current_path).to eql application_path(app_without_draft) end it 'has a link to edit any drafts owned by the current user' do - click_link 'Resume editing saved draft', - href: edit_draft_path(draft) - expect(page.current_path) - .to eql edit_draft_path(draft) + click_link 'Resume editing saved draft', href: edit_draft_path(draft) + expect(page.current_path).to eql edit_draft_path(draft) end it 'has links to edit the applications when a draft does not yet exist' do action_path = new_draft_path(application_template_id: app_without_draft.id) - click_link 'Edit application', - href: action_path - expect(page.current_path) - .to eql edit_draft_path(ApplicationDraft.all.last.id) + click_link 'Edit application', href: action_path + expect(page.current_path).to eql edit_draft_path(ApplicationDraft.all.last.id) end it 'has a link to create an application if one does not exist' do - click_link 'Create application', - href: new_application_path(position_id: position.id) - expect(page.current_path) - .to eql edit_draft_path(ApplicationDraft.all.last.id) + click_link 'Create application', href: new_application_path(position_id: position.id) + expect(page.current_path).to eql edit_draft_path(ApplicationDraft.all.last.id) end it 'has a button to discard drafts' do - expect { click_button 'Discard saved draft' } - .to change { ApplicationDraft.count }.by(-1) + expect { click_button 'Discard saved draft' }.to change { ApplicationDraft.count }.by(-1) + end + context 'logged in as a user the draft does not belong to' do + let(:another_user) { create :user, staff: true } + before do + when_current_user_is another_user + visit staff_dashboard_path + end + it 'has links to view the applications' do + click_link 'View application', href: application_path(app_with_draft) + expect(page.current_path).to eql application_path(app_with_draft) + visit staff_dashboard_path + click_link 'View application', href: application_path(app_without_draft) + expect(page.current_path).to eql application_path(app_without_draft) + end + it 'does not have a link to edit the draft' do + expect(page).not_to have_link 'Resume editing saved draft', href: edit_draft_path(draft) + end + it 'shows that the application is locked for editing' do + locked_text = "This application is currently being edited by #{draft.locked_by.full_name}" + expect(page).to have_text locked_text + end + it 'has a link to create an application if one does not exist' do + click_link 'Create application', href: new_application_path(position_id: position.id) + expect(page.current_path).to eql edit_draft_path(ApplicationDraft.all.last.id) + end + it 'does not have a button to discard drafts' do + expect(page).to_not have_button 'Discard saved draft' + end end end