Commit 39711e75 by Mai Hoang Thai Ha

initialize method to_slug, used scope and class method

parent 6fa1254d
......@@ -45,6 +45,7 @@ group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'rubocop-rails', '~> 2.11', '>= 2.11.3'
gem 'bullet', '~> 6.1', '>= 6.1.4'
end
group :test do
......
......@@ -84,6 +84,9 @@ GEM
bootsnap (1.7.5)
msgpack (~> 1.0)
builder (3.2.4)
bullet (6.1.4)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.11)
byebug (11.1.3)
capybara (3.35.3)
addressable
......@@ -135,8 +138,6 @@ GEM
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-nav (0.3.0)
pry (>= 0.9.10, < 0.13.0)
pry-rails (0.3.9)
pry (>= 0.10.4)
public_suffix (4.0.6)
......@@ -238,6 +239,7 @@ GEM
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (2.0.0)
uniform_notifier (1.14.2)
web-console (4.1.0)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
......@@ -264,6 +266,7 @@ PLATFORMS
DEPENDENCIES
bootsnap (>= 1.4.4)
bullet (~> 6.1, >= 6.1.4)
byebug
capybara (>= 3.26)
httparty (~> 0.18.1)
......@@ -272,7 +275,6 @@ DEPENDENCIES
listen (~> 3.3)
mysql2 (~> 0.5)
nokogiri (~> 1.11, >= 1.11.7)
pry-nav (~> 0.3.0)
pry-rails (~> 0.3.9)
puma (~> 5.0)
rack-mini-profiler (~> 2.0)
......
class CitiesController < ApplicationController
def index
@region_job_list = City.joins(:jobs).group(:region).count
@region_job_list = City.jobs_count
end
def show
end
end
class IndustriesController < ApplicationController
def index
@industries_job_list = Industry.joins(:jobs).group(:name).count.sort_by(&:first)
# @industries_job_list = Industry.joins(:jobs).group(:name).count.sort_by(&:first)
@industries_job_list = Industry.jobs_count
end
def show
end
......
class TopController < ApplicationController
def index
@latest_jobs = Job.order(updated_at: :desc).limit(5)
@top_cities = City.all.map { |city| [city, city.jobs.count] }.sort_by(&:second).reverse.take(9)
@top_industries = Industry.all.map { |industry| [industry, industry.jobs.count] }.sort_by(&:second).reverse.take(9)
@total_job = Job.count
@latest_jobs = Job.order(updated_at: :desc).limit(5).joins(:cities).includes(:company, :cities, :cities_jobs)
@top_cities = City.top_jobs
@top_industries = Industry.top_jobs
end
end
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def to_slug(string)
string.parameterize.truncate 80, omission: ''
end
end
class City < ApplicationRecord
scope :jobs_count, -> { joins(:jobs).group(:region).count }
has_and_belongs_to_many :jobs
enum region: %i[Vietnam International]
enum region: { vietnam: 0, international: 1 }
validates :slug, presence: true, uniqueness: { case_sensitive: true }
......@@ -11,4 +13,8 @@ class City < ApplicationRecord
def add_slugs
update slug: to_slug(name)
end
def self.top_jobs
self.all.map { |city| [city, city.jobs.count] }.sort_by(&:second).reverse.take(9)
end
end
class Industry < ApplicationRecord
scope :jobs_count, -> { joins(:jobs).group(:name).count.sort_by(&:first) }
has_and_belongs_to_many :jobs
validates :slug, presence: true, uniqueness: { case_sensitive: true }
......@@ -12,4 +14,8 @@ class Industry < ApplicationRecord
def add_slugs
update slug: to_slug(name)
end
def self.top_jobs
self.all.map { |industry| [industry, industry.jobs.count] }.sort_by(&:second).reverse.take(9)
end
end
......@@ -4,7 +4,7 @@
.countries-job-list id=region
.container-fullwidth.py-5
h2.mt-0
= region.upcase
= region
small.text-muted
| (#{pluralize(job_count, "job")})
hr.my-4
......
......@@ -5,7 +5,7 @@
.col-xl-9.mx-auto
h1.mb-5.mt-5
| Total:
= pluralize(Job.count, "job")
= pluralize(@total_job, "job")
.row.justify-content-start
.col-md-10.mb-md-0.no-padding
span.fa.fa-search.form-control-feedback
......
......@@ -73,4 +73,20 @@ Rails.application.configure do
# Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true
# Enable bullet, tunrn on logger
# 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
config.after_initialize do
Bullet.enable = true
Bullet.alert = true
Bullet.bullet_logger = true
Bullet.console = true
Bullet.rails_logger = true
Bullet.add_footer = true
end
end
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'
]
hash = Hash[source.zip destination]
self.downcase.encode('ASCII', 'UTF-8', fallback: hash).gsub(' ', '-').parameterize.truncate 80
end
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