Commit b07006ff by Trịnh Hoàng Phúc

Add table History, fix review 19/05/2020

parent 475139e9
Pipeline #632 failed with stages
in 0 seconds
......@@ -25,7 +25,7 @@
},
error: (err) => {
if(err.status === 401){
if(confirm(err.responseJSON.message)){
if(confirm(err.responseText)){
window.location.href = "/users/sign_in";
}
}
......@@ -55,7 +55,7 @@
},
error: (err) => {
if(err.status === 401){
if(confirm(err.responseJSON.message)){
if(confirm(err.responseText)){
window.location.href = "/users/sign_in";
}
}
......@@ -78,7 +78,7 @@
},
error: (err) => {
if(err.status === 401){
if(confirm(err.responseJSON.message)){
if(confirm(err.responseText)){
window.location.href = "/users/sign_in";
}
}
......
class AdminsController < ApplicationController
before_action :authenticate_admin!
def applies
@cities = City.order("title ASC").all
@industries = Industry.order("title ASC").all
......
class HistoryController < ApplicationController
def index
return redirect_to jobs_path if cookies[:job_ids_history].blank?
if user_signed_in?
@jobs_history = History.includes(:job, :user).where("user_id = #{current_user.id}")
else
arr_job_ids = cookies[:job_ids_history].split(",")
job_ids = cookies[:job_ids_history].split(",")
@jobs_history = Job.where("id IN (?)", job_ids).order("id DESC")
end
arr_job_ids = cookies[:job_ids_history].split(",")
job_ids = cookies[:job_ids_history].split(",")
@jobs_history = Job.where("id IN (?)", job_ids).order("id DESC")
end
end
\ No newline at end of file
......@@ -17,26 +17,24 @@ class JobsController < ApplicationController
def show
@job = Job.find_by(id: params[:id])
return redirect_to page_404_path if @job.blank?
if user_signed_in?
@apply = Apply.find_by(job_id: params[:id], user_id: current_user.id)
@favorite = Favorite.find_by(job_id: params[:id], user_id: current_user.id)
end
@relate_jobs = Job.related_jobs(@job.company_id, @job.id)
if cookies[:job_ids_history].nil?
cookies[:job_ids_history] = { value: @job.id, expires: Time.now + 3600 }
History.create_history(params[:id], current_user.id)
else
arr_job_ids = cookies[:job_ids_history].split(",")
unless arr_job_ids.include? (@job.id.to_s)
arr_job_ids.shift if arr_job_ids.count == 10
arr_job_ids << @job.id.to_s
if cookies[:job_ids_history].blank?
cookies[:job_ids_history] = { value: @job.id, expires: Time.now + 3600 }
else
arr_job_ids = cookies[:job_ids_history].split(",")
unless arr_job_ids.include? (@job.id.to_s)
arr_job_ids.shift if arr_job_ids.count == 10
arr_job_ids << @job.id.to_s
end
cookies[:job_ids_history] = { value: arr_job_ids.join(","), expires: Time.now + 3600 }
end
cookies[:job_ids_history] = { value: arr_job_ids.join(","), expires: Time.now + 3600 }
end
@relate_jobs = Job.related_jobs(@job.company_id, @job.id)
end
def search
......
class UsersController < ApplicationController
before_action :authenticate_user!
def info
redirect_to user_session_path unless user_signed_in?
end
def apply_jobs
redirect_to user_session_path unless user_signed_in?
@applied_jobs = Apply.where("user_id = #{current_user.id}")
end
def favorite_jobs
redirect_to user_session_path unless user_signed_in?
@favorite_jobs = Favorite.where("user_id = #{current_user.id}")
end
def apply_or_favorite_jobs
unless user_signed_in?
payload = {
message: "You are not logged in, please login to operate the feature",
}
render :json => payload, :status => :unauthorized
else
if params[:job_ids].present?
params[:job_ids].each do |id|
if params[:type] == "apply"
Apply.find_or_create_by({:user_id => current_user.id, :job_id => id})
else
Favorite.find_or_create_by({:user_id => current_user.id, :job_id => id})
end
if params[:job_ids].present?
params[:job_ids].each do |id|
if params[:type] == "apply"
Apply.find_or_create_by({:user_id => current_user.id, :job_id => id})
else
Favorite.find_or_create_by({:user_id => current_user.id, :job_id => id})
end
payload = {
message: "Thank you for #{params[:type] == "apply" ? "applied" : "favourited"} with VenJob",
}
render :json => payload, :status => :ok
end
payload = {
message: "Thank you for #{params[:type] == "apply" ? "applied" : "favourited"} with VenJob",
}
render :json => payload, :status => :ok
end
end
def remove_favorite_job
unless user_signed_in?
if params[:favorite_id].present?
Favorite.find_by(id: params[:favorite_id]).destroy
payload = {
message: "You are not logged in, please login to operate the feature",
message: "Remove successfully"
}
render :json => payload, :status => :unauthorized
else
if params[:favorite_id].present?
Favorite.find_by(id: params[:favorite_id]).destroy
payload = {
message: "Remove successfully"
}
render :json => payload, :status => :ok
end
render :json => payload, :status => :ok
end
end
end
class History < ApplicationRecord
belongs_to :user
belongs_to :job
def self.create_history(job_id, user_id)
return History.find_or_create_by({ job_id: job_id, user_id: user_id })
end
end
......@@ -9,7 +9,8 @@ class Job < ApplicationRecord
has_many :applies
has_many :users, through: :applies
has_many :histories
has_many :users, through: :histories
has_many :favorites
has_many :users, through: :favorites
......
......@@ -8,6 +8,8 @@ class User < ApplicationRecord
has_many :jobs, through: :applies
has_many :favorites
has_many :jobs, through: :favorites
has_many :histories
has_many :jobs, through: :histories
mount_uploader :cv, CvUploader
......
......@@ -20,27 +20,7 @@
</tr>
</thead>
<tbody>
<% @jobs_history.each_with_index do |job, index| %>
<tr class="woocommerce-cart-form__cart-item cart_item">
<td>
<%= index + 1%>
</td>
<td style="text-align: center;">
<input type="checkbox" class="checkbox-apply-job" data-id="<%= job.id %>">
</td>
<td>
<%= link_to job.title, job %>
</td>
<td>
<% job.cities.each_with_index do |city, index| %>
<%= city.title %><%= ", " if index != job.cities.size - 1 %>
<% end %>
</td>
<td class="product-price">
<%= job.salary %>
</td>
</tr>
<% end %>
<%= render partial: (user_signed_in? ? '/shared/history_item_has_user' : '/shared/history_item_has_not_user'), collection: @jobs_history, as: :history_item %>
<tr>
<td colspan="6" class="actions">
<a class="button apply-all-job" href="javascript:void(0)">Apply all</a>
......
<tr class="woocommerce-cart-form__cart-item cart_item">
<td>
<%= history_item_counter + 1 %>
</td>
<td style="text-align: center;">
<input type="checkbox" class="checkbox-apply-job" data-id="<%= history_item.id %>">
</td>
<td>
<%= link_to history_item.title, history_item %>
</td>
<td>
<% history_item.cities.each_with_index do |city, index| %>
<%= city.title %><%= ", " if index != history_item.cities.size - 1 %>
<% end %>
</td>
<td class="product-price">
<%= history_item.salary %>
</td>
</tr>
\ No newline at end of file
<tr class="woocommerce-cart-form__cart-item cart_item">
<td>
<%= history_item_counter + 1 %>
</td>
<td style="text-align: center;">
<input type="checkbox" class="checkbox-apply-job" data-id="<%= history_item.job_id %>">
</td>
<td>
<%= link_to history_item.job.title, history_item.job %>
</td>
<td>
<% history_item.job.cities.each_with_index do |city, index| %>
<%= city.title %><%= ", " if index != history_item.job.cities.size - 1 %>
<% end %>
</td>
<td class="product-price">
<%= history_item.job.salary %>
</td>
</tr>
\ No newline at end of file
class CreateHistories < ActiveRecord::Migration[6.0]
def change
create_table :histories do |t|
t.references :user, null: false, foreign_key: true
t.references :job, null: false, foreign_key: true
end
end
end
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