Commit 753933f5 by Quang Vinh Nguyen

add jobs controller and resign City and Industry model

parent 67a7eb97
......@@ -15,7 +15,7 @@ class JobsController < ApplicationController
def jobs_in_city
@city = City.find_by(slug: params[:slug])
@jobs = get_jobs_in_city(@city.id).page(params[:page])
@jobs = @city.jobs.page(params[:page])
end
def home
......@@ -23,10 +23,6 @@ class JobsController < ApplicationController
@cities = City.all.select{ |city| city.jobs.any? }.take(8)
end
def get_jobs_in_city(city_id)
Job.where(city_id: city_id)
end
def get_jobs_rsolr(title = '*')
solr = RSolr.connect url: 'http://localhost:8983/solr/gettingstarted/'
search_params = { q: "search_text:*#{title.downcase}*", rows: 5_000 }
......
module JobsHelper
def number_of_jobs_in_city(city_id)
jobs = Job.where(city_id: city_id)
jobs.count
end
end
class CitiesJob < ApplicationRecord
belongs_to :job, foreign_key: 'job_id'
belongs_to :city, foreign_key: 'city_id'
validates :job_id, presence: true
validates :city_id, presence: true
end
# frozen_string_literal: true
class City < ApplicationRecord
has_many :jobs, dependent: :destroy
has_many :cities_jobs, foreign_key: 'city_id',
dependent: :destroy
has_many :jobs, through: :cities_jobs
validates :name, presence: true, length: { maximum: 255 },
uniqueness: { case_sensitive: false }
validates :city_type, length: { maximum: 255 }
......
class IndustriesJob < ApplicationRecord
belongs_to :job, foreign_key: 'job_id'
belongs_to :industry, foreign_key: 'industry_id'
validates :job_id, presence: true
validates :industry_id, presence: true
end
class Industry < ApplicationRecord
has_many :jobs, dependent: :destroy
has_many :industries_jobs, foreign_key: 'industry_id',
dependent: :destroy
has_many :jobs, through: :industries_jobs
validates :name, presence: true, length: { maximum: 255 },
uniqueness: { case_sensitive: false }
......
......@@ -2,8 +2,14 @@
class Job < ApplicationRecord
belongs_to :company, foreign_key: 'company_id'
belongs_to :city, foreign_key: 'city_id'
belongs_to :industry, foreign_key: 'industry_id'
has_many :cities_jobs, foreign_key: 'job_id',
dependent: :destroy
has_many :cities, through: :cities_jobs
has_many :industries_jobs, foreign_key: 'job_id',
dependent: :destroy
has_many :industries, through: :industries_jobs
has_many :entries, dependent: :destroy
has_many :favorite_jobs, dependent: :destroy
......@@ -30,7 +36,6 @@ class Job < ApplicationRecord
paginates_per 10
def self.search(search)
# where("name LIKE ? OR ingredients LIKE ? OR cooking_instructions LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%")
where("title LIKE ?", "%#{search}%")
end
end
......@@ -4,7 +4,7 @@
<% @cities.each do |city| %>
<li class = 'list-group-item'>
<%= city.name %>
<%= link_to pluralize(number_of_jobs_in_city(city.id), 'job'),
<%= link_to pluralize(city.jobs.count, 'job'),
controller: 'jobs',
action: 'jobs_in_city',
slug: city.slug %>
......
......@@ -25,7 +25,7 @@
<% @cities.each do |city| %>
<li>
<%= city.name %>
<%= link_to pluralize(number_of_jobs_in_city(city.id), 'job'),
<%= link_to pluralize(city.jobs.count, 'job'),
controller: 'jobs',
action: 'jobs_in_city',
slug: city.slug %>
......
<h4><%= @city.name %> have <%= pluralize(number_of_jobs_in_city(@city.id), 'job') %></h4>
<%#= link_to 'Back', city_jobs_path %>
<h4><%= @city.name %>: <%= pluralize(@city.jobs.count, 'job') %></h4>
<div class='container'>
<div class='jobs-list'>
<ul>
......
......@@ -11,5 +11,6 @@ class CreateCities < ActiveRecord::Migration[5.2]
t.timestamps
end
add_index :cities, [:name, :slug]
end
end
class CreateIndustries < ActiveRecord::Migration[5.2]
def change
create_table :industries do |t|
t.string :code
t.string :name
t.string :slug
t.timestamps
end
add_index :industries, :name
end
end
......@@ -3,8 +3,6 @@ class CreateJobs < ActiveRecord::Migration[5.2]
create_table :jobs do |t|
t.string :title, index: true
t.references :company, foreign_key: true
t.references :city, foreign_key: true
t.references :industry, foreign_key: true
t.string :position
t.decimal :salary, precision: 12, scale: 2
t.datetime :expiry_date
......
class CreateIndustriesJobs < ActiveRecord::Migration[5.2]
def change
create_table :industries_jobs do |t|
t.references :job, foreign_key: true
t.references :industry, foreign_key: true
t.timestamps
end
add_index :industries_jobs, [:job_id, :industry_id], unique: true
end
end
class CreateCitiesJobs < ActiveRecord::Migration[5.2]
def change
create_table :cities_jobs do |t|
t.references :job, foreign_key: true
t.references :city, foreign_key: true
t.timestamps
end
add_index :cities_jobs, [:job_id, :city_id], unique: true
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_06_12_070531) do
ActiveRecord::Schema.define(version: 2018_06_19_011959) do
create_table "cities", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "name"
......@@ -22,6 +22,17 @@ ActiveRecord::Schema.define(version: 2018_06_12_070531) do
t.integer "parent_code"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["name", "slug"], name: "index_cities_on_name_and_slug"
end
create_table "cities_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.bigint "job_id"
t.bigint "city_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
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", unique: true
t.index ["job_id"], name: "index_cities_jobs_on_job_id"
end
create_table "companies", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
......@@ -55,16 +66,27 @@ ActiveRecord::Schema.define(version: 2018_06_12_070531) do
end
create_table "industries", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "code"
t.string "name"
t.string "slug"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["name"], name: "index_industries_on_name"
end
create_table "industries_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.bigint "job_id"
t.bigint "industry_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["industry_id"], name: "index_industries_jobs_on_industry_id"
t.index ["job_id", "industry_id"], name: "index_industries_jobs_on_job_id_and_industry_id", unique: true
t.index ["job_id"], name: "index_industries_jobs_on_job_id"
end
create_table "jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "title"
t.bigint "company_id"
t.bigint "city_id"
t.bigint "industry_id"
t.string "position"
t.decimal "salary", precision: 12, scale: 2
t.datetime "expiry_date"
......@@ -76,9 +98,7 @@ ActiveRecord::Schema.define(version: 2018_06_12_070531) do
t.text "link"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["city_id"], name: "index_jobs_on_city_id"
t.index ["company_id"], name: "index_jobs_on_company_id"
t.index ["industry_id"], name: "index_jobs_on_industry_id"
t.index ["title", "update_date"], name: "index_jobs_on_title_and_update_date"
t.index ["title"], name: "index_jobs_on_title"
end
......@@ -104,11 +124,13 @@ ActiveRecord::Schema.define(version: 2018_06_12_070531) do
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "cities_jobs", "cities"
add_foreign_key "cities_jobs", "jobs"
add_foreign_key "entries", "jobs"
add_foreign_key "entries", "users"
add_foreign_key "favorite_jobs", "jobs"
add_foreign_key "favorite_jobs", "users"
add_foreign_key "jobs", "cities"
add_foreign_key "industries_jobs", "industries"
add_foreign_key "industries_jobs", "jobs"
add_foreign_key "jobs", "companies"
add_foreign_key "jobs", "industries"
end
......@@ -9,16 +9,15 @@ namespace :importdb do
desc 'import data to cities table'
task cities: :environment do
provs = Redis.new
provs_hash_arr = provs.smembers 'cities'
provs_hash = JSON.parse(provs_hash_arr[0])
provs_hash.each do |_code, prov|
City.create!(name: prov['name'],
slug: prov['slug'],
city_type: prov['type'],
name_with_type: prov['name_with_type'],
code: prov['code'])
end
city_redis = Redis.new
city_yaml_arr = city_redis.smembers('cities')
city_yaml_arr.each do |row|
city = YAML.safe_load(row)
next if City.find_by(name: city[1])
City.create!( code: city[0],
name: city[1],
slug: city[2])
end
end
desc 'import data to companies table'
......@@ -36,11 +35,14 @@ namespace :importdb do
desc 'seed industries data'
task industries: :environment do
path = Rails.root.join('db', 'industries.csv')
CSV.foreach(path) do |csv|
Industry.create(
name: csv[1]
)
inds_redis = Redis.new
inds_yaml_arr = inds_redis.smembers('industry')
inds_yaml_arr.each do |row|
ind = YAML.safe_load(row)
next if Industry.find_by(name: ind[1])
Industry.create!( code: ind[0],
name: ind[1],
slug: ind[2] )
end
end
......@@ -48,36 +50,44 @@ namespace :importdb do
task jobs: :environment do
jobs_redis = Redis.new
jobs_yaml_arr = jobs_redis.smembers 'crawled'
jobs_yaml_arr.each do |row|
jobs_yaml_arr.each_with_index do |row,id|
# break if id == 3
job = YAML.safe_load(row)
next if job[1].blank? || !!Job.find_by(title: job[1]) || !!Job.find_by(link: job[14])
Job.create!(
title: job[1],
company_id: 1,
city_id: get_city_id(job[3]),
industry_id: 1,
position: 'NA',
salary: '1_000',
expiry_date: Time.now,
description: 'NA',
update_date: Time.now,
published: true,
welfare: 'NA',
condition: 'NA',
link: job[14]
)
end
end
job_new = Job.create!(
title: job[1],
company_id: 1,
position: 'NA',
salary: '1_000',
expiry_date: Time.now,
description: 'NA',
update_date: Time.now,
published: true,
welfare: 'NA',
condition: 'NA',
link: job[14]
)
# import job industry
industry_arr = job[5].split('+').map { |ind| ind.strip }
if industry_arr.any?
industry_arr.each do |ind|
ind_id = Industry.find_by(name: ind).id || Industry.first.id
job_new.industries_jobs.create!(industry_id: ind_id)
end
else
job_new.industries_jobs.create!(industry_id: City.first.id)
end
# Return id of city id database
def get_city_id(city_name = 'Hồ Chí Minh')
city_query = City.where(name: city_name)
if city_query.any?
city_query.each do |city|
return city['id']
# import job industry
city_arr = job[3].split(',').map { |city| city.strip }
if city_arr.any?
city_arr.each do |city|
city_id = City.find_by(name: city).id || City.first.id
job_new.cities_jobs.create!(city_id: city_id)
end
else
job_new.cities_jobs.create!(city_id: City.first.id)
end
else
return 59
end
end
end
# frozen_string_literal: true
namespace :update do
desc 'update solr index from database'
task solr: :environment do
solr = RSolr.connect url: 'http://localhost:8983/solr/gettingstarted/'
jobs = Job.all
if jobs.any?
jobs.each_with_index do |job, _id|
# break if id == 102
# check is job_id is exist and not update job to solr
response_solr = solr.get 'select', params: { q: "job_id: #{job['id']}" }
next if response_solr['response']['docs'].any?
# hash of job attribute array: city_name, ...
h = {}
h['city_id'] = job.cities.collect { |city| city['id'].to_s }
h['city_name'] = job.cities.collect { |city| city['name'].downcase }
h['city_slug'] = job.cities.collect { |city| city['slug'] }
h['industry_id'] = job.industries.collect { |ind| ind['id'].to_s }
h['industry_name'] = job.industries.collect { |ind| ind['name'].downcase }
h['industry_slug'] = job.industries.collect { |ind| ind['slug'] }
doc = {
job_id: job.id,
job_title: job.title.downcase,
job_city_id: h['city_id'],
job_city_name: h['city_name'],
job_city_slug: h['city_slug'],
job_industry_id: h['industry_id'],
job_industry_name: h['industry_name'],
job_industry_slug: h['industry_slug']
}
solr.add [doc]
end
solr.commit
end
end
end
FactoryGirl.define do
factory :cities_job do
job nil
city nil
end
end
FactoryGirl.define do
factory :industries_job do
job nil
industry nil
end
end
require 'rails_helper'
RSpec.describe CitiesJob, type: :model do
describe 'db schema' do
context 'index' do
it do
should have_db_index(:job_id)
should have_db_index(:city_id)
end
end
context 'foreign key' do
it do
should belong_to(:job).with_foreign_key('job_id')
should belong_to(:city).with_foreign_key('city_id')
end
end
end
describe 'validations' do
it do
should validate_presence_of(:job_id)
should validate_presence_of(:city_id)
end
end
end
......@@ -16,7 +16,8 @@ RSpec.describe City, type: :model do
should have_db_column(:parent_code).of_type(:integer)
end
it { should have_many(:jobs).dependent(:destroy) }
it { should have_many(:cities_jobs).dependent(:destroy) }
it { should have_many(:jobs).through(:cities_jobs) }
end
it 'right values input' do
......
require 'rails_helper'
RSpec.describe IndustriesJob, type: :model do
describe 'db schema' do
context 'index' do
it do
should have_db_index(:job_id)
should have_db_index(:industry_id)
end
end
context 'foreign key' do
it do
should belong_to(:job).with_foreign_key('job_id')
should belong_to(:industry).with_foreign_key('industry_id')
end
end
end
describe 'validations' do
it do
should validate_presence_of(:job_id)
should validate_presence_of(:industry_id)
end
end
end
......@@ -7,7 +7,8 @@ RSpec.describe Industry, type: :model do
describe 'db schema' do
it { should have_db_column(:name).of_type(:string) }
it { should have_many(:jobs).dependent(:destroy) }
it { should have_many(:industries_jobs).dependent(:destroy) }
it { should have_many(:jobs).through(:industries_jobs) }
end
it 'right values input' do
......
......@@ -19,22 +19,22 @@ RSpec.describe Job, type: :model do
it { should have_many(:entries).dependent(:destroy) }
it { should have_many(:favorite_jobs).dependent(:destroy) }
it { should have_many(:cities_jobs).dependent(:destroy) }
it { should have_many(:cities).through(:cities_jobs) }
it { should have_many(:industries_jobs).dependent(:destroy) }
it { should have_many(:industries).through(:industries_jobs) }
end
context 'index' do
it do
should have_db_index(:title)
should have_db_index(:city_id)
should have_db_index(:company_id)
should have_db_index(:industry_id)
end
end
context 'foreign key' do
it do
should belong_to(:company).with_foreign_key('company_id')
should belong_to(:city).with_foreign_key('city_id')
should belong_to(:industry).with_foreign_key('industry_id')
end
end
end
......@@ -45,9 +45,7 @@ RSpec.describe Job, type: :model do
let(:job) do
FactoryGirl.create(
:job,
company_id: company.id,
city_id: city.id,
industry_id: industry.id
company_id: company.id
)
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