refactor code

parent 1c8089a4
Pipeline #1425 canceled with stages
in 0 seconds
......@@ -10,10 +10,8 @@ class FavoriteJobsController < ApplicationController
favorite = favorite_query.find_favorite(job_params, current_user)
if favorite.nil?
FavoriteJob.create(job: @job, user: current_user)
@favorite_exists = true
else
favorite.destroy
@favorite_exists = false
end
respond_to do |format|
format.html { redirect_to new_user_session_path, alert: 'You need to sign in or sign up before continuing.' }
......
......@@ -19,7 +19,7 @@ class JobsController < ApplicationController
def show
@job = Job.find_by(slug: params[:job_slug]) or not_found
check_favorite_exists(@job)
favorite_text(@job)
end
private
......
......@@ -25,11 +25,7 @@ module ApplicationHelper
render file: "#{Rails.root}/public/404.html", status: :not_found
end
def favorite_text
@favorite_exists ? 'Unfavorite' : 'Favorite'
end
def check_favorite_exists(input)
@favorite_exists = !FavoriteJob.find_by(job: input, user: current_user).nil?
def favorite_text(job)
FavoriteJob.find_by(job: job, user: current_user).present? ? 'Unfavorite' : 'Favorite'
end
end
= link_to favorite_text(job), favorite_update_path(job_id: job.id), id: "favorite-#{job.id}", remote: user_signed_in?, class: "btn btn-outline-primary btn-lg"
\ No newline at end of file
| $("#favorite-#{@job.id}").text("
= favorite_text
= favorite_text(@job)
| ");
\ No newline at end of file
......@@ -12,7 +12,6 @@
= paginate @jobs, window: 1
.job-list
- @jobs.each do |job|
= hidden_field :status, check_favorite_exists(job)
.row.bg-white
.col-10.p-3.card-body.text-dark
h5.mb-1
......@@ -29,10 +28,7 @@
p.mb-1
= truncate(job.overview, length: 250)
.col-sm-2.p-3.favourite
-if user_signed_in?
= link_to favorite_text, favorite_update_path(job_id: job.id), id: "favorite-#{job.id}", remote: true, class: "btn btn-outline-primary btn-lg"
-else
= link_to favorite_text, favorite_update_path(job_id: job.id), class: "btn btn-outline-primary btn-lg"
= render partial: "favorite_jobs/favorite", locals: {job: job}
h6.offset-md-4.px
= page_entries_info @jobs
h6.offset-md-4
......
......@@ -51,7 +51,4 @@
.col.text-end
= link_to "Apply this Job", apply_path(job_id: @job.id), class: "btn btn-outline-primary btn-lg"
.col.text-start
-if user_signed_in?
= link_to favorite_text, favorite_update_path(job_id: @job.id), id: "favorite-#{@job.id}", remote: true, class: "btn btn-outline-primary btn-lg"
-else
= link_to favorite_text, favorite_update_path(job_id: @job.id), class: "btn btn-outline-primary btn-lg"
= render partial: "favorite_jobs/favorite", locals: {job: @job}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment