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

Add supporting document model #3

Merged
merged 8 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion job_tracker/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ gem "bootsnap", require: false
gem 'cssbundling-rails'

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"
gem "image_processing", "~> 1.2"
AidenEscamilla marked this conversation as resolved.
Show resolved Hide resolved

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
Expand Down
8 changes: 8 additions & 0 deletions job_tracker/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ GEM
factory_bot_rails (6.4.3)
factory_bot (~> 6.4)
railties (>= 5.0.0)
ffi (1.16.3)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
importmap-rails (2.0.1)
actionpack (>= 6.0.0)
activesupport (>= 6.0.0)
Expand All @@ -138,6 +142,7 @@ GEM
net-smtp
marcel (1.0.2)
matrix (0.4.2)
mini_magick (4.12.0)
mini_mime (1.1.5)
minitest (5.21.1)
msgpack (1.7.2)
Expand Down Expand Up @@ -232,6 +237,8 @@ GEM
rspec-mocks (~> 3.12)
rspec-support (~> 3.12)
rspec-support (3.12.1)
ruby-vips (2.2.0)
ffi (~> 1.12)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
selenium-webdriver (4.16.0)
Expand Down Expand Up @@ -285,6 +292,7 @@ DEPENDENCIES
cssbundling-rails
debug
factory_bot_rails
image_processing (~> 1.2)
importmap-rails
jbuilder
jsbundling-rails (~> 1.3)
Expand Down
70 changes: 70 additions & 0 deletions job_tracker/app/controllers/supporting_documents_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class SupportingDocumentsController < ApplicationController
before_action :set_supporting_document, only: %i[ show edit update destroy ]

# GET /supporting_documents or /supporting_documents.json
def index
@supporting_documents = SupportingDocument.all
end

# GET /supporting_documents/1 or /supporting_documents/1.json
def show
end

# GET /supporting_documents/new
def new
@supporting_document = SupportingDocument.new
end

# GET /supporting_documents/1/edit
def edit
end

# POST /supporting_documents or /supporting_documents.json
def create
@supporting_document = SupportingDocument.new(supporting_document_params)
AidenEscamilla marked this conversation as resolved.
Show resolved Hide resolved

respond_to do |format|
if @supporting_document.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
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @supporting_document.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /supporting_documents/1 or /supporting_documents/1.json
def update
respond_to do |format|
if @supporting_document.update(supporting_document_params)
AidenEscamilla marked this conversation as resolved.
Show resolved Hide resolved
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
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @supporting_document.errors, status: :unprocessable_entity }
end
end
end

# DELETE /supporting_documents/1 or /supporting_documents/1.json
def destroy
@supporting_document.destroy!

