Commit 9ca3a5e6 by Tô Ngọc Ánh

create slug for location, industry

parent f1070925
Pipeline #813 failed with stages
in 0 seconds
class JobsController < ApplicationController
def city_job
location = Location.find_by_id(params[:id])
results(location)
end
def industry_job
industry = Industry.find_by_id(params[:id])
results(industry)
end
def search
end
private
def results(object)
@keyword = object.try(:name) || object.try(:city)
@jobs = object.jobs.all.includes(:company, :locations, :industries).page(params[:page])
def index
@locations = Location.select(:id, :city)
@industries = Industry.select(:id, :name)
object = params[:model].classify.constantize.find_by_slug(params[:slug])
@keyword = object.try(:name) || object.try(:city)
@jobs = object.jobs.all.includes(:company, :locations, :industries).page(params[:page])
render 'job_list'
end
end
require './lib/common/convert_slug'
class Industry < ApplicationRecord
include ConvertSlug
scope :with_count_job, -> { joins(:jobs).group(:industry_id).select('industries.*, count(jobs.id) as job_count') }
scope :top_industries, ->(number) { with_count_job.order(Arel.sql('job_count DESC')).limit(number) }
has_and_belongs_to_many :jobs
before_save :add_slug
private
def add_slug
self.slug = "#{self.id}-#{self.to_slug(self.name)}"
end
end
require './lib/common/convert_slug.rb'
class Location < ApplicationRecord
include ConvertSlug
scope :with_count_job, -> { joins(:jobs).group(:location_id).select('locations.*, count(jobs.id) as job_count') }
scope :top_locations, ->(number) { with_count_job.order(Arel.sql('job_count DESC')).limit(number) }
scope :international, -> { where(oversea: true) }
......@@ -8,4 +12,12 @@ class Location < ApplicationRecord
has_many :locations_jobs
has_many :jobs, through: :locations_jobs
before_save :add_slug
private
def add_slug
self.slug = "#{self.id}-#{self.to_slug(self.city)}"
end
end
<div class='col-4 my-2'>
<div class='card'>
<%= link_to industry_jobs_path(industry), class: 'card-body text-decoration-none' do %>
<%= link_to jobs_path(model: 'industry', slug: industry.slug), class: 'card-body text-decoration-none' do %>
<h5 class='card-title font-weight-bold'><%= industry.name %></h5>
<p class='card-text'><%= pluralize(industry.job_count, 'Job') %></p>
<% end %>
......
<div class='col-4 my-2'>
<div class='card'>
<%= link_to city_jobs_path(location), class: 'card-body text-decoration-none' do %>
<%= link_to jobs_path(model: 'location', slug: location.slug), class: 'card-body text-decoration-none' do %>
<h5 class='card-title font-weight-bold'><%= location.city %></h5>
<p class='card-text'><%= pluralize(location.job_count, 'Job') %></p>
<% end %>
......
......@@ -2,7 +2,6 @@ Rails.application.routes.draw do
root to: 'home#index'
get 'cities', to: 'locations#index'
get 'industries', to: 'industries#index'
get 'jobs/city/:id', to: 'jobs#city_job', as: :city_jobs
get 'jobs/industry/:id', to: 'jobs#industry_job', as: :industry_jobs
get 'jobs/:model/:slug', to: 'jobs#index', as: :jobs
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
class AddSlugToIndustries < ActiveRecord::Migration[5.2]
def change
add_column :industries, :slug, :string
add_index :industries, :slug, unique: true
end
end
class AddSlugToLocations < ActiveRecord::Migration[5.2]
def change
add_column :locations, :slug, :string
add_index :locations, :slug, 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: 2020_07_20_075150) do
ActiveRecord::Schema.define(version: 2020_08_05_034659) do
create_table "applied_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.bigint "user_id"
......@@ -55,6 +55,8 @@ ActiveRecord::Schema.define(version: 2020_07_20_075150) do
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.index ["slug"], name: "index_industries_on_slug", unique: true
end
create_table "industries_jobs", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
......@@ -82,6 +84,8 @@ ActiveRecord::Schema.define(version: 2020_07_20_075150) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "oversea"
t.string "slug"
t.index ["slug"], name: "index_locations_on_slug", unique: true
end
create_table "locations_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
......
module ConvertSlug
def to_slug(str)
str = str.to_s.strip.downcase
accents = {
%w[à á ạ ả ã â ầ ấ ậ ẩ ẫ ă ằ ắ ặ ẳ ẵ] => 'a',
%w[è é ẹ ẻ ẽ ê ề ế ệ ể ễ] => 'e',
%w[ì í ị ỉ ĩ] => 'i',
%w[ò ó ọ ỏ õ ô ồ ố ộ ổ ỗ ơ ờ ớ ợ ở ỡ] => 'o',
%w[ù ú ụ ủ ũ ư ừ ứ ự ử ữ] => 'u',
%w[ỳ ý ỵ ỷ ỹ] => 'y',
%w[đ] => 'd'
}
accents.each do |ac, rep|
ac.each do |s|
str = str.tr(s, rep)
end
end
str.parameterize
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