Commit e2e10771 by nnnghia98

split job import file

parent 36ffa23a
...@@ -25,7 +25,6 @@ gem 'sunspot_rails' ...@@ -25,7 +25,6 @@ gem 'sunspot_rails'
gem 'sunspot_solr' gem 'sunspot_solr'
gem 'devise' gem 'devise'
gem 'activerecord-import' gem 'activerecord-import'
gem 'pry'
# Use Redis adapter to run Action Cable in production # Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0' # gem 'redis', '~> 4.0'
# Use Active Model has_secure_password # Use Active Model has_secure_password
...@@ -40,6 +39,7 @@ gem 'bootsnap', '>= 1.4.2', require: false ...@@ -40,6 +39,7 @@ gem 'bootsnap', '>= 1.4.2', require: false
group :development, :test do group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console # Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'pry'
end end
group :development do group :development do
......
...@@ -5,23 +5,16 @@ class Import ...@@ -5,23 +5,16 @@ class Import
cities = [] cities = []
companies = [] companies = []
industries = [] industries = []
jobs = []
city_columns = [:name, :region] city_columns = [:name, :region]
company_columns = [:name, :email, :address, :code] company_columns = [:name, :email, :address, :code]
industry_columns = [:name] industry_columns = [:name]
job_columns = [:title, :level, :salary, :description, :short_des, :requirement,
:category, :company_id]
CSV.foreach(Rails.root.join("lib", "jobss.csv"), headers: true) do |row| CSV.foreach(Rails.root.join("lib", "jobss.csv"), headers: true) do |row|
cities << {name: row["company province"], region: "Việt Nam"} cities << {name: row["company province"], region: "Việt Nam"}
companies << {name: row["company name"], email: row["contact email"], companies << {name: row["company name"], email: row["contact email"],
address: row["company address"], code: row["company id"]} address: row["company address"], code: row["company id"]}
industries << {name: row["category"]} industries << {name: row["category"]}
jobs << {title: row["name"], level: row["level"], salary: row["salary"],
description: row["description"], short_des: row["benefit"],
requirement: row["requirement"], category: row["type"],
company_id: Company.find_by(code: row["company id"])&.id}
end end
City.import city_columns, cities, on_duplicate_key_ignore: true City.import city_columns, cities, on_duplicate_key_ignore: true
...@@ -32,8 +25,5 @@ class Import ...@@ -32,8 +25,5 @@ class Import
Industry.import industry_columns, industries, on_duplicate_key_ignore: true Industry.import industry_columns, industries, on_duplicate_key_ignore: true
puts "Industries imported" puts "Industries imported"
Job.import job_columns, jobs
puts "Jobs imported"
end end
end end
class JobCsv
def initialize(row)
@row = row
end
def title
@title ||= @row["title"]
end
def level
@level ||= @row["level"]
end
def salary
@salary ||= @row["salary"]
end
def description
@description ||= @row["description"]
end
def short_des
@short_des ||= @row["benefit"]
end
def requirement
@requirement ||= @row["requirement"]
end
def category
@category ||= @row["type"]
end
def company_id
@company_id ||= company.id
end
def company
@company ||= Company.find_or_create_by(code: @row["company id"])
end
def csv_attributes
{title: self.title, level: self.level, salary: self.salary, description: self.description,
short_des: self.short_des, requirement: self.requirement, category: self.category,
company_id: self.company_id}
end
end
require "csv"
class JobsImport
def import_job
jobs = []
job_columns = [:title, :level, :salary, :description, :short_des,
:requirement, :category, :company_id]
CSV.foreach(Rails.root.join("lib", "jobss.csv"), headers: true) do |row|
jobcsv = JobCsv.new(row)
jobs << jobcsv.csv_attributes
end
Job.import job_columns, jobs
puts "Jobs imported"
end
end
<table class="table"> <%= render partial: "cities/city", collection: @cities %>
<thead>
<tr>
<th scope="col">name</th>
<th scope="col">region</th>
</tr>
</thead>
<tbody>
<% @cities.each do |city| %>
<tr>
<td><%= city.name %></td>
<td><%= city.region %></td>
</tr>
<% end %>
</tbody>
</table>
<table class="table"> <%= render partial: "industries/industry", collection: @industries %>
<thead>
<tr>
<th scope="col">name</th>
</tr>
</thead>
<tbody>
<% @industries.each do |industry| %>
<tr>
<td><%= industry.name %></td>
</tr>
<% end %>
</tbody>
</table>
<table class="table"> <%= render partial: "jobs/job", collection: @jobs %>
<thead>
<tr>
<th scope="col">title</th>
<th scope="col">short description</th>
<th scope="col">salary</th>
</tr>
</thead>
<tbody>
<% @jobs.each do |job| %>
<tr>
<td><%= job.title %></td>
<td><%= job.short_des %></td>
<td><%= job.salary %></td>
</tr>
<% end %>
</tbody>
</table>
# require "./app/services/import.rb"
namespace :task do namespace :task do
desc "import data" desc "import data"
task import: :environment do task import: :environment do
Import.new.import Import.new.import
JobsImport.new.import_job
end 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