Commit 25f4d82e by Mai Hoang Thai Ha

fixed review

parent 4aa82d16
// Place all the styles related to the Cities 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 Industries 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 Jobs 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 Top controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
class ApplicationController < ActionController::Base
end
class CitiesController < ApplicationController
def index
@region_job_list = City.jobs_count
end
def show
@regions = City.regions.keys
@job_quantity_by_region = City.job_quantity_by_region
end
end
class IndustriesController < ApplicationController
def index
# @industries_job_list = Industry.joins(:jobs).group(:name).count.sort_by(&:first)
@industries_job_list = Industry.jobs_count
end
def show
@job_quantity_by_industry = Industry.job_quantity_by_industry
end
end
class JobsController < ApplicationController
def index
if params[:city_slug].present?
city = City.find_by(slug: params[:city_slug])
@name = city.name
@jobs = city.jobs
elsif params[:industry_slug].present?
industry = Industry.find_by(slug: params[:industry_slug])
@name = industry.name
@jobs = industry.jobs
if job_params.present?
search
else
@name = 'JOBS'
@jobs = Job.all
@jobs = Job.page(params[:page]).per(20)
end
@jobs = @jobs.page(params[:page]).per(20)
end
def show
@job = Job.find(params[:id])
end
private
def search
if job_params.key?(:model) && job_params.key?(:slug) # search by model
model = params[:model].classify.constantize
@target = model.find_by(slug: job_params[:slug])
@jobs = @target.jobs.page(params[:page]).per(20)
elsif job_params.key?(:search) # search by keywords
wildcard_search = "%#{job_params[:search]}%"
@jobs = Job.where('name ILIKE ? OR postal_code LIKE ?', wildcard_search, wildcard_search)
.page(params[:page]).per(Job::JOB_PER_PAGE)
end
end
def job_params
params.permit(:model, :slug, :search)
end
end
class City < ApplicationRecord
scope :jobs_count, -> { joins(:jobs).group(:region).count }
TOP_JOB_COUNT = 9
has_and_belongs_to_many :jobs
enum region: { vietnam: 0, international: 1 }
validates :slug, presence: true, uniqueness: { case_sensitive: true }
has_and_belongs_to_many :jobs
def to_params
slug
end
validates :slug, presence: true, uniqueness: { case_sensitive: true }
def add_slugs
update slug: to_slug(name)
def self.top_jobs
joins(:jobs).select('cities.*, count(jobs.id) as job_count')
.group('cities.id').order('job_count desc').limit(TOP_JOB_COUNT)
end
def self.top_jobs
self.all.map { |city| [city, city.jobs.count] }.sort_by(&:second).reverse.take(9)
def self.job_quantity_by_region
regions.each_with_object({}) do |(name, _), hash|
hash[name.to_sym] = send(name).joins(:jobs).group(:name, :slug)
.order('COUNT(jobs.id) DESC').count
end
end
end
class Industry < ApplicationRecord
scope :jobs_count, -> { joins(:jobs).group(:name).count.sort_by(&:first) }
TOP_JOB_COUNT = 9
has_and_belongs_to_many :jobs
validates :slug, presence: true, uniqueness: { case_sensitive: true }
# before_validation :add_slugs
def to_params
slug
end
def add_slugs
update slug: to_slug(name)
def self.top_jobs
joins(:jobs).select('industries.*, count(jobs.id) as job_count')
.group('industries.id').order('job_count desc').limit(TOP_JOB_COUNT)
end
def self.top_jobs
self.all.map { |industry| [industry, industry.jobs.count] }.sort_by(&:second).reverse.take(9)
def self.job_quantity_by_industry
Industry.joins(:jobs).group(:name, :slug).order('COUNT(jobs.id) DESC').count
end
end
class Job < ApplicationRecord
scope :latest, -> { order(updated_at: :desc).limit(5).joins(:cities).includes(:company, :cities, :cities_jobs) }
LATEST_JOBS_LIMIT = 5
has_and_belongs_to_many :industries
has_and_belongs_to_many :cities
......@@ -7,4 +7,8 @@ class Job < ApplicationRecord
has_many :apply_jobs, dependent: :destroy
has_many :favorite_jobs, dependent: :destroy
has_many :history_job, dependent: :destroy
def self.latest
joins(:cities).includes(:cities, :cities_jobs, :company).order(created_at: :desc).limit(LATEST_JOBS_LIMIT)
end
end
.cities-list
.container
- @region_job_list.each do |region, job_count|
.countries-job-list id=region
.container-fullwidth.py-5
h2.mt-0
= region
small.text-muted
| (#{pluralize(job_count, "job")})
hr.my-4
.row
- list = get_cities_list(region)
- list.each do |city, count|
.col-lg-4.col-md-6.text-center
.mt-5
.city-item
h3.h4.mb-2.see-more-text= link_to city, city_jobs_path(City.find_by(name: city).slug) , class:'text-decoration-none fw-normal text-reset'
p.text-muted.mb-0= pluralize(count, "job")
\ No newline at end of file
.countries-list
.container.py-5
h2.mt-0 REGION
hr.my-4
.row
- @region_job_list.each do |region, job_count|
.col-lg-4.col-md-6.text-center
.mt-5
h3.h4.mb-2.text-white.see-more-text= link_to region, anchor: region
p.text-muted.mb-0= pluralize(job_count, "job")
= render 'regions_list'
= render 'cities_list'
\ No newline at end of file
/region
.countries-list
.container.py-5
h2.mt-0 REGION
hr.my-4
.row
- @regions.each do |region|
.col-lg-4.col-md-6.text-center
.mt-5
h3.h4.mb-2.text-white.see-more-text= link_to region, anchor: region
// cities list
.cities-list
.container
- @job_quantity_by_region.each do |region, city_job_count|
.countries-job-list id=region
.container-fullwidth.py-5
h2.mt-0
= region
hr.my-4
.row
- city_job_count.each do |city, count|
.col-lg-4.col-md-6.text-center
.mt-5
.city-item
h3.h4.mb-2.see-more-text= link_to city[0], job_list_path(model: :city, slug: city[1]) , class:'text-decoration-none fw-normal text-reset'
p.text-muted.mb-0= pluralize(count, "job")
\ No newline at end of file
......@@ -3,10 +3,10 @@
h2.mt-0 INDUSTRIES
hr.my-4
#info-box.row
- @industries_job_list.each do |name, job_count|
- @job_quantity_by_industry.each do |name, job_count|
.col-lg-6.col-md-6.text-center
.mt-5
h3.h4.mb-2.see-more-text= link_to name, industry_jobs_path(Industry.find_by(name: name).slug) , class:'text-decoration-none fw-normal text-reset'
h3.h4.mb-2.see-more-text= link_to name[0], job_list_path(model: :industry, slug: name[1]) , class:'text-decoration-none fw-normal text-reset'
p.text-muted.mb-0= pluralize(job_count, "job")
hr.divider.my-4
......@@ -5,14 +5,14 @@
| >
- city_list = @job.cities
- city_list.each_with_index do |city, i|
= link_to city_jobs_path(city.slug), class: 'text-decoration-none text-info' do
= link_to job_list_path(model: :city, slug: city.slug), class: 'text-decoration-none text-info' do
= city.name
= " | " if i != city_list.length - 1
span.d-block.mx-2
| >
- industry_list = @job.industries
- industry_list.each_with_index do |industry, i|
= link_to industry_jobs_path(industry.slug), class: 'text-decoration-none text-info' do
= link_to job_list_path(model: :industry, slug: industry.slug), class: 'text-decoration-none text-info' do
= industry.name
= " | " if i != industry_list.length - 1
span.d-block.mx-2
......
- provide(:title, 'Job list page')
= render 'search'
= render 'shared/search'
.container
h1.mt-5
= @name
......
.latest-job.mb-5
.container.mb-5
h2
| Latest jobs
hr.my-2
- @latest_jobs.each do |job|
.job-item.mb-4
= link_to job.title, job_path(Job.find_by(title: job.title).id), class: "job-title"
.job-caption
= link_to job.company.name, "#", class: "job-company"
p.job-salary
| Salary:
= job.salary
.job-locations
ul
- job.cities.each do |city|
li
= city.name
.job-desc
= truncate(simple_format(job.description), escape: false, length: 250)
hr.my-2
.top_cities.mb-5
.container
h2 Top cities
hr.my-2
.row.align-items-start.mb-3
- @top_cities.each do |city, city_jobs|
.col-4.city-item
= link_to city.name, city_jobs_path(City.find_by(name: city.name).slug), class: 'city-name'
span.job-count
| (
= pluralize(city_jobs, "job")
|)
= link_to 'See all cities', cities_path, class:'all-cities-btn'
\ No newline at end of file
.top_industries.mb-5
.container
h2 Top industries
hr.my-2
.row.align-items-start
- @top_industries.each do |industry, industry_jobs|
.col-4.industry-item
= link_to industry.name, industry_jobs_path(Industry.find_by(name: industry.name).slug), class: 'industry-name'
span.job-count
| (
= pluralize(industry_jobs, "job")
|)
= link_to 'See all industries', industries_path, class:'all-industries-btn'
\ No newline at end of file
- provide(:title, 'Home page')
= render 'search'
= render 'latest_jobs'
= render 'top_cities'
= render 'top_industries'
/ search box
= render 'shared/search'
/ latest job
.latest-job.mb-5
.container.mb-5
h2
| Latest jobs
hr.my-2
- @latest_jobs.each do |job|
.job-item.mb-4
= link_to job.title, job_path(job.id), class: "job-title"
.job-caption
= link_to job.company.name, "#", class: "job-company"
p.job-salary
| Salary:
= job.salary
.job-locations
ul
- job.cities.each do |city|
li
= city.name
.job-desc
= truncate(simple_format(job.description), escape: false, length: 250)
hr.my-2
/ top cities
.top_cities.mb-5
.container
h2 Top cities
hr.my-2
.row.align-items-start.mb-3
- @top_cities.each do |city, city_jobs|
.col-4.city-item
= link_to city.name, job_list_path(model: :city, slug: city.slug), class: 'city-name'
span.job-count
| (
= pluralize(city_jobs, "job")
|)
= link_to 'See all cities', cities_path, class:'all-cities-btn'
/ top industries
.top_industries.mb-5
.container
h2 Top industries
hr.my-2
.row.align-items-start
- @top_industries.each do |industry, industry_jobs|
.col-4.industry-item
= link_to industry.name, job_list_path(model: :industry, slug: industry.slug), class: 'industry-name'
span.job-count
| (
= pluralize(industry_jobs, "job")
|)
= link_to 'See all industries', industries_path, class:'all-industries-btn'
\ No newline at end of file
class String
def to_slug
source = [
'à', 'á', 'â', 'ã', 'è', 'é', 'ê', 'ì', 'í', 'ò',
'ó', 'ô', 'õ', 'ù', 'ú', 'ý', 'ă', 'đ', 'ĩ', 'ũ',
'ơ', 'ư', 'ạ', 'ả', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ắ',
'ằ', 'ẳ', 'ẵ', 'ặ', 'ẹ', 'ẻ', 'ẽ', 'ế', 'ề', 'ể',
'ễ', 'ệ', 'ỉ', 'ị', 'ọ', 'ỏ', 'ố', 'ồ', 'ổ', 'ỗ',
'ộ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ụ', 'ủ', 'ứ', 'ừ',
'ử', 'ữ', 'ự', 'ỳ', 'ỷ', 'ỹ', 'ỵ'
]
destination = [
'a', 'a', 'a', 'a', 'e', 'e', 'e', 'i', 'i', 'o',
'o', 'o', 'o', 'u', 'u', 'y', 'a', 'd', 'i', 'u',
'o', 'u', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
'a', 'a', 'a', 'a', 'e', 'e', 'e', 'e', 'e', 'e',
'e', 'e', 'i', 'i', 'o', 'o', 'o', 'o', 'o', 'o',
'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u',
'u', 'u', 'u', 'y', 'y', 'y', 'y'
]
source = %w[
à á â ã è é ê ì í ò ó ô õ ù ú ý ă đ ĩ ũ ơ ư ạ ả ấ ầ ẩ ẫ ậ ắ ằ ẳ ẵ ặ
ẹ ẻ ẽ ế ề ể ễ ệ ỉ ị ọ ỏ ố ồ ổ ỗ ộ ớ ờ ở ỡ ợ ụ ủ ứ ừ ử ữ ự ỳ ỷ ỹ ỵ
].freeze
destination = %w[
a a a a e e e i i o o o o u u y a d i u o u a a a a a a a a a a a a
e e e e e e e e i i o o o o o o o o o o o o u u u u u u u y y y y
].freeze
hash = Hash[source.zip destination]
self.downcase.encode('ASCII', 'UTF-8', fallback: hash).gsub(' ', '-').parameterize.truncate 80
downcase.encode('ASCII', 'UTF-8', fallback: hash).gsub(' ', '-').parameterize
end
end
Rails.application.routes.draw do
root 'top#index'
resources :cities, param: :slug, only: %i[index show] do
resources :jobs, only: %i[index]
end
resources :industries, param: :slug, only: %i[index show] do
resources :jobs, only: %i[index]
end
resources :cities, only: %i[index]
resources :industries, only: %i[index]
get '/jobs/:model/:slug', to: 'jobs#index', as: :job_list
resources :jobs, only: %i[index show]
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