refactor code base on solid s

parent 35b9860b
Pipeline #1412 canceled with stages
in 0 seconds
...@@ -4,7 +4,6 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } ...@@ -4,7 +4,6 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '3.0.1' ruby '3.0.1'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
# gem 'bullet', group: 'development'
gem 'rails', '~> 6.1.3', '>= 6.1.3.2' gem 'rails', '~> 6.1.3', '>= 6.1.3.2'
gem 'bootstrap', '~> 5.0.1' gem 'bootstrap', '~> 5.0.1'
gem 'nokogiri', '~> 1.11', '>= 1.11.7' gem 'nokogiri', '~> 1.11', '>= 1.11.7'
...@@ -15,7 +14,7 @@ gem 'babosa' ...@@ -15,7 +14,7 @@ gem 'babosa'
gem 'active_storage_validations' gem 'active_storage_validations'
gem 'mini_magick', '>= 4.9.5' gem 'mini_magick', '>= 4.9.5'
gem 'devise' gem 'devise'
gem 'bullet', group: 'development'
# Use sqlite3 as the database for Active Record # Use sqlite3 as the database for Active Record
gem 'mysql2', '~> 0.5.3' gem 'mysql2', '~> 0.5.3'
# Use Puma as the app server # Use Puma as the app server
...@@ -54,6 +53,7 @@ group :development do ...@@ -54,6 +53,7 @@ group :development do
gem 'listen', '~> 3.3' gem 'listen', '~> 3.3'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring' gem 'spring'
gem 'bullet'
end end
group :test do group :test do
......
class FavoriteJobsController < ApplicationController class FavoriteJobsController < ApplicationController
def show def index
@favorites = JobsQuery.new.order_favorite(current_user).page(params[:page]) @favorites = job_query.order_favorite(current_user).page(params[:page])
end end
def update def update
favorite = JobsQuery.new.find_favorite(job_params, current_user) favorite = job_query.find_favorite(job_params, current_user)
if favorite == [] if favorite.empty?
FavoriteJob.create(job: Job.find(params[:job_id]), user: current_user) FavoriteJob.create(job: Job.find(params[:job_id]), user: current_user)
@favorite_exists = true @favorite_exists = true
else else
JobsQuery.new.find_favorite(job_params, current_user).first.destroy favorite.first.destroy
@favorite_exists = false @favorite_exists = false
end end
respond_to do |format| respond_to do |format|
format.html {}
format.js {} format.js {}
end end
end end
def destroy def destroy
@favorite = JobsQuery.new.find_favorite(job_params, current_user).first.destroy @favorite = job_query.find_favorite(job_params, current_user).first.destroy
redirect_to favorite_url redirect_to favorite_url
flash[:success] = "#{@favorite.job.title} was completed and destroyed." flash[:success] = "#{@favorite.job.title} was completed and destroyed."
end end
...@@ -29,4 +28,8 @@ class FavoriteJobsController < ApplicationController ...@@ -29,4 +28,8 @@ class FavoriteJobsController < ApplicationController
def job_params def job_params
params.permit(:job_id) params.permit(:job_id)
end end
def job_query
@job_query ||= FavoriteQuery.new
end
end end
class HistoryJobsController < ApplicationController class HistoryJobsController < ApplicationController
def show def index
@histories = JobsQuery.new.order_history(current_user) @histories = job_query.order_history(current_user)
end
private
def job_query
@job_query ||= HistoryQuery.new
end end
end end
...@@ -24,11 +24,11 @@ class JobsController < ApplicationController ...@@ -24,11 +24,11 @@ class JobsController < ApplicationController
private private
def history_action def history_action
history = JobsQuery.new.find_history(job_params, current_user) history = job_query.find_history(job_params, current_user)
if history == [] if history.empty?
HistoryJob.create(job: Job.find(params[:job_slug]), user: current_user) HistoryJob.create(job: Job.find(params[:job_slug]), user: current_user)
else else
history.touch_all(:created_at) history.touch_all(:updated_at)
end end
HistoryJob.first.destroy if HistoryJob.count > 20 HistoryJob.first.destroy if HistoryJob.count > 20
end end
...@@ -36,4 +36,8 @@ class JobsController < ApplicationController ...@@ -36,4 +36,8 @@ class JobsController < ApplicationController
def job_params def job_params
params.permit(:job_slug) params.permit(:job_slug)
end end
def job_query
@job_query ||= HistoryQuery.new
end
end end
class FavoriteQuery
def initialize(favorites = FavoriteJob)
@favorites = favorites.includes(job: %i[cities cities_jobs])
end
def find_favorite(params, current_user)
@favorites.where(job: Job.find(params[:job_id]), user: current_user)
end
def find_favorite_slug(params, current_user)
@favorites.where(job: Job.find(params[:job_slug]), user: current_user)
end
def order_favorite(current_user)
@favorites.where(user: current_user).order('favorite_jobs.created_at DESC')
end
end
\ No newline at end of file
class JobsQuery class HistoryQuery
def initialize(jobs = FavoriteJob, histories = HistoryJob) def initialize(histories = HistoryJob)
@jobs = jobs.includes(job: %i[cities cities_jobs])
@histories = histories.includes(job: %i[cities cities_jobs]) @histories = histories.includes(job: %i[cities cities_jobs])
end 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) def order_history(current_user)
@histories.where(user: current_user).order('history_jobs.created_at DESC') @histories.where(user: current_user).order('history_jobs.updated_at DESC')
end end
def find_history(params, current_user) def find_history(params, current_user)
......
...@@ -19,7 +19,7 @@ h3.p-3.text-center ...@@ -19,7 +19,7 @@ h3.p-3.text-center
= history.job.cities.map(&:name).uniq.join(' | ') = history.job.cities.map(&:name).uniq.join(' | ')
p.mb-1 p.mb-1
= truncate(history.job.overview, length: 250) = truncate(history.job.overview, length: 250)
-if @histories.any? -if @histories.any?
.span.p-3.text-center .span.p-3.text-center
= f.submit "Apply job", class: "btn btn-primary btn-lg" = f.submit "Apply job", class: "btn btn-primary btn-lg"
-else -else
......
...@@ -52,5 +52,3 @@ ...@@ -52,5 +52,3 @@
= link_to "Apply this Job", apply_path(job_id: @job.id), class: "btn btn-outline-primary btn-lg" = link_to "Apply this Job", apply_path(job_id: @job.id), class: "btn btn-outline-primary btn-lg"
.col.text-start .col.text-start
= link_to favorite_text, favorite_update_path(job_id: @job.id), id: 'favorite_link', remote: true, class: "btn btn-outline-primary btn-lg" = link_to favorite_text, favorite_update_path(job_id: @job.id), id: 'favorite_link', remote: true, class: "btn btn-outline-primary btn-lg"
\ No newline at end of file
...@@ -9,9 +9,9 @@ Rails.application.routes.draw do ...@@ -9,9 +9,9 @@ Rails.application.routes.draw do
post 'confirm', to: 'apply_jobs#confirm' post 'confirm', to: 'apply_jobs#confirm'
post 'done', to: 'apply_jobs#done' post 'done', to: 'apply_jobs#done'
get 'job_favorite', to: 'favorite_jobs#update', as: 'favorite_update' get 'job_favorite', to: 'favorite_jobs#update', as: 'favorite_update'
get 'favorite', to: 'favorite_jobs#show', as: 'favorite' get 'favorite', to: 'favorite_jobs#index', as: 'favorite'
delete 'unfavorite', to: 'favorite_jobs#destroy' delete 'unfavorite', to: 'favorite_jobs#destroy'
get 'history', to: 'history_jobs#show', as: 'history' get 'history', to: 'history_jobs#index', as: 'history'
devise_for :users, skip: %i[sessions registrations passwords], controllers: { confirmations: 'users/confirmations' } devise_for :users, skip: %i[sessions registrations passwords], controllers: { confirmations: 'users/confirmations' }
get '/my', to: 'users#show' get '/my', to: 'users#show'
devise_scope :user do devise_scope :user do
......
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