Commit 0f84c9fd by Mai Hoang Thai Ha

add index for favorite and history

parent a59c56fc
...@@ -3,7 +3,8 @@ class FavoriteJobsController < ApplicationController ...@@ -3,7 +3,8 @@ class FavoriteJobsController < ApplicationController
before_action :load_job, only: %i[create destroy] before_action :load_job, only: %i[create destroy]
def index def index
@apply_jobs = Job.apply_jobs_of(current_user.id).sort_by_date(page: params[:page], per_page: Job::JOB_PER_PAGE) @jobs = Job.jobs_of('favorite_jobs', current_user.id)
.page(params[:page]).per(Job::JOB_PER_PAGE)
end end
def create def create
......
...@@ -4,6 +4,7 @@ class HistoryJobsController < ApplicationController ...@@ -4,6 +4,7 @@ class HistoryJobsController < ApplicationController
def index def index
# job_id_list = current_user.history_jobs.map(&:job_id) # job_id_list = current_user.history_jobs.map(&:job_id)
# @jobs = Job.includes(:cities, :cities_jobs, :company).references(:cities).find(job_id_list) # @jobs = Job.includes(:cities, :cities_jobs, :company).references(:cities).find(job_id_list)
@history_jobs = Job.history_jobs_of(current_user.id) @history_jobs = Job.jobs_of('history_jobs', current_user.id)
.page(params[:page]).per(Job::JOB_PER_PAGE)
end end
end end
...@@ -8,6 +8,6 @@ class HistoryJob < ApplicationRecord ...@@ -8,6 +8,6 @@ class HistoryJob < ApplicationRecord
def history_count def history_count
history_list = user.history_jobs.order(updated_at: :desc) history_list = user.history_jobs.order(updated_at: :desc)
history_list.last.destroy if history_list.count > 20 history_list.last.destroy if history_list.count >= 20
end end
end end
class Job < ApplicationRecord class Job < ApplicationRecord
scope :history_jobs_of, ->(user_id) do scope :jobs_of, ->(model, user_id) do
joins(:history_jobs) joins(model.to_sym)
.where("history_jobs.user_id = #{user_id}") .where("#{model.to_sym}.user_id = #{user_id}")
.includes(:cities, :cities_jobs, :company).references(:cities) .includes(:cities, :cities_jobs, :company).references(:cities)
end end
......
- provide(:title, 'Job list page') - provide(:title, 'favorite page')
/ search box
= render 'shared/search'
.container .container
h1.mt-5
= @name
hr.my-4
.no-padding.d-flex.align-items-center.flex-column .no-padding.d-flex.align-items-center.flex-column
.page-info.p-2 .page-info.p-2
= page_entries_info @apply_jobs = page_entries_info @jobs
.page-info.p-2 .page-info.p-2
= paginate @apply_jobs = paginate @jobs
.container .container
.container .container
- @apply_jobs.each do |job| - @jobs.each do |job|
/ job / job
.job-item .job-item
.job-head.d-flex.align-items-center.justify-content-between .job-head.d-flex.align-items-center.justify-content-between
...@@ -32,6 +27,6 @@ ...@@ -32,6 +27,6 @@
hr.my-4 hr.my-4
.no-padding.d-flex.align-items-center.flex-column .no-padding.d-flex.align-items-center.flex-column
.page-info.p-2 .page-info.p-2
= page_entries_info @apply_jobs = page_entries_info @jobs
.page-info.p-2 .page-info.p-2
= paginate @apply_jobs = paginate @jobs
\ No newline at end of file \ No newline at end of file
- provide(:title, 'favorite page')
.container .container
.my-5 .no-padding.d-flex.align-items-center.flex-column
.page-info.p-2
= page_entries_info @history_jobs
.page-info.p-2
= paginate @history_jobs
.container
.container
- @history_jobs.each do |job| - @history_jobs.each do |job|
/ job
.job-item .job-item
.job-head.d-flex.align-items-center.justify-content-between .job-head.d-flex.align-items-center.justify-content-between
= link_to job.title, job, class: 'job-title fs-3 text-decoration-none text-reset' = link_to job.title, job, class: 'job-title fs-3 text-decoration-none text-reset'
...@@ -18,3 +25,8 @@ ...@@ -18,3 +25,8 @@
.job-desc .job-desc
= truncate(simple_format(job.description), escape: false, length: 250) = truncate(simple_format(job.description), escape: false, length: 250)
hr.my-4 hr.my-4
.no-padding.d-flex.align-items-center.flex-column
.page-info.p-2
= page_entries_info @history_jobs
.page-info.p-2
= paginate @history_jobs
\ No newline at end of file
...@@ -8,6 +8,8 @@ header.navbar.navbar-fixed-top.navbar-inverse ...@@ -8,6 +8,8 @@ header.navbar.navbar-fixed-top.navbar-inverse
li li
= link_to "History", history_jobs_path = link_to "History", history_jobs_path
li li
= link_to "Favorite", favorite_jobs_path
li
= link_to "Profile", user_profile_path = link_to "Profile", user_profile_path
li li
= link_to "Log out", destroy_user_session_path, method: :delete = link_to "Log out", destroy_user_session_path, method: :delete
......
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