Skip to content

Commit

Permalink
Merge pull request #4 from AidenEscamilla/fix_supporting_document_ref…
Browse files Browse the repository at this point in the history
…erence

Fix supporting document reference
  • Loading branch information
AidenEscamilla authored Feb 21, 2024
2 parents 0ca54ed + 6b5f10b commit bcc169d
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 62 deletions.
29 changes: 26 additions & 3 deletions job_tracker/app/controllers/supporting_documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,22 @@ def edit

# POST /supporting_documents or /supporting_documents.json
def create
@supporting_document = SupportingDocument.new(supporting_document_params)
@supporting_document = SupportingDocument.new(
name: supporting_document_params[:name],
document: supporting_document_params[:document])


respond_to do |format|
if @supporting_document.save

valid_ids = supporting_document_params[:job_application_ids].select { |app_id| app_id.blank? == false}
valid_ids.each { |id|
linker = DocumentLinker.new(
job_application_id: id,
supporting_document_id: @supporting_document.id)
linker.save!
}

format.html { redirect_to supporting_document_url(@supporting_document), notice: "Supporting document was successfully created." }
format.json { render :show, status: :created, location: @supporting_document }
else
Expand All @@ -37,7 +49,17 @@ def create
# PATCH/PUT /supporting_documents/1 or /supporting_documents/1.json
def update
respond_to do |format|
if @supporting_document.update(supporting_document_params)
if @supporting_document.update(
name: supporting_document_params[:name],
document: supporting_document_params[:document])

valid_ids = supporting_document_params[:job_application_ids].select { |app_id| app_id.blank? == false}
valid_ids.each { |id|
linker = DocumentLinker.create!(
job_application_id: id,
supporting_document_id: @supporting_document.id)
}

format.html { redirect_to supporting_document_url(@supporting_document), notice: "Supporting document was successfully updated." }
format.json { render :show, status: :ok, location: @supporting_document }
else
Expand Down Expand Up @@ -65,6 +87,7 @@ def set_supporting_document

# Only allow a list of trusted parameters through.
def supporting_document_params
params.require(:supporting_document).permit(:name, :document, :job_application_id)

params.require(:supporting_document).permit(:name, :document, :job_application_ids => [])
end
end
6 changes: 6 additions & 0 deletions job_tracker/app/models/document_linker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class DocumentLinker < ApplicationRecord
belongs_to :job_application
belongs_to :supporting_document

validates :job_application, :supporting_document, presence: true
end
7 changes: 6 additions & 1 deletion job_tracker/app/models/job_application.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
class JobApplication < ApplicationRecord
belongs_to :employer
has_one :job_description, dependent: :delete
has_many :supporting_documents, dependent: :delete_all
has_many :document_linkers, dependent: :destroy
has_many :supporting_documents, :through => :document_linkers

accepts_nested_attributes_for :job_description, update_only: true

enum status: %i[applied rejected interview_scheduled second_round waiting_decision offered_job hired].index_with(&:to_s)

def full_title
"#{employer.name}: #{job_title}"
end

end
5 changes: 4 additions & 1 deletion job_tracker/app/models/supporting_document.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
class SupportingDocument < ApplicationRecord
belongs_to :job_application
has_many :document_linkers, dependent: :destroy
has_many :job_applications, :through => :document_linkers

has_one_attached :document, dependent: :destroy


validates :name, :document, presence: true
validates :name, format: { with: /\A[a-zA-Z\s]+\z/, message: "only allows letters and spaces" }
end
13 changes: 7 additions & 6 deletions job_tracker/app/views/supporting_documents/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
<%= form.file_field :document %>
</div>

