Commit 03ee3000 by Tô Ngọc Ánh

create scope select top locations, industries

parent e175a0c0
Pipeline #744 failed with stages
in 0 seconds
...@@ -3,18 +3,8 @@ class HomeController < ApplicationController ...@@ -3,18 +3,8 @@ class HomeController < ApplicationController
@total_jobs = Job.count @total_jobs = Job.count
@locations = Location.all @locations = Location.all
@industries = Industry.all @industries = Industry.all
@jobs = Job.order(created_at: :desc).limit(6) @jobs = Job.order(created_at: :desc).limit(Job::NUMBER_LATEST_JOB)
@top_cities = list_top_of(@locations, 9) @top_cities = Location.top_locations(9)
@top_industries = list_top_of(@industries, 9) @top_industries = Industry.top_industries(9)
end
private
def list_top_of(list_object, number)
top_object = {}
list_object.each do |object|
top_object[object.id] = object.jobs.count
end
top_object.sort_by { |_k, v| v }.reverse.take(number)
end end
end end
class Industry < ApplicationRecord class Industry < ApplicationRecord
scope :top_industries, ->(number) { joins(:jobs).group(:industry_id).order(Arel.sql('count(jobs.id) DESC')).take(number) }
has_and_belongs_to_many :jobs has_and_belongs_to_many :jobs
end end
class Job < ApplicationRecord class Job < ApplicationRecord
NUMBER_LATEST_JOB = 6
belongs_to :company belongs_to :company
has_many :applied_jobs has_many :applied_jobs
has_many :histories has_many :histories
......
class Location < ApplicationRecord class Location < ApplicationRecord
CITY_VIETNAM_NUMBER = 70.freeze scope :top_locations, ->(number) { joins(:jobs).group(:location_id).order(Arel.sql('count(jobs.id) DESC')).take(number) }
CITY_VIETNAM_NUMBER = 70
has_many :locations_jobs has_many :locations_jobs
has_many :jobs, through: :locations_jobs has_many :jobs, through: :locations_jobs
......
<% location = Location.find_by(id: object[0]) %>
<div class='col-4 my-2'> <div class='col-4 my-2'>
<div class='card'> <div class='card'>
<%= link_to '#', class: 'card-body text-decoration-none' do %> <%= link_to '#', class: 'card-body text-decoration-none' do %>
<h5 class='card-title font-weight-bold'><%= location.city %></h5> <h5 class='card-title font-weight-bold'><%= location.city %></h5>
<p class='card-text'><%= object[1] %> Jobs</p> <p class='card-text'><%= location.jobs.size %> Jobs</p>
<% end %> <% end %>
</div> </div>
</div> </div>
<% industry = Industry.find_by(id: object[0]) %>
<div class='col-4 my-2'> <div class='col-4 my-2'>
<div class='card'> <div class='card'>
<%= link_to '#', class: 'card-body text-decoration-none' do %> <%= link_to '#', class: 'card-body text-decoration-none' do %>
<h5 class='card-title font-weight-bold'><%= industry.name %></h5> <h5 class='card-title font-weight-bold'><%= industry.name %></h5>
<p class='card-text'><%= object[1] %> Jobs</p> <p class='card-text'><%= industry.jobs.size %> Jobs</p>
<% end %> <% end %>
</div> </div>
</div> </div>
...@@ -16,14 +16,14 @@ ...@@ -16,14 +16,14 @@
<div id='top-city' class='my-4 text-center'> <div id='top-city' class='my-4 text-center'>
<h1>Top Cities</h1> <h1>Top Cities</h1>
<div class='row'> <div class='row'>
<%= render partial: 'home/city', collection: @top_cities, as: :object %> <%= render partial: 'home/city', collection: @top_cities, as: :location %>
</div> </div>
<a href='#'>See more...</a> <a href='#'>See more...</a>
</div> </div>
<div id='top-industry' class='my-4 text-center'> <div id='top-industry' class='my-4 text-center'>
<h1>Top Industries</h1> <h1>Top Industries</h1>
<div class='row'> <div class='row'>
<%= render partial: 'home/industry', collection: @top_industries, as: :object %> <%= render partial: 'home/industry', collection: @top_industries, as: :industry %>
</div> </div>
<a href='#'>See more...</a> <a href='#'>See more...</a>
</div> </div>
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