Commit aec2ab4b by Xuan Trung Le

fix errors and optimize code.

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