refactor code with object query and remove jquery gem

parent eba9e791
Pipeline #1411 failed with stages
in 0 seconds
......@@ -4,7 +4,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '3.0.1'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'jquery-rails', '~> 4.4'
# gem 'bullet', group: 'development'
gem 'rails', '~> 6.1.3', '>= 6.1.3.2'
gem 'bootstrap', '~> 5.0.1'
gem 'nokogiri', '~> 1.11', '>= 1.11.7'
......@@ -15,6 +15,7 @@ gem 'babosa'
gem 'active_storage_validations'
gem 'mini_magick', '>= 4.9.5'
gem 'devise'
gem 'bullet', group: 'development'
# Use sqlite3 as the database for Active Record
gem 'mysql2', '~> 0.5.3'
# Use Puma as the app server
......
......@@ -76,6 +76,9 @@ GEM
popper_js (>= 2.9.2, < 3)
sassc-rails (>= 2.0.0)
builder (3.2.4)
bullet (6.1.5)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.11)
byebug (11.1.3)
capybara (3.35.3)
addressable
......@@ -106,10 +109,6 @@ GEM
concurrent-ruby (~> 1.0)
jbuilder (2.11.2)
activesupport (>= 5.0.0)
jquery-rails (4.4.0)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
kaminari (1.2.1)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.1)
......@@ -225,6 +224,7 @@ GEM
turbolinks-source (5.2.0)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
uniform_notifier (1.14.2)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.1.0)
......@@ -258,12 +258,12 @@ DEPENDENCIES
babosa
bootsnap (>= 1.4.4)
bootstrap (~> 5.0.1)
bullet
byebug
capybara (>= 3.26)
devise
friendly_id (~> 5.4, >= 5.4.2)
jbuilder (~> 2.7)
jquery-rails (~> 4.4)
kaminari (~> 1.2, >= 1.2.1)
listen (~> 3.3)
mini_magick (>= 4.9.5)
......
class FavoriteJobsController < ApplicationController
def show
@favorites = FavoriteJob.order('created_at DESC').page(params[:page])
@favorites = JobsQuery.new.order_favorite(current_user).page(params[:page])
end
def update
favorite = FavoriteJob.where(job: Job.find(params[:job_id]), user: current_user)
favorite = JobsQuery.new.find_favorite(job_params, current_user)
if favorite == []
FavoriteJob.create(job: Job.find(params[:job_id]), user: current_user)
@favorite_exists = true
else
FavoriteJob.where(job: Job.find(params[:job_id]), user: current_user).first.destroy
JobsQuery.new.find_favorite(job_params, current_user).first.destroy
@favorite_exists = false
end
respond_to do |format|
......@@ -19,8 +19,14 @@ class FavoriteJobsController < ApplicationController
end
def destroy
@favorite = FavoriteJob.where(job: Job.find(params[:job_id]), user: current_user).first.destroy
@favorite = JobsQuery.new.find_favorite(job_params, current_user).first.destroy
redirect_to favorite_url
flash[:success] = "#{@favorite.job.title} was completed and destroyed."
end
private
def job_params
params.permit(:job_id)
end
end
class HistoryJobsController < ApplicationController
def show
@histories = HistoryJob.order('created_at DESC')
@histories = JobsQuery.new.order_history(current_user)
end
end
......@@ -14,23 +14,26 @@ class JobsController < ApplicationController
end
@total = jobs.count
@jobs = jobs.latest_jobs.page(params[:page])
@favorite_exists = FavoriteJob.where(job: @job, user: current_user) == [] ? false : true
end
def show
@job = Job.latest_jobs.find_by(slug: params[:job_slug])
@favorite_exists = FavoriteJob.where(job: @job, user: current_user) == [] ? false : true
@favorite_exists = FavoriteJob.where(job: @job, user: current_user) != []
end
private
def history_action
history = HistoryJob.where(job_id: @job.id, user_id: current_user.id)
history = JobsQuery.new.find_history(job_params, current_user)
if history == []
HistoryJob.create(job_id: @job.id, user_id: current_user.id)
HistoryJob.create(job: Job.find(params[:job_slug]), user: current_user)
else
history.touch_all(:created_at)
end
HistoryJob.first.destroy if HistoryJob.count > 20
end
def job_params
params.permit(:job_slug)
end
end
......@@ -3,11 +3,17 @@
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"
// import Rails from "@rails/ujs"
// import Turbolinks from "turbolinks"
// import * as ActiveStorage from "@rails/activestorage"
// import "channels"
Rails.start()
Turbolinks.start()
ActiveStorage.start()
// Rails.start()
// Turbolinks.start()
// ActiveStorage.start()
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")
class JobsQuery
def initialize(jobs = FavoriteJob, histories = HistoryJob)
@jobs = jobs.includes(job: %i[cities cities_jobs])
@histories = histories.includes(job: %i[cities cities_jobs])
end
def find_favorite(params, current_user)
@jobs.where(job: Job.find(params[:job_id]), user: current_user)
end
def find_favorite_slug(params, current_user)
@jobs.where(job: Job.find(params[:job_slug]), user: current_user)
end
def order_favorite(current_user)
@jobs.where(user: current_user).order('favorite_jobs.created_at DESC')
end
def order_history(current_user)
@histories.where(user: current_user).order('history_jobs.created_at DESC')
end
def find_history(params, current_user)
@histories.where(job: Job.find(params[:job_slug]), user: current_user)
end
end
\ No newline at end of file
......@@ -23,9 +23,13 @@ h3.p-3.text-center
= truncate(favorite.job.overview, length: 250)
.col-sm-2.p-3.border.favourite
= link_to "Remove", unfavorite_path(job_id: favorite.job.id), method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-outline-primary"
h6.offset-md-4.p-2
= page_entries_info @favorites
.col-6.offset-md-5.text-center
= paginate @favorites, window: 1
.span.p-3.text-center
= f.submit "Apply job", class: "btn btn-primary btn-lg"
-if @favorites.any?
h6.offset-md-4.p-2
= page_entries_info @favorites
.col-6.offset-md-5.text-center
= paginate @favorites, window: 1
.span.p-3.text-center
= f.submit "Apply job", class: "btn btn-primary btn-lg"
-else
h4.text-center
| You haven't favorite jobs
......@@ -19,5 +19,9 @@ h3.p-3.text-center
= history.job.cities.map(&:name).uniq.join(' | ')
p.mb-1
= truncate(history.job.overview, length: 250)
.span.p-3.text-center
= f.submit "Apply job", class: "btn btn-primary btn-lg"
\ No newline at end of file
-if @histories.any?
.span.p-3.text-center
= f.submit "Apply job", class: "btn btn-primary btn-lg"
-else
h4.text-center
| You haven't history jobs
\ No newline at end of file
......@@ -13,7 +13,7 @@
.job-list
- @jobs.each do |job|
.row.bg-white
.col-10.p-3.border.card-body.text-dark
.col-10.p-3.card-body.text-dark
h5.mb-1
= link_to job.title, detail_path(job.slug)
p.mb-1
......@@ -27,7 +27,7 @@
= link_to job.cities.map(&:name).uniq.join(' | '), "#"
p.mb-1
= truncate(job.overview, length: 250)
.col-sm-2.p-3.border.favourite
.col-sm-2.p-3.favourite
= link_to favorite_text, favorite_update_path(job_id: job.id), id: 'favorite_link', remote: true, class: "btn btn-outline-primary"
h6.offset-md-4.px-5
= page_entries_info @jobs
......
......@@ -84,4 +84,11 @@ Rails.application.configure do
# Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true
config.after_initialize do
Bullet.enable = true
Bullet.bullet_logger = true
Bullet.raise = true # raise an error if n+1 query occurs
Bullet.unused_eager_loading_enable = false
end
end
......@@ -8,8 +8,8 @@ Rails.application.routes.draw do
get 'apply', to: 'apply_jobs#new'
post 'confirm', to: 'apply_jobs#confirm'
post 'done', to: 'apply_jobs#done'
get 'favorite', to: 'favorite_jobs#update', as: 'favorite_update'
get 'favorite2', to: 'favorite_jobs#show', as: 'favorite'
get 'job_favorite', to: 'favorite_jobs#update', as: 'favorite_update'
get 'favorite', to: 'favorite_jobs#show', as: 'favorite'
delete 'unfavorite', to: 'favorite_jobs#destroy'
get 'history', to: 'history_jobs#show', as: 'history'
devise_for :users, skip: %i[sessions registrations passwords], controllers: { confirmations: 'users/confirmations' }
......
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