Commit 94090d82 by Xuan Trung Le

fix errors and optimize code.

parent 757cbfe4
class JobsController < ApplicationController class JobsController < ApplicationController
before_action :set_job, only: [:show] before_action :set_job, only: [:show]
def index def index
@jobs = Job.all.order(updated_date: 'DESC').limit(5) @jobs = Job.preload(:company).order(updated_date: :desc).limit(5)
@cities = City.top_cities @cities = City.top_cities
@industries = Industry.top_industries @industries = Industry.top_industries
end end
def show def show
#code
end
def job_lists
#code
end end
def city def city
city = City.find(params[:id]) city = City.find(params[:city_id])
@jobs = city.jobs.page(params[:page]).per(20) @jobs = city.jobs.includes(:company).page(params[:page]).per(20)
@result = "jobs/City/#{city.name}" @result = "jobs/City/#{city.name}"
render :action => :job_lists render :action => :job_lists
end end
def industry def industry
industry = Industry.find(params[:id]) industry = Industry.find(params[:industry_id])
@jobs = industry.jobs.page(params[:page]).per(20) @jobs = industry.jobs.includes(:company).page(params[:page]).per(20)
@result = "jobs/Industry/#{industry.name}" @result = "jobs/Industry/#{industry.name}"
render :action => :job_lists render :template => "jobs/job_lists"
end end
def company def company
company = Company.find(params[:id]) company = Company.find(params[:company_id])
@jobs = company.jobs.page(params[:page]).per(20) @jobs = company.jobs.page(params[:page]).per(20)
@result = "jobs/Company/#{company.name}" @result = "jobs/Company/#{company.name}"
render :action => :job_lists render :template => "jobs/job_lists"
end end
def set_job def set_job
......
class CitiesJob < ApplicationRecord
belongs_to :city, counter_cache: :jobs_count
belongs_to :job
end
class City < ApplicationRecord class City < ApplicationRecord
belongs_to :country, optional: true belongs_to :country, optional: true
has_and_belongs_to_many :companies has_and_belongs_to_many :companies
has_and_belongs_to_many :jobs has_many :cities_jobs
has_many :jobs, through: :cities_jobs
scope :top_cities, -> { select('cities.id, cities.name AS name, COUNT(jobs.id) AS jobs_count'). scope :top_cities, -> { select('id, name, jobs_count').
joins(:jobs). where('jobs_count > 0').
group('cities.id'). order(jobs_count: :desc).
order('jobs_count DESC, name'). limit(9) }
limit(9) }
scope :city_list, -> { select('cities.id, cities.name AS name, countries.name AS country_name, scope :city_list, -> { select('cities.id, cities.name AS name, countries.name AS country_name, cities.jobs_count').
COUNT(jobs.id) AS jobs_count'). joins(:country).
joins(:jobs). where('jobs_count > 0').
joins(:country). order('jobs_count DESC, name') }
# where('jobs_count >= 1').
group('cities.id').
order('jobs_count DESC, name') }
scope :get_job, -> id { find(id) }
end end
class IndustriesJob < ApplicationRecord
belongs_to :industry, counter_cache: :jobs_count
belongs_to :job
end
class Industry < ApplicationRecord class Industry < ApplicationRecord
has_and_belongs_to_many :jobs # has_and_belongs_to_many :jobs
has_many :industries_jobs
has_many :jobs, through: :industries_jobs
scope :top_industries, -> { select('industries.id, industries.name AS name, COUNT(jobs.id) AS jobs_count'). scope :top_industries, -> { select('id, name, jobs_count').
joins(:jobs). where('jobs_count > 0').
group('industries.id').
order('jobs_count DESC, name'). order('jobs_count DESC, name').
limit(9) } limit(9) }
scope :industry_list, -> { select('industries.id, industries.name AS name, COUNT(jobs.id) AS jobs_count'). scope :industry_list, -> { select('id, name, jobs_count').
joins(:jobs). where('jobs_count > 0').
# where('jobs_count >= 1'). order('jobs_count DESC, name') }
group('industries.id').
order('jobs_count DESC, name') }
end end
...@@ -4,8 +4,10 @@ class Job < ApplicationRecord ...@@ -4,8 +4,10 @@ class Job < ApplicationRecord
has_many :candidates, through: :apply_jobs, class_name: 'User', source: :user has_many :candidates, through: :apply_jobs, class_name: 'User', source: :user
has_many :favorite_jobs has_many :favorite_jobs
has_many :people_who_liked, through: :favorite_jobs, class_name: 'User', source: :user has_many :people_who_liked, through: :favorite_jobs, class_name: 'User', source: :user
has_and_belongs_to_many :industries has_many :industries_jobs
has_and_belongs_to_many :cities has_many :industries, through: :industries_jobs
has_many :cities_jobs
has_many :cities, through: :cities_jobs
def self.create_new_jobs(arr_jobs) def self.create_new_jobs(arr_jobs)
arr_jobs.each do |item| arr_jobs.each do |item|
...@@ -27,7 +29,6 @@ class Job < ApplicationRecord ...@@ -27,7 +29,6 @@ class Job < ApplicationRecord
end end
# Company # Company
<<<<<<< 91b545e3685a56b535c9905838040868191d9dc5
company = Company.find_by(name: item[:company_name]) company = Company.find_by(name: item[:company_name])
if company.nil? if company.nil?
company = Company.create(name: item[:company_name], company = Company.create(name: item[:company_name],
...@@ -35,15 +36,7 @@ class Job < ApplicationRecord ...@@ -35,15 +36,7 @@ class Job < ApplicationRecord
description: item[:company_description]) description: item[:company_description])
end end
job.company = company job.company = company
job.company.cities << (job.cities - job.company.cities) job.company.cities << (job.cities - job.company.cities)
=======
job.company = Company.find_or_initialize_by(name: item[:company_name])
job.company.location = item[:company_location]
job.company.description = item[:company_description]
job.company.cities = job.cities
# job.company.save
>>>>>>> Create the TOP, industry_list, city_list, job_detail pages.
# Industry # Industry
unless item[:industry].blank? unless item[:industry].blank?
......
<div class="col-md-<%= column %>"> <div class="col-md-<%= column %>">
<div class="job-intro well mr0 mrBot20" id="vietnam"> <div class="job-intro well mr0 mrBot20" id="vietnam">
<h4 class="mr0"><%= link_to city.name, "#{jobs_path}/city/#{city.id}" %></h4> <h4 class="mr0"><%= link_to city.name, _city_jobs_path(city) %></h4>
<p>Jobs: <span class="badge"><%= city.jobs_count %></span></p> <p>Jobs: <span class="badge"><%= city.jobs_count %></span></p>
</div> </div>
</div> </div>
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
<%- if city.country_name.eql?(Country::VIETNAM) -%> <%- if city.country_name.eql?(Country::VIETNAM) -%>
<div class="col-md-3 maxH109"> <div class="col-md-3 maxH109">
<div class="job-intro well mr0 mrBot20 maxH89"> <div class="job-intro well mr0 mrBot20 maxH89">
<h4 class="mr0"><%= link_to city.name, "#{jobs_path}/city/#{city.id}" %></h4> <h4 class="mr0"><%= link_to city.name, _city_jobs_path(city) %></h4>
<p>Jobs: <span class="badge"><%= city.jobs_count %></span></p> <p>Jobs: <span class="badge"><%= city.jobs_count %></span></p>
</div> </div>
</div> </div>
......
<div class="col-md-<%= column %> maxH109"> <div class="col-md-<%= column %> maxH109">
<div class="job-intro well mr0 mrBot20 maxH89"> <div class="job-intro well mr0 mrBot20 maxH89">
<h4 class="mr0"><%= link_to industry.name, "#{jobs_path}/industry/#{industry.id}" %></h4> <h4 class="mr0"><%= link_to industry.name, _industry_jobs_path(industry) %></h4>
<p>Jobs: <span class="badge"><%= industry.jobs_count %></span></p> <p>Jobs: <span class="badge"><%= industry.jobs_count %></span></p>
</div> </div>
</div> </div>
...@@ -5,13 +5,13 @@ ...@@ -5,13 +5,13 @@
<li> <li>
<%- @job.cities.each_with_index do |city, index| -%> <%- @job.cities.each_with_index do |city, index| -%>
<%= "#{index > 0 ? ',' : ''}" %> <%= "#{index > 0 ? ',' : ''}" %>
<%= link_to city.name, "#{jobs_path}/city/#{city.id}" %> <%= link_to city.name, _city_jobs_path(city) %>
<%- end -%> <%- end -%>
</li> </li>
<li> <li>
<%- @job.industries.each_with_index do |industry, index| -%> <%- @job.industries.each_with_index do |industry, index| -%>
<%= "#{index > 0 ? ',' : ''}" %> <%= "#{index > 0 ? ',' : ''}" %>
<%= link_to industry.name, "#{jobs_path}/industry/#{industry.id}" %> <%= link_to industry.name, _industry_jobs_path(industry) %>
<%- end -%> <%- end -%>
</li> </li>
<li><%= @job.name %></li> <li><%= @job.name %></li>
...@@ -20,11 +20,11 @@ ...@@ -20,11 +20,11 @@
<div class="job"> <div class="job">
<div class="col-md-9"> <div class="col-md-9">
<h1>2.<%= @job.name %></h1> <h1>2.<%= @job.name %></h1>
<p>3.<%= link_to @job.company.name, "#{jobs_path}/company/#{@job.company.id}" %></p> <p>3.<%= link_to @job.company.name, _company_jobs_path(@job.company) %></p>
<p>4. Location: <p>4. Location:
<%- @job.cities.each_with_index do |city, index| -%> <%- @job.cities.each_with_index do |city, index| -%>
<%= "#{index > 0 ? ',' : ''}" %> <%= "#{index > 0 ? ',' : ''}" %>
<%= link_to city.name, "#{jobs_path}/city/#{city.id}" %> <%= link_to city.name, _city_jobs_path(city) %>
<%- end -%> <%- end -%>
</p> </p>
<p>5. Salary: <%= @job.salary %></p> <p>5. Salary: <%= @job.salary %></p>
...@@ -33,11 +33,11 @@ ...@@ -33,11 +33,11 @@
</p> </p>
</div> </div>
<div class="col-md-3"> <div class="col-md-3">
<%= link_to "Aplly", "#", class: "btn btn-primary btn-lg" %> <%= link_to "Apply", "#", class: "btn btn-primary btn-lg" %>
</div> </div>
<div class="action"> <div class="action">
<div class="col-md-6"> <div class="col-md-6">
<%= link_to "Aplly", "#", class: "btn btn-default btn-lg" %> <%= link_to "Apply", "#", class: "btn btn-default btn-lg" %>
</div> </div>
<div class="col-md-6"> <div class="col-md-6">
<%= link_to "Favorite", "#", class: "btn btn-default btn-lg" %> <%= link_to "Favorite", "#", class: "btn btn-default btn-lg" %>
......
...@@ -5,9 +5,9 @@ Rails.application.routes.draw do ...@@ -5,9 +5,9 @@ Rails.application.routes.draw do
get 'cities' => 'cities#index' get 'cities' => 'cities#index'
get 'industries' => "industries#index" get 'industries' => "industries#index"
collection do collection do
get 'city/:id' => "jobs#city" get 'city/:city_id' => "jobs#city", as: :_city
get 'industry/:id' => "jobs#industry" get 'industry/:industry_id' => "jobs#industry", as: :_industry
get 'company/:id' => "jobs#company" get 'company/:company_id' => "jobs#company", as: :_company
end end
end end
root 'jobs#index' root 'jobs#index'
......
class AddJobsCountToCities < ActiveRecord::Migration[5.1]
def change
add_column :cities, :jobs_count, :integer, default: 0
end
end
class AddJobsCountToIndustries < ActiveRecord::Migration[5.1]
def change
add_column :industries, :jobs_count, :integer, default: 0
end
end
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# 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: 20171005085453) do ActiveRecord::Schema.define(version: 20171016030017) do
create_table "apply_jobs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| create_table "apply_jobs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.bigint "job_id" t.bigint "job_id"
...@@ -27,21 +27,22 @@ ActiveRecord::Schema.define(version: 20171005085453) do ...@@ -27,21 +27,22 @@ ActiveRecord::Schema.define(version: 20171005085453) do
t.bigint "country_id" t.bigint "country_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "jobs_count", default: 0
t.index ["country_id"], name: "index_cities_on_country_id" t.index ["country_id"], name: "index_cities_on_country_id"
end end
create_table "cities_companies", id: false, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| create_table "cities_companies", id: false, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.bigint "city_id", null: false t.bigint "company_id"
t.bigint "company_id", null: false t.bigint "city_id"
t.index ["city_id", "company_id"], name: "index_cities_companies_on_city_id_and_company_id" t.index ["city_id"], name: "index_cities_companies_on_city_id"
t.index ["company_id", "city_id"], name: "index_cities_companies_on_company_id_and_city_id" t.index ["company_id"], name: "index_cities_companies_on_company_id"
end end
create_table "cities_jobs", id: false, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| create_table "cities_jobs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.bigint "job_id", null: false t.bigint "job_id"
t.bigint "city_id", null: false t.bigint "city_id"
t.index ["city_id", "job_id"], name: "index_cities_jobs_on_city_id_and_job_id" t.index ["city_id"], name: "index_cities_jobs_on_city_id"
t.index ["job_id", "city_id"], name: "index_cities_jobs_on_job_id_and_city_id" t.index ["job_id"], name: "index_cities_jobs_on_job_id"
end end
create_table "companies", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| create_table "companies", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
...@@ -71,6 +72,7 @@ ActiveRecord::Schema.define(version: 20171005085453) do ...@@ -71,6 +72,7 @@ ActiveRecord::Schema.define(version: 20171005085453) do
t.string "name" t.string "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
t.integer "jobs_count", default: 0
end end
create_table "industries_jobs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| create_table "industries_jobs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
...@@ -112,6 +114,10 @@ ActiveRecord::Schema.define(version: 20171005085453) do ...@@ -112,6 +114,10 @@ ActiveRecord::Schema.define(version: 20171005085453) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "cv" t.string "cv"
t.string "name" t.string "name"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end end
......
...@@ -7,4 +7,14 @@ namespace :data do ...@@ -7,4 +7,14 @@ namespace :data do
@data = Crawler.crawl_job_infomation(links) @data = Crawler.crawl_job_infomation(links)
Job.create_new_jobs(@data) Job.create_new_jobs(@data)
end end
desc "reset counter industry"
task reset_counter_industry: :environment do |t|
Industry.find_each { |industry| Industry.reset_counters(industry.id, :jobs) }
end
desc "reset counter city"
task reset_counter_job: :environment do |t|
City.find_each { |city| City.reset_counters(city.id, :jobs) }
end
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