Commit 6ce36e02 by Mai Hoang Thai Ha

feature favorite and history

parent afd86273
// Place all the styles related to the FavoriteJobs controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
// Place all the styles related to the HistoryJobs controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
class AppliesController < ApplicationController
before_action :logged_in_user
before_action :load_job, only: %i[new confirm create]
def new
......
class FavoriteJobsController < ApplicationController
before_action :logged_in_user
before_action :load_job, only: %i[create destroy]
def create
@favorite_job = current_user.favorite_jobs.build(job_id: @job.id)
@favorite_job.save
respond_to do |format|
format.html { redirect_back(fallback_location: root_path) }
format.js
end
end
def destroy
@favorite_job = current_user.favorite_jobs.find_by(job_id: @job.id)
@favorite_job.destroy
respond_to do |format|
format.html { redirect_back(fallback_location: root_path) }
format.js
end
end
private
def load_job
@job = Job.find_by(id: params[:job_id])
end
end
class HistoryJobsController < ApplicationController
before_action :logged_in_user
def index
job_id_list = current_user.history_jobs.map(&:job_id)
@jobs = Job.includes(:cities, :cities_jobs, :company).references(:cities).find(job_id_list)
end
end
class JobsController < ApplicationController
before_action :history, only: :show
def index
if job_params.present?
search
......@@ -14,6 +16,13 @@ class JobsController < ApplicationController
private
def history
return unless user_signed_in?
history = current_user.history_jobs.find_or_initialize_by(job_id: params[:id])
history.update(updated_at: Time.current)
end
def search
if job_params.key?(:model) && job_params.key?(:slug) # search by model
model = params[:model].classify.constantize
......
......@@ -3,6 +3,11 @@
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
......
class HistoryJob < ApplicationRecord
before_save :history_count
belongs_to :job
belongs_to :user
private
def history_count
history_list = user.history_jobs.order(updated_at: :desc)
history_list.last.destroy if history_list.count > 20
end
end
......@@ -9,7 +9,7 @@ class Job < ApplicationRecord
has_many :favorite_jobs, dependent: :destroy
has_many :history_job, dependent: :destroy
def self.sort_by_date(page: 1, per_page: limit)
def self.sort_by_date(page: 1, per_page: 1)
includes(:cities, :cities_jobs, :company).order(created_at: :desc).page(page).per(per_page).references(:cities)
end
end
......@@ -26,6 +26,10 @@ class User < ApplicationRecord
result
end
def favorite?(job)
favorite_jobs.exists?(job_id: job.id)
end
protected
def password_required?
......
$("#favorite-<%= @job.id").html("<%= escape_javascript(render "shared/unfavorite", job_id: @job.id) %>"); %>
$("#favorite-<%= @job.id %>").html("<%= escape_javascript(render "shared/favorite", job_id: @job.id) %>");
.container
.my-5
- @jobs.each do |job|
.job-item
.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'
.job-caption
= link_to job.company.name, '#', class: 'job-company text-decoration-none text-secondary'
p.job-salary.text-success
| Salary: #{job.salary}
ul.list-unstyled
= show_location(job.cities)
.job-desc
= truncate(simple_format(job.description), escape: false, length: 250)
hr.my-4
\ No newline at end of file
......@@ -15,7 +15,12 @@
- @jobs.each do |job|
/ job
.job-item
.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'
- if user_signed_in? && current_user.favorite?(job)
= render 'shared/unfavorite', job_id: job.id
- else
= render 'shared/favorite', job_id: job.id
.job-caption
= link_to job.company.name, '#', class: 'job-company text-decoration-none text-secondary'
p.job-salary.text-success
......
......@@ -85,4 +85,7 @@ ruby:
= info
.job-apply.d-flex.align-items-center.justify-content-between
= link_to 'Apply for this job', apply_job_path(job_id: @job.id), class: 'btn btn-primary'
= link_to 'Favorite', '#', class: 'btn btn-primary'
- if user_signed_in? && current_user.favorite?(@job)
= render 'shared/unfavorite', job_id: @job.id
- else
= render 'shared/favorite', job_id: @job.id
......@@ -6,6 +6,8 @@ header.navbar.navbar-fixed-top.navbar-inverse
ul.nav.navbar-nav.navbar-right.text-light
- if user_signed_in?
li
= link_to "History", history_jobs_path
li
= link_to "Profile", user_profile_path
li
= link_to "Log out", destroy_user_session_path, method: :delete
......
= form_with(model: @favorite_job, url: favorite_jobs_path(job_id: job_id), id: "favorite-#{job_id}", remote: true) do |f|
= f.submit 'Favorite', class: 'btn btn-primary btn-height'
\ No newline at end of file
= form_with(model: @favorite_job, url: favorite_job_path(current_user, job_id: job_id), id: "unfavorite-#{job_id}", method: :delete, remote: true) do |f|
= f.submit 'Un favorite', class: 'btn btn-secondary btn-height'
\ No newline at end of file
......@@ -16,12 +16,13 @@ Rails.application.routes.draw do
resources :cities, only: %i[index]
resources :industries, only: %i[index]
get '/jobs/:model/:slug', to: 'jobs#index', as: :job_list
resources :favorite_jobs, only: %i[create destroy]
resources :jobs, only: %i[index show]
get '/my', to: 'users#show', as: :user_profile
get '/jobs/:model/:slug', to: 'jobs#index', as: :job_list
get '/my', to: 'users#show', as: :user_profile
get '/apply', to: 'applies#new', as: :apply_job
post '/confirm', to: 'applies#confirm', as: :confirm_job
post '/done', to: 'applies#create', as: :done_job
get '/history', to: 'history_jobs#index', as: :history_jobs
end
class AddIndexToFavoriteJobs < ActiveRecord::Migration[6.1]
def change
add_index :favorite_jobs, %i[job_id job_id], unique: true
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_08_20_093814) do
ActiveRecord::Schema.define(version: 2021_08_24_082155) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name", null: false
......@@ -81,6 +81,7 @@ ActiveRecord::Schema.define(version: 2021_08_20_093814) do
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["job_id"], name: "index_favorite_jobs_on_job_id"
t.index ["job_id"], name: "index_favorite_jobs_on_job_id_and_job_id", unique: true
t.index ["user_id"], name: "index_favorite_jobs_on_user_id"
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