Commit a7e94105 by Tô Ngọc Ánh

func create, destroy favorite

parent 146f451f
Pipeline #973 failed with stages
in 0 seconds
...@@ -2,7 +2,7 @@ class AppliedJobsController < ApplicationController ...@@ -2,7 +2,7 @@ class AppliedJobsController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
def index def index
@applied_jobs = Job.applied_jobs_of(current_user.id).includes(:locations).page(params[:page]) @applied_jobs = Job.applied_jobs_of(current_user.id).includes(:locations).order(created_at: :asc).page(params[:page])
end end
def new def new
......
...@@ -2,7 +2,8 @@ class FavoritesController < ApplicationController ...@@ -2,7 +2,8 @@ class FavoritesController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
def index def index
favorites = current_user.favorites.select(:job_id).all
@jobs = Job.where(id: favorites).page(params[:page])
end end
def create def create
...@@ -12,8 +13,8 @@ class FavoritesController < ApplicationController ...@@ -12,8 +13,8 @@ class FavoritesController < ApplicationController
end end
def destroy def destroy
@job = Job.find_by(id: params[:job_id]) @favorite = current_user.favorites.find_by(job_id: params[:job_id])
favorite = Favorite.find_by(id: params[:id]) @favorite.destroy
respond_to :js if favorite.destroy respond_to :js
end end
end end
...@@ -7,7 +7,7 @@ class AppliedJob < ApplicationRecord ...@@ -7,7 +7,7 @@ class AppliedJob < ApplicationRecord
validates :full_name, presence: true, length: { maximum: 200 } validates :full_name, presence: true, length: { maximum: 200 }
validates :email, presence: true, format: { with: EMAIL_REGEXN } validates :email, presence: true, format: { with: EMAIL_REGEXN }
validates :curriculum_vitae, presence: true validates :curriculum_vitae, presence: true
validates :user, uniqueness: { scope: :job, message: 'has already applied this job' } validates :user, uniqueness: { scope: :job, message: 'have already applied this job.' }
mount_uploader :curriculum_vitae, CurriculumVitaeUploader mount_uploader :curriculum_vitae, CurriculumVitaeUploader
end end
class Favorite < ApplicationRecord class Favorite < ApplicationRecord
belongs_to :user belongs_to :user
belongs_to :job belongs_to :job
validates :user, uniqueness: { scope: :job, message: 'have already favorited this job.' }
end end
...@@ -11,4 +11,8 @@ class User < ApplicationRecord ...@@ -11,4 +11,8 @@ class User < ApplicationRecord
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :recoverable, :rememberable, :validatable,
:confirmable :confirmable
def favorited?(job_id)
favorites.find_by(job_id: job_id).present?
end
end end
<%= form_tag new_applied_job_path, method: :get, enforce_utf8: false do %>
<%= collection_radio_buttons(:job_id, params[:job_id], @jobs, :id, :title) do |radio| %>
<div class='card flex-md-row align-items-center my-2'>
<%= radio.radio_button(class: "ml-3") %>
<div class='card-body'>
<%= link_to radio.object.title, job_path(radio.object), class: 'card-title font-weight-bold text-decoration-none' %>
<p class='card-text'><%= radio.object.company.name %></p>
<p class='mb-0'>
<strong>Work place:</strong>
<% radio.object.locations.each do |location| %>
<%= location.city %>
<% end %>
</p>
<p><strong>Salary: </strong><%= radio.object.salary %></p>
<p class='card-text'><%= strip_tags(radio.object.description).truncate(Job::WORDS_SHORT_DESCRIPTION) %></p>
</div>
<div id="favorite-<%= radio.object.id %>" class='p-2'>
<%= render 'favorites/link_favorite', job_id: radio.object.id %>
</div>
</div>
<% end %>
<div class="actions text-center">
<%= submit_tag 'Apply Now', name: nil, class: 'btn btn-outline-success btn-lg' %>
</div>
<% end %>
<%= link_to 'Favorite', job_favorites_path(job),
method: :post, class: 'btn btn-outline-danger btn-lg', remote: true,
disable_with: '<i class="fa fa-spinner fa-spin"></i>'.html_safe %>
\ No newline at end of file
<%= link_to 'Unfavorite', job_favorite_path(job_id: job_id, id: favorite_id),
method: :delete, class: 'btn btn-outline-secondary btn-lg', remote: true,
disable_with: '<i class="fa fa-spinner fa-spin"></i>'.html_safe %>
\ No newline at end of file
<% if user_signed_in? && current_user.favorited?(job_id) %>
<%= link_to 'Unfavorite', job_favorite_path(job_id),
method: :delete, class: 'btn btn-danger btn-lg', remote: true,
data: { disable_with: '<i class="fa fa-spinner fa-spin"></i>'.html_safe } %>
<% else %>
<%= link_to 'Favorite', job_favorites_path(job_id: job_id),
method: :post, class: 'btn btn-outline-danger btn-lg', remote: true,
data: { disable_with: '<i class="fa fa-spinner fa-spin"></i>'.html_safe } %>
<% end %>
$('#favorite-<%= @job.id %>') $('#favorite-<%= @job.id %>')
.html("<%= j render 'favorites/link_destroy', job_id: @job.id, favorite_id: @favorite.id %>") .html("<%= j render 'favorites/link_favorite', job_id: @job.id %>")
\ No newline at end of file
$('#favorite-<%= @job.id %>') $('#favorite-<%= @favorite.job_id %>')
.html("<%= j render 'favorites/link_create', job: @job %>") .html("<%= j render 'favorites/link_favorite', job_id: @favorite.job_id %>")
\ No newline at end of file
<div class="my-4">
<h3 class='text-center'>Favorited Jobs</h3>
<hr class="divider">
<div class='content'>
<%= paginate @jobs %>
<%= render 'favorites/job', jobs: @jobs %>
<%= paginate @jobs %>
</div>
</div>
\ No newline at end of file
...@@ -12,6 +12,6 @@ ...@@ -12,6 +12,6 @@
<p class='card-text'><%= strip_tags(job.description).truncate(Job::WORDS_SHORT_DESCRIPTION) %></p> <p class='card-text'><%= strip_tags(job.description).truncate(Job::WORDS_SHORT_DESCRIPTION) %></p>
</div> </div>
<div id="favorite-<%= job.id %>" class='p-2'> <div id="favorite-<%= job.id %>" class='p-2'>
<%= render 'favorites/link_create', job: job %> <%= render 'favorites/link_favorite', job_id: job.id %>
</div> </div>
</div> </div>
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
<%= link_to 'Apply Now', '#', class: 'btn btn-outline-secondary btn-lg disabled' %> <%= link_to 'Apply Now', '#', class: 'btn btn-outline-secondary btn-lg disabled' %>
<% end %> <% end %>
</div> </div>
<div class="col-6 text-left"> <div id="favorite-<%= @job.id %>" class="col-6 text-left">
<%= link_to 'Favorite', '#', class: 'btn btn-outline-danger btn-lg' %> <%= render 'favorites/link_favorite', job_id: @job.id %>
</div> </div>
</div> </div>
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
<% if user_signed_in? %> <% if user_signed_in? %>
<li class="nav-item"> <li class="nav-item">
<%= link_to my_favorites_path, class: 'nav-link text-white', title: 'Favorite' do %> <%= link_to my_favorites_path, class: 'nav-link text-white', title: 'Favorite' do %>
<small><%= current_user.favorites.size %></small>
<i class="fas fa-heart"></i> <i class="fas fa-heart"></i>
<% end %> <% end %>
</li> </li>
......
...@@ -11,7 +11,7 @@ Rails.application.routes.draw do ...@@ -11,7 +11,7 @@ Rails.application.routes.draw do
get 'my/jobs', to: 'applied_jobs#index', as: :my_applied_jobs get 'my/jobs', to: 'applied_jobs#index', as: :my_applied_jobs
get 'my/favorite', to: 'favorites#index', as: :my_favorites get 'my/favorite', to: 'favorites#index', as: :my_favorites
post 'jobs/:job_id/favorites', to: 'favorites#create', as: :job_favorites post 'jobs/:job_id/favorites', to: 'favorites#create', as: :job_favorites
delete 'jobs/:job_id/favorites/:id', to: 'favorites#destroy', as: :job_favorite delete 'jobs/:job_id/favorites', to: 'favorites#destroy', as: :job_favorite
resources :users, only: :show resources :users, only: :show
......
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