Change logic code and fix mentor's comments

parent d91084a9
Pipeline #1066 canceled with stages
in 0 seconds
class FavoriteJobsController < ApplicationController class FavoriteJobsController < ApplicationController
before_action :sign_in_favorite_validation, only: %i[create destroy] before_action :sign_in_validation, only: %i[create destroy index]
before_action :sign_in_validation, only: [:index]
def index def index
if params[:page].present? || params[:page] == ""
return redirect_to favorite_jobs_path, flash: {warning: Settings.user.favorite_job.validate_page } if params[:page].to_i <= 0 || params[:page].scan(/\D+/).present?
end
@count = current_user.favorite_jobs.count @count = current_user.favorite_jobs.count
@favorited_jobs = current_user.favorite_jobs.order_favorite.page(params[:page]).per(Job::LIMIT_PAGE) @favorited_jobs = current_user.favorite_jobs.order_favorite.page(params[:page]).per(Job::LIMIT_PAGE)
end end
def create def create
@job = Job.find_by_id(params[:job_id])
return if current_user.favorite_jobs.exists?(job_id: params[:job_id]) return if current_user.favorite_jobs.exists?(job_id: params[:job_id])
@follow = current_user.favorite!(params[:job_id]) @follow = current_user.favorite!(params[:job_id])
respond_to { |format| format.js } respond_to { |format| format.js }
end end
def destroy def destroy
@job = Job.find_by_id(params[:job_id])
@unfollow = current_user.unfavorite!(params[:job_id]) @unfollow = current_user.unfavorite!(params[:job_id])
respond_to { |format| format.js }
if request.referer.include?("favorite") if request.referer.include?("favorite")
@count = current_user.favorite_jobs.count @count = current_user.favorite_jobs.count
...@@ -31,17 +29,12 @@ class FavoriteJobsController < ApplicationController ...@@ -31,17 +29,12 @@ class FavoriteJobsController < ApplicationController
return redirect_to request.referer if count_on_page == Job::DIVISIBLE return redirect_to request.referer if count_on_page == Job::DIVISIBLE
end end
respond_to { |format| format.js }
end end
private private
def sign_in_favorite_validation
return if signed_in?
session[:return_to] = request.referer
flash[:warning] = Settings.user.warning_signin
redirect_to login_path
end
def sign_in_validation def sign_in_validation
return if signed_in? return if signed_in?
store_location store_location
......
class HistoryJobsController < ApplicationController class HistoryJobsController < ApplicationController
before_action :sign_in_favorite_validation, only: [:destroy]
before_action :sign_in_validation, only: [:index] before_action :sign_in_validation, only: [:index]
def index def index
...@@ -9,13 +8,6 @@ class HistoryJobsController < ApplicationController ...@@ -9,13 +8,6 @@ class HistoryJobsController < ApplicationController
private private
def sign_in_favorite_validation
return if signed_in?
session[:return_to] = request.referer
flash[:warning] = Settings.user.warning_signin
redirect_to login_path
end
def sign_in_validation def sign_in_validation
return if signed_in? return if signed_in?
store_location store_location
......
...@@ -30,15 +30,15 @@ class JobsController < ApplicationController ...@@ -30,15 +30,15 @@ class JobsController < ApplicationController
session.delete(:job_applied) session.delete(:job_applied)
return redirect_to jobs_path unless @job return redirect_to jobs_path unless @job
if signed_in? if signed_in?
@user = JobApplied.where(user_id: current_user.id, job_id: params[:id]) @is_job_applied = current_user.job_applieds.pluck(:job_id).include?@job.id
history_jobs = current_user.histories.order_history
history_jobs.last.destroy if history_jobs.count >= Job::LIMIT_HISTORY
founded_history = current_user.histories.find_by(job_id: params[:id]) founded_history = current_user.histories.find_by(job_id: params[:id])
return founded_history.update(updated_at: Time.current.utc) if founded_history.present? return founded_history.update(updated_at: Time.current.utc) if founded_history.present?
history = current_user.histories.create!(job_id: params[:id]) history = current_user.histories.create!(job_id: params[:id])
history_jobs = current_user.histories.order_history
history_jobs.last.destroy if history_jobs.count >= Job::LIMIT_HISTORY
end end
end end
......
...@@ -33,6 +33,7 @@ module SessionsHelper ...@@ -33,6 +33,7 @@ module SessionsHelper
end end
def store_location def store_location
return session[:return_to] = request.referer if request.url.include?("favorite_job")
session[:return_to] = request.url if request.get? session[:return_to] = request.url if request.get?
end end
end end
...@@ -27,6 +27,10 @@ class Job < ApplicationRecord ...@@ -27,6 +27,10 @@ class Job < ApplicationRecord
company&.name company&.name
end end
def converted_company_name
company&.converted_name
end
def format_desc def format_desc
description.truncate_words(250) description.truncate_words(250)
end end
......
...@@ -29,11 +29,11 @@ class User < ApplicationRecord ...@@ -29,11 +29,11 @@ class User < ApplicationRecord
end end
def unfavorite!(job_id) def unfavorite!(job_id)
favorite_jobs.find_by(job_id: job_id).destroy favorite_jobs.find_by(job_id: job_id)&.destroy
end end
def remove_history!(job_id) def remove_history!(job_id)
histories.find_by(job_id: job_id).destroy histories.find_by(job_id: job_id)&.destroy
end end
def self.new_remember_token def self.new_remember_token
......
$("#favorite-form-<%= @job.id %>").html("<%=escape_javascript render 'users/unfavorite', job_id: @job.id %>"); $("#favorite-form-<%= @follow.job_id %>").html("<%=escape_javascript render 'users/unfavorite', job_id: @follow.job_id %>");
$("#favorite-form-<%= @job.id %>").html("<%= j render 'users/favorite', job_id: @job.id %>"); $("#favorite-form-<%= @unfollow.job_id %>").html("<%= j render 'users/favorite', job_id: @unfollow.job_id %>");
$(".job-id-<%= @job.id %>").remove(); $(".job-id-<%= @unfollow.job_id %>").remove();
$(".count-favorite-jobs").html("<%= @count %>"); $(".count-favorite-jobs").html("<%= @count %>");
<div class="container"> <div class="container">
<%= render 'layouts/flash' %>
<% if @favorited_jobs.count > 0 %> <% if @favorited_jobs.count > 0 %>
<h1 class="text-center my-job-label">Favorite Job (<span class="count-favorite-jobs"><%= @count %></span> jobs)</h1> <h1 class="text-center my-job-label">Favorite Job (<span class="count-favorite-jobs"><%= @count %></span> jobs)</h1>
<%= paginate @favorited_jobs, outer_window: 2, window: 1 %> <%= paginate @favorited_jobs, outer_window: 2, window: 1 %>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<div class="job-info"> <div class="job-info">
<div><strong><%= @job.title %></strong></div> <div><strong><%= @job.title %></strong></div>
<div> <div>
<%= link_to @job.company_name, company_jobs_path(converted_name: @job.company.converted_name), class: 'company' %> <%= link_to @job.company_name, company_jobs_path(converted_name: @job.converted_company_name), class: 'company' %>
</div> </div>
<div class="breadcrumb"> <div class="breadcrumb">
<%= link_to "TOP", root_path %>&ensp;/&ensp; <%= link_to "TOP", root_path %>&ensp;/&ensp;
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
<% end %>/&ensp; <% end %>/&ensp;
<%= @job.title.truncate_words(5) %> <%= @job.title.truncate_words(5) %>
</div> </div>
<% if signed_in? && @user.present? %> <% if @is_job_applied %>
<div class="apply-job"> <div class="apply-job">
<strong>Applied</strong> <strong>Applied</strong>
</div> </div>
......
<%= form_for(:job_unfavorite, url: unfavorite_job_path(job_id: job_id), method: :delete, remote: true) do |f| %> <%= form_for(:job_unfavorite, url: unfavorite_job_path(job_id: job_id), method: :delete, remote: true, data: { disable_with: false }) do |f| %>
<%= f.submit "Unfavorite", class: "btn btn-outline-danger", id: "button-unfavorite" %> <%= f.submit "Unfavorite", class: "btn btn-outline-danger", id: "button-unfavorite" %>
<% end %> <% end %>
...@@ -21,6 +21,9 @@ user: ...@@ -21,6 +21,9 @@ user:
applied_job: applied_job:
applied: 'You applied for this job' applied: 'You applied for this job'
favorite_job:
validate_page: 'Params Page format failed'
email: email:
confirmation: 'Welcome To VeNJOB! Confirm Your Email' confirmation: 'Welcome To VeNJOB! Confirm Your Email'
reset_password: 'VeNJOB Password Assistance' reset_password: 'VeNJOB Password Assistance'
......
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