Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change draft relationship #444

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
3 changes: 1 addition & 2 deletions app/controllers/application_drafts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 3 additions & 6 deletions app/controllers/application_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions app/models/application_draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ 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 }
validates :application_template, :user, presence: true
validates :application_template, presence: true

def unlocked_for?(user)
locked_by == user
end

def move_question(question_number, direction)
transaction do
Expand Down
24 changes: 13 additions & 11 deletions app/models/application_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ 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
delegate :department, to: :position

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)
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]
Expand All @@ -35,16 +35,18 @@ def create_draft(user)
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
[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
14 changes: 4 additions & 10 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@ 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,
: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 }
Expand Down
16 changes: 13 additions & 3 deletions app/views/application_drafts/edit.haml
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions app/views/application_drafts/show.haml
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
23 changes: 10 additions & 13 deletions app/views/dashboard/_application_templates.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
%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
= 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)
= 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)
6 changes: 2 additions & 4 deletions spec/controllers/application_drafts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 7 additions & 10 deletions spec/controllers/application_templates_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/application_drafts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
FactoryBot.define do
factory :application_draft do
application_template
user
association :locked_by, factory: :user
end
end
26 changes: 4 additions & 22 deletions spec/models/application_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
20 changes: 6 additions & 14 deletions spec/system/application_draft_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions spec/system/application_draft_review_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions spec/system/toggle_active_application_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions spec/system/toggle_unavailability_enabled_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading