Skip to content

Commit

Permalink
Merge pull request #112 from FaxriddinMaxmadiyorov/issue#95
Browse files Browse the repository at this point in the history
Issue#95
  • Loading branch information
XaoGao authored Dec 10, 2023
2 parents 3796206 + 25fb3d0 commit a8e4bcf
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 3 deletions.
11 changes: 9 additions & 2 deletions app/controllers/resumes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
class ResumesController < ApplicationController
before_action :authenticate_user!, only: %i[new]

def index
@resumes = Resume.all
end

def show
@resume = Resume.find(params[:id])
end

def new
@resume = Resume.new
authorize! @resume, with: ResumePolicy
end

def edit
Expand All @@ -13,7 +20,7 @@ def edit

def create
@resume = current_user.resumes.build(resume_params)
if @resume.save?
if @resume.save
redirect_to @resume, notice: t(".created")
else
render :new, alert: resume.errors.full_messages.join(" ")
Expand All @@ -38,6 +45,6 @@ def destroy
private

def resume_params
params.require(:resume).permit(:description)
params.require(:resume).permit(:name, :description, :user_id)
end
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class User < ApplicationRecord

has_many :vacancies, dependent: :destroy
has_many :contacts, dependent: :destroy
has_many :resumes, dependent: :destroy
has_many :sent_notifications, class_name: "Notification",
foreign_key: :sender_id, dependent: :destroy, inverse_of: :sender
has_many :received_notifications, class_name: "Notification",
Expand Down
5 changes: 5 additions & 0 deletions app/policies/resume_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ResumePolicy < ApplicationPolicy
def new?
user.company?
end
end
10 changes: 10 additions & 0 deletions app/views/resumes/_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
= form_with(model: resume, data: { turbo: false }, class: "form-width-avg") do |form|
.form-group.row.mb-15
= form.label :name
= form.text_field :name, class: "form-control"
.form-group.row.mb-15
= form.label :description
= form.rich_text_area :description, class: "form-control"
= form.hidden_field :user_id, value: current_user.id
.form-group.mt-10
= form.submit text, class: "btn btn-outline-success"
Empty file.
8 changes: 8 additions & 0 deletions app/views/resumes/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- if @resume.errors.any?
.
h2 Errors
ul
- @resume.errors.each do |error|
li = error.full_message

= render partial: "form", locals: { resume: @resume, text: "New resume" }
11 changes: 11 additions & 0 deletions app/views/resumes/show.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.container.d-flex.flex-column.align-items-center
h1 = @resume.name
p.text-break = @resume.description

.container
.row
.col.text-center.d-flex.justify-content-center
= button_to "Back", resumes_path, method: :get, class: "btn btn-success mx-2"
- if current_user.id == @resume.user_id
= button_to "Edit", edit_resume_path, method: :get, class: "btn btn-primary mx-2"
= button_to "Delete", resume_path(@resume), method: :delete, data: { "turbo-confirm": "Are you sure you want to delete this resume?" }, class: "btn btn-danger mx-2"
3 changes: 2 additions & 1 deletion spec/factories/resumes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#
FactoryBot.define do
factory :resume do
name { Faker::Lorem.sentence(word_count: 3) }
description { nil }
user { nil }
user
end
end
4 changes: 4 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
role { User.roles.keys.reject { |key| key == "admin" }.sample }
end

trait :company do
role { "company" }
end

trait :individual do
role { %w[applicant moderator admin].sample }
end
Expand Down
17 changes: 17 additions & 0 deletions spec/policies/resume_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "rails_helper"

RSpec.describe ResumePolicy do
let(:user) { build_stubbed(:user, :company) }
let(:record) { build_stubbed(:resume, user:) }

let(:context) { { user: } }

describe_rule :new? do
succeed "when user is company"

failed "when user is admin" do
let(:user) { build_stubbed(:user, :admin) }
let(:record) { build_stubbed(:resume) }
end
end
end

0 comments on commit a8e4bcf

Please sign in to comment.