refactor code

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