Commit 4eff9b54 by Ngô Trung Hưng

created function search applied for admin

parent 404a9c9c
Pipeline #1096 failed with stages
in 0 seconds
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
...@@ -117,6 +117,11 @@ ...@@ -117,6 +117,11 @@
font-size: 30px; font-size: 30px;
} }
.total_count_applied_job {
font-size: 18px;
font-weight: bold;
}
@media only screen and (max-width: 768px) { @media only screen and (max-width: 768px) {
.block_result_applied { .block_result_applied {
padding-top: 60px; padding-top: 60px;
......
# frozen_string_literal: true
class Searches
attr_accessor :params
def initialize(params)
@params = params
end
def search
debugger
@result_after_fillter_date = AppliedJob.where("created_at BETWEEN ? AND ?", parse_date[:past_time], parse_date[:currnet_time] )
end
def parse_date
dt = {}
dt[:past_time] = Date.new(params['past_time(1i)'].to_i, params['past_time(2i)'].to_i, params['past_time(3i)'].to_i)
dt[:currnet_time] = Date.new(params['current_time(1i)'].to_i, params['current_time(2i)'].to_i, params['current_time(3i)'].to_i)
dt
end
end
# frozen_string_literal: true
# Admin controller
class AdminController < ApplicationController class AdminController < ApplicationController
before_action :authenticate_admin!, only: :index before_action :authenticate_admin!, only: %s(index search export_csv)
before_action :load_data_dropdown before_action :load_data_dropdown
include InitApply
# user input in url 'localhost:3000/admin' if admin signed redirect to applies page else admin login page
def routing def routing
return redirect_to new_admin_session_path if !admin_signed_in? return redirect_to new_admin_session_path unless admin_signed_in?
redirect_to applies_path
redirect_to applies_index_path
end end
def index def index
debugger Rails.cache.delete(:search_result)
@applied = (Searches.new(params[:apply]).search if params[:apply].present?) || AppliedJob.order(created_at: :desc) @applied = AppliedJob.all_result.page(params[:page]).per(6)
@apply = Apply.new @apply = apply
end
def search
return redirect_to applies_index_path if params[:apply].blank?
Rails.cache.delete(:search_result)
Rails.cache.fetch(:search_result, expires_in: 1.day) { result_for(params[:apply]) }
@applied = result_for(params[:apply]).page(params[:page]).per(6)
@city_id = params[:apply][:city_id]
@industry_id = params[:apply][:industry_id]
@params_date = params[:apply]
@apply = apply
render :index
end
def export_csv
csv = ExportCsv.new(Rails.cache.fetch(:search_result) || AppliedJob.all_result, AppliedJob::CSV_ATTR)
respond_to do |format|
format.csv { send_data csv.perform, filename: "#{Time.now}applied.csv" }
end
end end
def load_data_dropdown def load_data_dropdown
@industries = Industry.order(name: :asc) @industries = Industry.order(name: :asc)
@cities = City.select(:id, :name) @cities = City.select(:id, :name)
end end
private
def result_for(params)
Searches.new(params).search
end
end end
# frozen_string_literal: true
module InitApply
def apply
Apply.new(applied_params)
end
private
def applied_params
return {} if params[:apply].blank?
params.require(:apply).permit(:email, :city_id, :industry_id)
end
end
# frozen_string_literal: true
class Apply class Apply
include ActiveModel::Model include ActiveModel::Model
attr_accessor :email, :city_id, :industry_id, :past_time, :current_time attr_accessor :email, :city_id, :industry_id, :past_time, :current_time
def self.parse_datetime(params) def self.selected(params)
times = {} params.blank? ? '*' : params
past_time = ''
current_time = ''
3.times do |i|
past_time << params["past_time(#{i+1}i)"] << '-'
current_time << params["current_time(#{i+1}i)"] << '-'
end end
times[:past_time] = past_time.chop
times[:current_time] = current_time.chop def self.selected_date(params)
times dt = {}
dt[:selected_past_year] = params.present? ? params['past_time(1i)'].to_i : Date.current.year
dt[:selected_past_month] = params.present? ? params['past_time(2i)'].to_i : Date.current.month
dt[:selected_past_day] = params.present? ? params['past_time(3i)'].to_i : Date.current.day
dt[:selected_current_year] = params.present? ? params['current_time(1i)'].to_i : Date.current.year
dt[:selected_current_month] = params.present? ? params['current_time(2i)'].to_i : Date.current.month
dt[:selected_current_day] = params.present? ? params['current_time(3i)'].to_i : Date.current.day
dt
end end
end end
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
# Description/Explanation of Person class # Description/Explanation of Person class
class AppliedJob < ApplicationRecord class AppliedJob < ApplicationRecord
CSV_ATTR = %w[job_id name email cv created_at].freeze
scope :all_result, -> { order(created_at: :desc) }
mount_uploader :cv, CvUploader mount_uploader :cv, CvUploader
belongs_to :user belongs_to :user
belongs_to :job belongs_to :job
......
# frozen_string_literal: true
class Searches
attr_accessor :params
def initialize(params)
@params = params
end
def search
return AppliedJob.none unless get_data_fillter
return get_data_fillter if [params[:city_id], params[:industry_id]].all?(&:blank?)
ids_job_by_city = params[:city_id].present? ? CityJob.where(city_id: params[:city_id]).pluck(:job_id) : []
ids_job_by_industry = params[:industry_id].present? ? IndustryJob.where(industry_id: params[:industry_id]).pluck(:job_id) : []
intersection = [ids_job_by_city, ids_job_by_industry].any?(&:blank?) ? ids_job_by_city | ids_job_by_industry : ids_job_by_city & ids_job_by_industry
return AppliedJob.none if intersection.blank?
get_data_fillter.where(job_id: get_data_fillter.pluck(:job_id) & intersection)
end
def get_data_fillter
past_time = DateTime.new(params['past_time(1i)'].to_i, params['past_time(2i)'].to_i, params['past_time(3i)'].to_i)
current_time = DateTime.new(params['current_time(1i)'].to_i, params['current_time(2i)'].to_i, params['current_time(3i)'].to_i)
return AppliedJob.none if past_time > current_time || current_time < past_time || [past_time, current_time].any?(&:blank?)
result = AppliedJob.where(created_at: past_time...current_time + 1.days)
params[:email].present? ? result.where(email: params[:email]) : result
end
end
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<div class="admin_box_search__filter_string"> <div class="admin_box_search__filter_string">
<div class="row"> <div class="row">
<div class="col-lg-12"> <div class="col-lg-12">
<%= f.text_field :email, placeholder: 'Email', class: 'form-control' %> <%= f.search_field :email, placeholder: 'Email', class: 'form-control' %>
</div> </div>
</div> </div>
</div> </div>
...@@ -17,12 +17,12 @@ ...@@ -17,12 +17,12 @@
<div class="row"> <div class="row">
<div class="col-lg-6"> <div class="col-lg-6">
<div class="admin_box_search__filter_city_id"> <div class="admin_box_search__filter_city_id">
<%= f.select :city_id, options_from_collection_for_select(@cities, 'id', 'name'), prompt: t('pages.banner.all_locations') %> <%= f.select :city_id, options_from_collection_for_select(@cities, 'id', 'name', Apply.selected(@city_id)), include_blank: t('pages.banner.all_locations') %>
</div> </div>
</div> </div>
<div class="col-lg-6"> <div class="col-lg-6">
<div class="admin_box_search__filter_industry_id"> <div class="admin_box_search__filter_industry_id">
<%= f.select :industry_id, options_from_collection_for_select(@industries, 'id', 'name'), prompt: t('pages.banner.all_industries') %> <%= f.select :industry_id, options_from_collection_for_select(@industries, 'id', 'name', Apply.selected(@industry_id)), include_blank: t('pages.banner.all_industries') %>
</div> </div>
</div> </div>
</div> </div>
...@@ -33,22 +33,28 @@ ...@@ -33,22 +33,28 @@
<div class="row"> <div class="row">
<div class="col-lg-6 col-md-6"> <div class="col-lg-6 col-md-6">
<div class="admin_box_search__filter_date__past_time"> <div class="admin_box_search__filter_date__past_time">
<%= f.date_select :past_time, order: [:year, :month, :day], start_year: Time.now.year - 10, end_year: Time.now.year%> <% date = Apply.selected_date(@params_date) %>
<%= f.date_select :past_time, order: [:year, :month, :day], start_year: Time.now.year - 10, end_year: Time.now.year, selected: { day: date[:selected_past_day], month: date[:selected_past_month], year: date[:selected_past_year] } %>
</div> </div>
</div> </div>
<div class="col-lg-6 col-md-6"> <div class="col-lg-6 col-md-6">
<div class="admin_box_search__filter_date__current_time"> <div class="admin_box_search__filter_date__current_time">
<%= f.date_select :current_time, order: [:year, :month, :day], start_year: Time.now.year - 10, end_year: Time.now.year%> <%= f.date_select :current_time, order: [:year, :month, :day], start_year: Time.now.year - 10, end_year: Time.now.year, selected: { day: date[:selected_current_day], month: date[:selected_current_month], year: date[:selected_current_year] } %>
</div> </div>
</div> </div>
<div class="col-lg-6 col-md-6"> <div class="col-lg-4 col-md-4">
<div class="admin_box_search__btn"> <div class="admin_box_search__btn">
<%= f.submit t('admin.search'), class: 'btn btn-success btn-admin-search'%> <%= f.submit t('admin.filter'), class: 'btn btn-success btn-admin-search'%>
</div> </div>
</div> </div>
<div class="col-lg-6 col-md-6"> <div class="col-lg-4 col-md-4">
<div class="admin_box_search__btn">
<%= link_to t('admin.show_all'), applies_index_path, class: 'btn btn-info btn-admin-search' %>
</div>
</div>
<div class="col-lg-4 col-md-4">
<div class="admin_box_search__btn"> <div class="admin_box_search__btn">
<%= link_to t('admin.dl'), '#', class: 'btn btn-primary btn-admin-search' %> <%= link_to t('admin.dl'), export_csv_path(format: :csv), class: 'btn btn-primary btn-admin-search btn-export_csv' %>
</div> </div>
</div> </div>
</div> </div>
......
<div class="container"> <div class="container">
<div class="block_result_applied"> <div class="block_result_applied"><hr>
<hr> <% if @applied.present? %>
<span class="total_count_applied_job"> <%= t('admin.has_record', result_count: @applied.total_count) %></span><hr>
<div class="row"> <div class="row">
<%= render partial: 'card_candidate', collection: @applied, as: :data %> <%= render partial: 'card_candidate', collection: @applied, as: :data %>
<% else %>
<%= t('admin.no_record') %>
<% end %>
</div> </div>
</div> </div>
</div> </div>
<%# <%= render 'shared/paginate', data: @applied, as: :data %> %> <%= render 'shared/paginate', data: @applied, as: :data %>
...@@ -2,4 +2,3 @@ ...@@ -2,4 +2,3 @@
<%= render 'box_filter' %> <%= render 'box_filter' %>
<%= render 'shared/scroll_top' %> <%= render 'shared/scroll_top' %>
<%= render 'box_result_applied' %> <%= render 'box_result_applied' %>
...@@ -18,7 +18,6 @@ module Venjob ...@@ -18,7 +18,6 @@ module Venjob
config.i18n.available_locales = [:en, :vi] config.i18n.available_locales = [:en, :vi]
config.i18n.default_locale = :vi config.i18n.default_locale = :vi
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
# Settings in config/environments/* take precedence over those specified here. # Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers # Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading # -- all .rb files in that directory are automatically loaded after loading
......
...@@ -29,9 +29,9 @@ Rails.application.configure do ...@@ -29,9 +29,9 @@ Rails.application.configure do
if Rails.root.join('tmp', 'caching-dev.txt').exist? if Rails.root.join('tmp', 'caching-dev.txt').exist?
config.action_controller.perform_caching = true config.action_controller.perform_caching = true
config.cache_store = :memory_store config.cache_store = :memory_store, { size: 64.megabytes }
config.public_file_server.headers = { config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{2.days.to_i}" 'Cache-Control' => "public, max-age=#{1.days.to_i}"
} }
else else
config.action_controller.perform_caching = false config.action_controller.perform_caching = false
......
...@@ -152,8 +152,18 @@ en: ...@@ -152,8 +152,18 @@ en:
admin: admin:
search: 'Search' search: 'Search'
dl: 'Download CSV' dl: 'Download CSV'
show_all: 'Show All'
filter: 'Filter'
no_record: 'No record'
has_record: 'Have %{result_count} result'
profile: profile:
title: 'Profile' title: 'Profile'
name: 'Name: ' name: 'Name: '
email: 'Email: ' email: 'Email: '
join_at: 'Join at: ' join_at: 'Join at: '
header_csv:
job_id: 'Job name'
name: 'Name candidate'
email: 'Email'
cv: 'CV Path'
created_at: 'Applied at'
\ No newline at end of file
...@@ -152,9 +152,19 @@ vi: ...@@ -152,9 +152,19 @@ vi:
admin: admin:
search: 'Tìm kiếm' search: 'Tìm kiếm'
dl: 'Tải về CSV' dl: 'Tải về CSV'
show_all: 'Hiển thị tất cả'
filter: 'Lọc'
no_record: 'Không kết quả phù hợp'
has_record: ' %{result_count} kết quả'
profile: profile:
title: 'Thông tin thí sinh' title: 'Thông tin thí sinh'
name: 'Họ tên: ' name: 'Họ tên: '
email: 'Email: ' email: 'Email: '
join_at: 'Ngày tham gia: ' join_at: 'Ngày tham gia: '
header_csv:
job_id: 'Tên công việc'
name: 'Tên thí sinh'
email: 'Email'
cv: 'Đường dẫn tệp cv'
created_at: 'Ứng tuyển vào'
\ No newline at end of file
...@@ -7,9 +7,11 @@ Rails.application.routes.draw do ...@@ -7,9 +7,11 @@ Rails.application.routes.draw do
devise_for :admins, path: 'admin', path_names: { sign_in: 'login', sign_out: 'logout'} devise_for :admins, path: 'admin', path_names: { sign_in: 'login', sign_out: 'logout'}
root 'home#index' root 'home#index'
get 'admin/export_csv', to: 'admin#export_csv', as: :export_csv
get 'admin/applies', to: 'admin#index', as: :applies get 'admin/search', to: 'admin#search', as: :applies
get 'admin/applies', to: 'admin#index', as: :applies_index
get 'admin', to: 'admin#routing', as: :admin get 'admin', to: 'admin#routing', as: :admin
get 'info/user/:user_id', to: 'users#personal_page', as: :personal_page get 'info/user/:user_id', to: 'users#personal_page', as: :personal_page
get 'register/:code', to: 'users#confirm_sign_up', as: :confirm_sign_up get 'register/:code', to: 'users#confirm_sign_up', as: :confirm_sign_up
......
# frozen_string_literal: true
require 'csv'
# Export file csv
class ExportCsv
def initialize(objects, attributes)
@attributes = attributes
@objects = objects
@header = attributes.map { |attr| I18n.t("header_csv.#{attr}") }
end
def perform
CSV.generate do |csv|
csv << header if csv.tell.zero?
objects.includes(:job).each do |obj|
csv << convert_data(obj)
end
end
end
private
def convert_data(obj)
data = []
data << obj.job.name
data << obj.name
data << obj.email
data << obj.cv
data << obj.created_at.localtime
data
end
attr_reader :attributes, :objects, :header
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