creating export csv

parent 4e5ed11c
Pipeline #1081 failed with stages
in 0 seconds
...@@ -24,4 +24,10 @@ ...@@ -24,4 +24,10 @@
font-style: italic; font-style: italic;
font-size: 16px; font-size: 16px;
} }
.tilde {
color: black;
}
.datetime {
margin: 20px;
padding: 10px;
}
class AdminsController < ApplicationController class AdminsController < ApplicationController
before_action :sign_out_current_user
before_action :sign_in_validation_admin, only: [:index, :destroy]
def index def index
@cities = City.all @cities = City.all
@industries = Industry.all @industries = Industry.all
@count_apply_job = JobApplied.count @count_apply_job = JobApplied.count
@user_apply_job = JobApplied.all.order(updated_at: :desc).page(params[:page]).per(Job::LIMIT_PAGE) @user_apply_job = JobApplied.all.order(updated_at: :desc).page(params[:page]).per(Job::LIMIT_PAGE)
@months = [['None', '']]
(1..12).each {|m| @months << [Date::MONTHNAMES[m], m]} @days = ['None']
binding.pry (Admin::FIRST_DAY..Admin::LAST_DAY).each {|d| @days << d}
@months = ['None']
(Admin::FIRST_MONTH..Admin::LAST_MONTH).each {|m| @months << m}
@years = ['None']
(Admin::FIRST_YEAR..Admin::LAST_YEAR).each {|y| @years << y}
end end
def new def new
...@@ -30,9 +37,23 @@ class AdminsController < ApplicationController ...@@ -30,9 +37,23 @@ class AdminsController < ApplicationController
redirect_to root_path redirect_to root_path
end end
def download_csv
@user_apply_job = JobApplied.all.order(updated_at: :desc)
respond_to do |format|
format.csv { send_data @user_apply_job.to_csv, filename: "test-#{Date.today}.csv"}
end
end
private private
def sign_out_current_user def sign_out_current_user
sign_out if signed_in? && current_user.admin.nil? sign_out if signed_in? && current_user.admin.nil?
end end
def sign_in_validation_admin
return if signed_in? && current_user.admin?
store_location
flash[:warning] = Settings.user.warning_signin
redirect_to admin_login_path
end
end end
class UsersController < ApplicationController class UsersController < ApplicationController
before_action :sign_out_admin
before_action :sign_in_validation, only: [:update, :my_page, :my_info] before_action :sign_in_validation, only: [:update, :my_page, :my_info]
def my_page def my_page
...@@ -38,6 +39,10 @@ class UsersController < ApplicationController ...@@ -38,6 +39,10 @@ class UsersController < ApplicationController
private private
def sign_out_admin
sign_out if signed_in? && current_user.admin?
end
def sign_in_validation def sign_in_validation
return if signed_in? return if signed_in?
store_location store_location
......
class Admin < ApplicationRecord class Admin < ApplicationRecord
before_create :create_remember_token FIRST_DAY = 1
LAST_DAY = 31
def self.new_remember_token FIRST_MONTH = 1
SecureRandom.urlsafe_base64 LAST_MONTH = 12
end
def self.digest(token) FIRST_YEAR = 2015
Digest::SHA1.hexdigest(token.to_s) LAST_YEAR = 2020
end
private
def create_remember_token
self.remember_token = User.digest(User.new_remember_token)
end
end end
require 'csv'
class JobApplied < ApplicationRecord class JobApplied < ApplicationRecord
before_save { self.email = email.downcase } before_save { self.email = email.downcase }
mount_uploader :cv_user, UserCvUploader mount_uploader :cv_user, UserCvUploader
...@@ -12,4 +13,14 @@ class JobApplied < ApplicationRecord ...@@ -12,4 +13,14 @@ class JobApplied < ApplicationRecord
validates :email, presence: true, length: { maximum: 200 }, format: { with: VALID_EMAIL_REGEX } validates :email, presence: true, length: { maximum: 200 }, format: { with: VALID_EMAIL_REGEX }
validates :cv_user, presence: true validates :cv_user, presence: true
def self.to_csv
attributes = %w{title name cv_user email updated_at}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |info_application|
csv << info_application.job.attributes.values_at(attributes[0])
csv << attributes[(1..4)].map{ |attr| info_application.send(attr) }
end
end
end
end end
<div class="row justify-content-lg-center">
<div class="text col-1">Date:</div>
<select class="form-control col-1">
<%= @years.each do |year| %>
<option><%= year %></option>
<% end %>
</select>
<select class="form-control col-1">
<%= @months.each do |month| %>
<option><%= month %></option>
<% end %>
</select>
<select class="form-control col-1">
<%= @days.each do |day| %>
<option><%= day %></option>
<% end %>
</select>
<h2 class="col-1 tilde">~</h2>
<select class="form-control col-1">
<%= @years.each do |year| %>
<option><%= year %></option>
<% end %>
</select>
<select class="form-control col-1">
<%= @months.each do |month| %>
<option><%= month %></option>
<% end %>
</select>
<select class="form-control col-1">
<%= @days.each do |day| %>
<option><%= day %></option>
<% end %>
</select>
</div>
...@@ -20,15 +20,16 @@ ...@@ -20,15 +20,16 @@
<% end %> <% end %>
</select> </select>
</div> </div>
<div class="row"> <div class="datetime">
<div class="text col-2">Date</div> <%= render "datetime" %>
<select class="form-control" id="exampleFormControlSelect1">
<%= @months.each do |month| %>
<option><%= month %></option>
<% end %>
</select>
</div> </div>
<div class="search-btn"> <div class="row">
<div class="col-3"></div>
<div class="search-btn col-4">
<button class="btn btn-primary" type="submit">Search</button> <button class="btn btn-primary" type="submit">Search</button>
</div> </div>
<div class="download-csv-btn col-4">
<%= link_to('Download CSV', download_csv_path(format: :csv), class: 'btn btn-info')%>
</div>
</div>
</div> </div>
...@@ -37,6 +37,7 @@ Rails.application.routes.draw do ...@@ -37,6 +37,7 @@ Rails.application.routes.draw do
get 'admin/login', to: 'admins#new', as: :admin_login get 'admin/login', to: 'admins#new', as: :admin_login
delete 'admin/logout', to: 'admins#destroy', as: :admin_logout delete 'admin/logout', to: 'admins#destroy', as: :admin_logout
get 'admin/applies', to: 'admins#index', as: :admin_page get 'admin/applies', to: 'admins#index', as: :admin_page
get 'download-csv', to: 'admins#download_csv', as: :download_csv
resources :applied_jobs, only: [:new, :create] resources :applied_jobs, only: [:new, :create]
resources :reset_passwords, only: [:edit, :update] resources :reset_passwords, only: [:edit, :update]
......
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