<div>
<%= form.label :job_application_id, style: "display: block" %>
<%= form.select :job_application_id,
JobApplication.all.map { |job_app| [ "#{job_app.employer.name}: #{job_app.job_title}", job_app.id ] },
include_blank: true
%>
<div class="form-group">
<%= form.label :job_application_ids, style: "display: block" %>
<%= form.collection_check_boxes(:job_application_ids, JobApplication.all, :id, :full_title) do |box| %>
<li class="list-unstyled">
<%= box.label(:job_application_ids => box.value) { box.check_box + box.text}%>
</li>
<% end %>
</div>

<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
</h2>

<p>
<strong>Job application:</strong>
<%= link_to "#{supporting_document.job_application.job_title}", job_application_path(supporting_document.job_application) %>
</p>
<strong>Job applications:</strong>
<% supporting_document.job_applications.each do |application|%>
<li class="list-unstyled">
<%= link_to "#{application.job_title}", job_application_path(application) %>
</li>
<% end %>
</p>


<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class CreateSupportingDocuments < ActiveRecord::Migration[7.1]
def change
create_table :supporting_documents, id: :uuid do |t|
t.string :name
t.references :job_application, null: false, foreign_key: true, type: :uuid

t.timestamps
end
Expand Down
10 changes: 10 additions & 0 deletions job_tracker/db/migrate/20240220215140_create_document_linkers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateDocumentLinkers < ActiveRecord::Migration[7.1]
def change
create_table :document_linkers, id: :uuid do |t|
t.references :job_application, null: false, foreign_key: true, type: :uuid
t.references :supporting_document, null: false, foreign_key: true, type: :uuid

t.timestamps
end
end
end
16 changes: 12 additions & 4 deletions job_tracker/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions job_tracker/spec/factories/document_linkers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :document_linker do
job_application { nil }
supporting_document { nil }
end
end
3 changes: 2 additions & 1 deletion job_tracker/spec/factories/supporting_documents.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FactoryBot.define do
factory :supporting_document do
name { "" }
job_application { nil }

# TODO: figure out a way to generate these
end
end
8 changes: 4 additions & 4 deletions job_tracker/spec/models/supporting_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
let(:job_application) { create(:job_application, employer: employer) }
let(:supporting_document) {
create(:supporting_document,
job_application: job_application,
name: "Resume",
document: Rack::Test::UploadedFile.new("spec/fixtures/files/resume.pdf", "application/pdf")
)
}
let!(:document_linker) {create(:document_linker, job_application:, supporting_document:)}


describe 'associations' do
context 'belongs_to' do
it "belongs to job_application" do
expect(supporting_document.job_application).to eq(job_application)
context 'has_many' do
it "has a document_linker" do
expect(supporting_document.document_linkers.first).to_not be_nil
end
end

Expand Down
6 changes: 3 additions & 3 deletions job_tracker/spec/requests/supporting_documents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
{
name: "Resume",
document: Rack::Test::UploadedFile.new("spec/fixtures/files/resume.pdf", "application/pdf"),
job_application_id: job_application.id
job_application_ids: [job_application.id]
}
}

let(:invalid_attributes) {
{
name: nil,
document: nil,
job_application_id: nil
job_application_ids: []
}
}

