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" }
ruby '3.0.1'
# 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 'bootstrap', '~> 5.0.1'
gem 'nokogiri', '~> 1.11', '>= 1.11.7'
......@@ -15,7 +14,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
......@@ -54,6 +53,7 @@ group :development do
gem 'listen', '~> 3.3'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'bullet'
end
group :test do
......
class FavoriteJobsController < ApplicationController
def show
@favorites = JobsQuery.new.order_favorite(current_user).page(params[:page])
def index
@favorites = job_query.order_favorite(current_user).page(params[:page])
end
def update
favorite = JobsQuery.new.find_favorite(job_params, current_user)
if favorite == []
favorite = job_query.find_favorite(job_params, current_user)
if favorite.empty?
FavoriteJob.create(job: Job.find(params[:job_id]), user: current_user)
@favorite_exists = true
else
JobsQuery.new.find_favorite(job_params, current_user).first.destroy
favorite.first.destroy
@favorite_exists = false
end
respond_to do |format|
format.html {}
format.js {}
end
end
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
flash[:success] = "#{@favorite.job.title} was completed and destroyed."
end
......@@ -29,4 +28,8 @@ class FavoriteJobsController < ApplicationController
def job_params
params.permit(:job_id)
end
def job_query
@job_query ||= FavoriteQuery.new
end
end
class HistoryJobsController < ApplicationController
def show
@histories = JobsQuery.new.order_history(current_user)
def index
@histories = job_query.order_history(current_user)
end
private
def job_query
@job_query ||= HistoryQuery.new
end
end
......@@ -24,11 +24,11 @@ class JobsController < ApplicationController
private
def history_action
history = JobsQuery.new.find_history(job_params, current_user)
if history == []
history = job_query.find_history(job_params, current_user)
if history.empty?
HistoryJob.create(job: Job.find(params[:job_slug]), user: current_user)
else
history.touch_all(:created_at)
history.touch_all(:updated_at)
end
HistoryJob.first.destroy if HistoryJob.count > 20
end
......@@ -36,4 +36,8 @@ class JobsController < ApplicationController
def job_params
params.permit(:job_slug)
end
def job_query
@job_query ||= HistoryQuery.new
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
def initialize(jobs = FavoriteJob, histories = HistoryJob)
@jobs = jobs.includes(job: %i[cities cities_jobs])
class HistoryQuery
def initialize(histories = HistoryJob)
@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')
@histories.where(user: current_user).order('history_jobs.updated_at DESC')
end
def find_history(params, current_user)
......
......@@ -52,5 +52,3 @@
= link_to "Apply this Job", apply_path(job_id: @job.id), class: "btn btn-outline-primary btn-lg"
.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"
\ No newline at end of file
......@@ -9,9 +9,9 @@ Rails.application.routes.draw do
post 'confirm', to: 'apply_jobs#confirm'
post 'done', to: 'apply_jobs#done'
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'
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' }
get '/my', to: 'users#show'
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