Commit d06a8465 by Tô Ngọc Ánh

Add slug for company

parent e1c9cc35
Pipeline #839 failed with stages
in 0 seconds
require './lib/common/convert_slug'
class Company < ApplicationRecord class Company < ApplicationRecord
include ConvertSlug
has_many :jobs has_many :jobs
before_save :add_slug, unless: :slug
private
def add_slug
self.slug = "#{self.to_slug(self.name)}-#{Time.now.to_i}#{rand(10000)}"
end
end end
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
<div class="col-10"> <div class="col-10">
<h2 class="font-weight-bold"><%= @job.title %></h2> <h2 class="font-weight-bold"><%= @job.title %></h2>
<h4><i class="fas fa-building"></i> <h4><i class="fas fa-building"></i>
<%= link_to @job.company.name, '#', class: 'text-decoration-none text-dark' %> <%= link_to @job.company.name, jobs_path(model: 'company', slug: @job.company.slug), class: 'text-decoration-none text-dark' %>
</h4> </h4>
<span class="mx-2"><i class="fas fa-map-marker-alt"> </i> <span class="mx-2"><i class="fas fa-map-marker-alt"> </i>
<% @job.locations.each do |location| %> <% @job.locations.each do |location| %>
......
class AddSlugToCompanies < ActiveRecord::Migration[5.2]
def change
add_column :companies, :slug, :string
add_index :companies, :slug, unique: true
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: 2020_08_05_034659) do ActiveRecord::Schema.define(version: 2020_08_06_093557) do
create_table "applied_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| create_table "applied_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.bigint "user_id" t.bigint "user_id"
...@@ -30,7 +30,9 @@ ActiveRecord::Schema.define(version: 2020_08_05_034659) do ...@@ -30,7 +30,9 @@ ActiveRecord::Schema.define(version: 2020_08_05_034659) do
t.string "address" t.string "address"
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.string "slug"
t.index ["name"], name: "index_companies_on_name", unique: true t.index ["name"], name: "index_companies_on_name", unique: true
t.index ["slug"], name: "index_companies_on_slug", unique: true
end end
create_table "favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| create_table "favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
......
...@@ -13,6 +13,13 @@ namespace :create_slug do ...@@ -13,6 +13,13 @@ namespace :create_slug do
end end
end end
desc 'Create slug model Company'
task companies: :environment do
Company.all.each do |company|
company.update_attributes(slug: "#{company.to_slug(company.name)}-#{Time.now.to_i}#{rand(10000)}")
end
end
desc 'Create slug to all model' desc 'Create slug to all model'
task all: %i[locations industries] task all: %i[locations industries companies]
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