Create Slug in City and Industry

parent 745be587
...@@ -25,3 +25,4 @@ ...@@ -25,3 +25,4 @@
.viet-nam, .international { .viet-nam, .international {
cursor: pointer; cursor: pointer;
} }
...@@ -110,3 +110,16 @@ ...@@ -110,3 +110,16 @@
} }
} }
} }
.result-for {
background-color: #99ff99;
border: 2px solid green;
border-radius: 12px;
text-align: center;
}
.total {
background-color: #b3ecff;
border: 2px solid #006080;
border-radius: 12px;
text-align: center;
font-size: 32px;
}
module Convert
def self.to_convert(str)
str.mb_chars.normalize(:kd).gsub(/[Đđ]/, 'd').gsub(/[^\x00-\x7F]/,'').gsub(/[\W+]/,' ').downcase.to_s.split(' ').join('-')
end
def self.to_convert_name(name)
name.mb_chars.normalize(:kd).gsub(/[Đđ]/, 'd').gsub(/[^\x00-\x7F]/,'').gsub(/[\W+0-9]/,' ').downcase.to_s.split(' ').join('-')
end
end
class JobsController < ApplicationController class JobsController < ApplicationController
before_action :use_variables
def index def index
@cities = City.all
@industries = Industry.all
@total_job = Job.count
@jobs_list = Job.all_job.page(params[:page]).per(20) @jobs_list = Job.all_job.page(params[:page]).per(20)
end end
def city_jobs def city_jobs
@cities = City.all @city = City.find_by(converted_name: params[:converted_name])
@industries = Industry.all
@city = City.find_by(name: params[:name])
@jobs_list = @city.jobs.all_job.page(params[:page]).per(20) @jobs_list = @city.jobs.all_job.page(params[:page]).per(20)
@total_job = Job.count
@result_for_job = @city.jobs.count @result_for_job = @city.jobs.count
end end
def industry_jobs def industry_jobs
@industry = Industry.find_by(converted_name: params[:converted_name])
@jobs_list = @industry.jobs.all_job.page(params[:page]).per(20)
@result_for_job = @industry.jobs.count
end
def use_variables
@cities = City.all @cities = City.all
@industries = Industry.all @industries = Industry.all
@industry = Industry.find(params[:id])
@jobs_list = @industry.jobs.all_job.page(params[:page]).per(20)
@total_job = Job.count @total_job = Job.count
@result_for_job = @industry.jobs.count
end end
end end
class City < ApplicationRecord class City < ApplicationRecord
before_save :convert_city
has_many :city_jobs has_many :city_jobs
has_many :jobs, through: :city_jobs has_many :jobs, through: :city_jobs
......
class Company < ApplicationRecord class Company < ApplicationRecord
before_save :convert_company
has_many :jobs has_many :jobs
def convert_company
self.converted_name = Convert.to_convert("#{name}")
end
end end
class Industry < ApplicationRecord class Industry < ApplicationRecord
before_save :convert_industry
has_many :industry_jobs has_many :industry_jobs
has_many :jobs, through: :industry_jobs has_many :jobs, through: :industry_jobs
scope :top_industry, -> { joins(:jobs).group(:industry_id).order('count(job_id) DESC').limit(9) } scope :top_industry, -> { joins(:jobs).group(:industry_id).order('count(job_id) DESC').limit(9) }
scope :all_industry, -> { joins(:jobs).group(:industry_id).order('count(job_id) DESC') } scope :all_industry, -> { joins(:jobs).group(:industry_id).order('count(job_id) DESC') }
def convert_industry
converted_name = Convert.to_convert("#{name}")
end
def convert_name def convert_name
name.mb_chars.normalize(:kd).gsub(/[Đđ]/, 'd').gsub(/[^\x00-\x7F]/,'').gsub(/[\W+0-9]/,' ').downcase.to_s.split(' ').join('-') converted_name = Convert.to_convert_name("#{name}")
end end
end end
class Job < ApplicationRecord class Job < ApplicationRecord
before_save :convert_job
belongs_to :company belongs_to :company
has_many :city_jobs has_many :city_jobs
has_many :cities, through: :city_jobs has_many :cities, through: :city_jobs
...@@ -18,6 +19,10 @@ class Job < ApplicationRecord ...@@ -18,6 +19,10 @@ class Job < ApplicationRecord
scope :limit_job, -> { includes(:cities, :company).order(created_at: :desc).limit(5) } scope :limit_job, -> { includes(:cities, :company).order(created_at: :desc).limit(5) }
scope :all_job, -> { limit(20).order(created_at: :desc) } scope :all_job, -> { limit(20).order(created_at: :desc) }
def convert_job
self.converted_name = Convert.to_convert("#{title}")
end
def company_name def company_name
company&.name company&.name
end end
......
<div class="col-3 remove-decoration"> <div class="col-3 remove-decoration">
<%= link_to city_jobs_path(name: city.name) do %> <%= link_to city_jobs_path(converted_name: city.converted_name) do %>
<div class="border border-dark rounded vn-name"> <div class="border border-dark rounded vn-name">
<div> <div>
<strong><%= city.name %></strong> <strong><%= city.name %></strong>
......
<div class="col-3 remove-decoration"> <div class="col-3 remove-decoration">
<%= link_to city_jobs_path(name: city.convert_name) do %> <%= link_to city_jobs_path(converted_name: city.convert_name) do %>
<div class="border border-dark rounded international-name"> <div class="border border-dark rounded international-name">
<div> <div>
<strong><%= city.name %></strong> <strong><%= city.name %></strong>
......
<div class="col-3 remove-decoration"> <div class="col-3 remove-decoration">
<%= link_to industry_jobs_path(industry.id) do %> <%= link_to industry_jobs_path(converted_name: industry.converted_name) do %>
<div class="border border-dark rounded industry-details"> <div class="border border-dark rounded industry-details">
<div> <div>
<strong><%= industry.name %></strong> <strong><%= industry.name %></strong>
......
...@@ -3,12 +3,10 @@ ...@@ -3,12 +3,10 @@
<div class="job-details"> <div class="job-details">
<div class="title"><strong><%= job.title %></strong></div> <div class="title"><strong><%= job.title %></strong></div>
<div class="row"> <div class="row">
<div class="col-6"> <div class="col-5">
<% job.cities.each do |city| %> <%= job.cities.map(&:name).join(' | ') %>
<%= city.name %>
<% end %>
</div> </div>
<div class="col-4 salary">💲 Salary: <%= job.salary %></div> <div class="col-5 salary">💲 Salary: <%= job.salary %></div>
<div class="col-10 introduction"> <div class="col-10 introduction">
<%= job_description(job.description) %><br> <%= job_description(job.description) %><br>
<%= link_to 'Read more..', '#'%> <%= link_to 'Read more..', '#'%>
......
<div class="row"> <div class="row panigation">
<div class="col-12 d-flex justify-content-end paginate-jobs"> <div class="col-12 d-flex justify-content-end paginate-jobs">
<%= paginate @jobs_list, outer_window: 3, window: 2 %> <%= paginate @jobs_list, outer_window: 3, window: 2 %>
</div> </div>
......
...@@ -3,9 +3,8 @@ ...@@ -3,9 +3,8 @@
<div class="search-bar"> <div class="search-bar">
<%= render 'layouts/search_bar' %> <%= render 'layouts/search_bar' %>
</div> </div>
<div class="row total-jobs"> <div class="total-jobs">
<div class="col-4">Total: <%= @total_job %> jobs</div> <div class="total">Total: <strong><%= @result_for_job %></strong> jobs in <strong><%= @city.name %></strong></div>
<div class="col-4">Result for: <%= @result_for_job %> jobs</div>
</div> </div>
<%= render "jobs/pagination" %> <%= render "jobs/pagination" %>
<div class="job-list"> <div class="job-list">
......
...@@ -3,9 +3,8 @@ ...@@ -3,9 +3,8 @@
<div class="search-bar"> <div class="search-bar">
<%= render 'layouts/search_bar' %> <%= render 'layouts/search_bar' %>
</div> </div>
<div class="row total-jobs"> <div class="total-jobs">
<div class="col-4">Total: <%= @total_job %> jobs</div> <div class="total">Total: <strong><%= @result_for_job %></strong> jobs in <strong><%= @industry.name %></strong></div>
<div class="col-4">Result for: <%= @result_for_job %> jobs</div>
</div> </div>
<%= render "jobs/pagination" %> <%= render "jobs/pagination" %>
<div class="job-list"> <div class="job-list">
......
<div class="col-4"> <div class="col-4">
<div class="row-table border border-dark rounded city-list"> <div class="row-table border border-dark rounded city-list">
<%= link_to city_jobs_path(name: city.name) do %> <%= link_to city_jobs_path(converted_name: city.converted_name) do %>
<div class="city-name"><strong><%= city.name %></strong></div> <div class="city-name"><strong><%= city.name %></strong></div>
<div class="count-job"><%= city.jobs.count %></div> <div class="count-job"><%= city.jobs.count %></div>
<% end %> <% end %>
......
<div class="col-4"> <div class="col-4">
<div class="row-table border border-dark rounded industry-list"> <div class="row-table border border-dark rounded industry-list">
<%= link_to industry_jobs_path(industry.id) do %> <%= link_to industry_jobs_path(converted_name: industry.converted_name) do %>
<div class="industry-name"><strong><%= industry.name %></strong></div> <div class="industry-name"><strong><%= industry.name %></strong></div>
<div class="count-job"><%= industry.jobs.count %></div> <div class="count-job"><%= industry.jobs.count %></div>
<% end %> <% end %>
......
...@@ -5,9 +5,7 @@ ...@@ -5,9 +5,7 @@
<div><%= job.company_name %></div> <div><%= job.company_name %></div>
<div class="salary">💲 Salary: <%= job.salary %></div> <div class="salary">💲 Salary: <%= job.salary %></div>
<div> <div>
<% job.cities.each do |city| %> <%= job.cities.map(&:name).join(' | ') %>
<%= city.name %>
<% end %>
</div> </div>
<div class="row"> <div class="row">
<div class="col-10 introduction"> <div class="col-10 introduction">
......
Rails.application.routes.draw do Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :jobs resources :jobs
get 'jobs/city/:name', to: 'jobs#city_jobs', as: :city_jobs get 'jobs/city/:converted_name', to: 'jobs#city_jobs', as: :city_jobs
get 'jobs/industry/:id', to: 'jobs#industry_jobs', as: :industry_jobs get 'jobs/industry/:converted_name', to: 'jobs#industry_jobs', as: :industry_jobs
resources :top_pages resources :top_pages
resources :industries resources :industries
......
...@@ -8,6 +8,8 @@ class CreateJobs < ActiveRecord::Migration[5.2] ...@@ -8,6 +8,8 @@ class CreateJobs < ActiveRecord::Migration[5.2]
t.string :experience t.string :experience
t.string :expiration_date t.string :expiration_date
t.bigint :company_id t.bigint :company_id
t.string :converted_name
t.timestamps t.timestamps
end end
end end
......
...@@ -3,6 +3,7 @@ class CreateCities < ActiveRecord::Migration[5.2] ...@@ -3,6 +3,7 @@ class CreateCities < ActiveRecord::Migration[5.2]
create_table :cities, :options => 'COLLATE=utf8_general_ci' do |t| create_table :cities, :options => 'COLLATE=utf8_general_ci' do |t|
t.string :name t.string :name
t.boolean :location t.boolean :location
t.string :converted_name
t.timestamps t.timestamps
end end
......
...@@ -2,6 +2,7 @@ class CreateIndustries < ActiveRecord::Migration[5.2] ...@@ -2,6 +2,7 @@ class CreateIndustries < ActiveRecord::Migration[5.2]
def change def change
create_table :industries, :options => 'COLLATE=utf8_general_ci' do |t| create_table :industries, :options => 'COLLATE=utf8_general_ci' do |t|
t.string :name t.string :name
t.string :converted_name
t.timestamps t.timestamps
end end
......
...@@ -4,7 +4,8 @@ class CreateCompanies < ActiveRecord::Migration[5.2] ...@@ -4,7 +4,8 @@ class CreateCompanies < ActiveRecord::Migration[5.2]
t.string :name t.string :name
t.text :address t.text :address
t.text :introduction t.text :introduction
t.string :converted_name
t.timestamps t.timestamps
end end
end end
......
...@@ -10,11 +10,12 @@ ...@@ -10,11 +10,12 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_07_23_071239) do ActiveRecord::Schema.define(version: 2020_07_15_035356) do
create_table "cities", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "cities", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "name" t.string "name"
t.boolean "location" t.boolean "location"
t.string "converted_name"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
...@@ -30,6 +31,7 @@ ActiveRecord::Schema.define(version: 2020_07_23_071239) do ...@@ -30,6 +31,7 @@ ActiveRecord::Schema.define(version: 2020_07_23_071239) do
t.string "name" t.string "name"
t.text "address" t.text "address"
t.text "introduction" t.text "introduction"
t.string "converted_name"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
...@@ -50,6 +52,7 @@ ActiveRecord::Schema.define(version: 2020_07_23_071239) do ...@@ -50,6 +52,7 @@ ActiveRecord::Schema.define(version: 2020_07_23_071239) do
create_table "industries", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "industries", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "name" t.string "name"
t.string "converted_name"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
...@@ -79,6 +82,7 @@ ActiveRecord::Schema.define(version: 2020_07_23_071239) do ...@@ -79,6 +82,7 @@ ActiveRecord::Schema.define(version: 2020_07_23_071239) do
t.string "experience" t.string "experience"
t.string "expiration_date" t.string "expiration_date"
t.bigint "company_id" t.bigint "company_id"
t.string "converted_name"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
......
...@@ -3,6 +3,7 @@ class Crawler ...@@ -3,6 +3,7 @@ class Crawler
VIETNAM = 0 VIETNAM = 0
FOREIGN = 1 FOREIGN = 1
def initialize(logger, url) def initialize(logger, url)
@logger = logger @logger = logger
@url = url @url = url
...@@ -74,7 +75,6 @@ class Crawler ...@@ -74,7 +75,6 @@ class Crawler
expiration_date: expiration_date, expiration_date: expiration_date,
description: description, description: description,
company_id: company.id) company_id: company.id)
city_relationship(row, job) city_relationship(row, job)
industry_relationship(row, job) industry_relationship(row, job)
end end
......
...@@ -59,6 +59,12 @@ class CSVImporter ...@@ -59,6 +59,12 @@ class CSVImporter
salary: salary, salary: salary,
company_id: company_id) company_id: company_id)
job = Job.find_or_create_by!(title: title_job,
description: description_job,
level: level,
salary: salary,
company_id: company.id)
industry_name = row["category"] industry_name = row["category"]
industries_relationship = Industry.where(name: industry_name) industries_relationship = Industry.where(name: industry_name)
next if industries_relationship.blank? next if industries_relationship.blank?
......
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