respond_to do |format|
format.html { redirect_to supporting_documents_url, notice: "Supporting document was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_supporting_document
@supporting_document = SupportingDocument.find(params[:id])
end

# Only allow a list of trusted parameters through.
def supporting_document_params
params.require(:supporting_document).permit(:name, :document, :job_application_id)
end
end
2 changes: 2 additions & 0 deletions job_tracker/app/helpers/supporting_documents_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SupportingDocumentsHelper
end
1 change: 1 addition & 0 deletions job_tracker/app/models/job_application.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class JobApplication < ApplicationRecord
belongs_to :employer
has_one :job_description, dependent: :delete
has_many :supporting_documents, dependent: :delete_all

accepts_nested_attributes_for :job_description, update_only: true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def update!(job_application_params:)
@job_description.update(job_application_params[:job_description_attributes])

if job_description_form.valid? && valid?
job_application.update!(job_application_params)
job_application.update!(job_application_params) # be aware of SQL injection here
else
job_application.errors.merge!(errors)
job_application.errors.merge!(job_description_form.errors)
Expand Down
7 changes: 7 additions & 0 deletions job_tracker/app/models/supporting_document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SupportingDocument < ApplicationRecord
belongs_to :job_application
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
2 changes: 1 addition & 1 deletion job_tracker/app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<li> <%= link_to "Job Applications", job_applications_path %> </li>
<li> <%= "Interviews" %> </li>
<li> <%= link_to "Employers", employers_path %> </li>
<li> <%= "Supporting Documents" %> </li>
<li> <%= link_to "Supporting Documents", supporting_documents_path %> </li>
<li> <%= "Job Offers" %> </li>
</ul>
7 changes: 7 additions & 0 deletions job_tracker/app/views/job_applications/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
<li> <%= link_to "Job Description", job_description_path(@job_application.job_description) %> </li>
<li> <%= "Interviews" %> </li>
<li> <%= "Supporting Documents" %> </li>
<div>
<ul>
<% @job_application.supporting_documents.each do |document| %>
<li> <%= link_to "#{document.name}", supporting_document_path(document) %> </li>
<% end %>
</ul>
</div>
<li> <%= "Offer?" %> </li>
</ul>

Expand Down
14 changes: 14 additions & 0 deletions job_tracker/app/views/supporting_documents/_attachment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<% if document.attached? %>
<ul>
<li>
Download: <%= link_to "#{document.filename}", rails_blob_path(document, disposition: 'attachment')%>
</li>
</ul>
<div class="row">
<% if document.image?%>
<div class="message-image-container">
<%= image_tag document.variant(resize_to_limit: [400, 500]), class:"message-image" %>
</div>
<% end %>
</div>
<% end %>
35 changes: 35 additions & 0 deletions job_tracker/app/views/supporting_documents/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<%= form_with(model: supporting_document) do |form| %>
<% if supporting_document.errors.any? %>
<div style="color: red">
<h2><%= pluralize(supporting_document.errors.count, "error") %> prohibited this supporting_document from being saved:</h2>

<ul>
<% supporting_document.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name %>
</div>

<div>
<%= form.label :document, style: "display: block" %>
<%= 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>

<div>
<%= form.submit %>
</div>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div id="<%= dom_id supporting_document %>">
<h2>
<strong>Name:</strong>
<%= supporting_document.name %>
</h2>

<p>
<strong>Job application:</strong>
<%= link_to "#{supporting_document.job_application.job_title}", job_application_path(supporting_document.job_application) %>
</p>


<p>
<strong>Documents:</strong>
</p>

<%= render "supporting_documents/attachment", document: supporting_document.document%>

</div>
10 changes: 10 additions & 0 deletions job_tracker/app/views/supporting_documents/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Editing supporting document</h1>

<%= render "form", supporting_document: @supporting_document %>

<br>

<div>
<%= link_to "Show this supporting document", @supporting_document %> |
<%= link_to "Back to supporting documents", supporting_documents_path %>
</div>
14 changes: 14 additions & 0 deletions job_tracker/app/views/supporting_documents/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>

<h1><%= link_to "Supporting documents", home_index_path %></h1>

<div id="supporting_documents">
<% @supporting_documents.each do |supporting_document| %>
<%= render supporting_document %>
<p>
<%= link_to "Show this supporting document", supporting_document %>
</p>
<% end %>
</div>

<%= link_to "New supporting document", new_supporting_document_path %>
9 changes: 9 additions & 0 deletions job_tracker/app/views/supporting_documents/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>New supporting document</h1>

<%= render "form", supporting_document: @supporting_document %>

<br>

<div>
<%= link_to "Back to supporting documents", supporting_documents_path %>
</div>
10 changes: 10 additions & 0 deletions job_tracker/app/views/supporting_documents/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>

<%= render @supporting_document %>

<div>
<%= link_to "Edit this supporting document", edit_supporting_document_path(@supporting_document) %> |
<%= link_to "Back to supporting documents", supporting_documents_path %>

<%= button_to "Destroy this supporting document", @supporting_document, method: :delete %>
</div>
1 change: 1 addition & 0 deletions job_tracker/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :supporting_documents
resources :job_descriptions, except: [:destroy]
root 'home#index'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[7.0]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :key ], unique: true
end

create_table :active_storage_attachments, id: primary_key_type do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end

create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false

t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end

private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
Loading