Expand Down Expand Up @@ -103,7 +103,7 @@
{
name: "Cover Letter",
document: Rack::Test::UploadedFile.new("spec/fixtures/files/cover_letter.pdf", "application/pdf"),
job_application_id: job_application.id
job_application_ids: [job_application.id]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
let(:employer) { Employer.create!(name: "Employer") }
let(:job_application) {create(:job_application, employer: employer)}

let(:supporting_document) {
SupportingDocument.create!(
let(:supporting_document) {SupportingDocument.create!(
name: "Resume",
document: Rack::Test::UploadedFile.new("spec/fixtures/files/resume.pdf", "application/pdf"),
job_application: job_application
)
}
document: Rack::Test::UploadedFile.new("spec/fixtures/files/resume.pdf", "application/pdf")
)}

before(:each) do
before do
assign(:supporting_document, supporting_document)
end

Expand All @@ -25,7 +22,7 @@

assert_select "input[name=?]", "supporting_document[document]"

assert_select "select[name=?]", "supporting_document[job_application_id]"
assert_select "input[name=?]", "supporting_document[job_application_ids][]"
end
end
end
24 changes: 11 additions & 13 deletions job_tracker/spec/views/supporting_documents/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@
RSpec.describe "supporting_documents/index", type: :view do
let(:employer) { create(:employer) }
let(:job_application) { create(:job_application, employer: employer) }
let(:supporting_document1) { SupportingDocument.create!(
name: "Resume",
document: Rack::Test::UploadedFile.new("spec/fixtures/files/resume.pdf", "application/pdf")
)}

let(:supporting_document2) { SupportingDocument.create!(
name: "Cover letter",
document: Rack::Test::UploadedFile.new("spec/fixtures/files/cover_letter.pdf", "application/pdf")
)}

before do
assign(:supporting_documents, [
SupportingDocument.create!(
name: "Resume",
document: Rack::Test::UploadedFile.new("spec/fixtures/files/resume.pdf", "application/pdf"),
job_application: job_application
),
SupportingDocument.create!(
name: "Cover letter",
document: Rack::Test::UploadedFile.new("spec/fixtures/files/cover_letter.pdf", "application/pdf"),
job_application: job_application
)
])
assign(:supporting_documents, [supporting_document1, supporting_document2])
end

it "renders a list of supporting_documents" do
render

assert_select "div>h2", text: Regexp.new("Name:\n*+".to_s), count: 2
assert_select "div>p", text: Regexp.new("Job application:\n*+".to_s), count: 2
assert_select "div>p", text: Regexp.new("Job applications:\n*+".to_s), count: 2
assert_select "div>p", text: Regexp.new("Document:*+".to_s), count: 2
end
end
15 changes: 8 additions & 7 deletions job_tracker/spec/views/supporting_documents/new.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
RSpec.describe "supporting_documents/new", type: :view do
let(:employer) { create(:employer) }
let(:job_application) { create(:job_application, employer: employer) }
let(:supporting_document) {SupportingDocument.create!(
name: "Resume",
document: Rack::Test::UploadedFile.new("spec/fixtures/files/resume.pdf", "application/pdf")
)}

before do
assign(:supporting_document, SupportingDocument.new(
name: "Resume",
document: Rack::Test::UploadedFile.new("spec/fixtures/files/resume.pdf", "application/pdf"),
job_application: job_application
))
assign(:supporting_document, supporting_document)
end

it "renders new supporting_document form" do
render

assert_select "form[action=?][method=?]", supporting_documents_path, "post" do
assert_select "form[action=?][method=?]", "#{supporting_documents_path}/#{supporting_document.id}", "post" do

assert_select "input[name=?]", "supporting_document[name]"

assert_select "input[name=?]", "supporting_document[document]"

assert_select "select[name=?]", "supporting_document[job_application_id]"
assert_select "input[name=?]", "supporting_document[job_application_ids][]"
end

end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
RSpec.describe "supporting_documents/show", type: :view do
let(:employer) { Employer.create!(name: "MyString") }
let(:job_application) { JobApplication.create!(employer: employer) }

before(:each) do
assign(:supporting_document, SupportingDocument.create!(
let(:supporting_document) {SupportingDocument.create!(
name: "Resume",
document: Rack::Test::UploadedFile.new("spec/fixtures/files/resume.pdf", "application/pdf"),
job_application: job_application
))
document: Rack::Test::UploadedFile.new("spec/fixtures/files/resume.pdf", "application/pdf")
)}
# let(:document_linker) { DocumentLinker.create!( job_application:,supporting_document:) }

before do
assign(:supporting_document, supporting_document)
end


it "renders attributes in <p>" do
render
expect(rendered).to match(/Name/)
expect(rendered).to match(/Document/)
expect(rendered).to match(/Job application/)
expect(rendered).to match(/Job applications/)
end
end

0 comments on commit bcc169d

Please sign in to comment.