creating export csv

parent 4e5ed11c
Pipeline #1081 failed with stages
in 0 seconds
......@@ -24,4 +24,10 @@
font-style: italic;
font-size: 16px;
}
.tilde {
color: black;
}
.datetime {
margin: 20px;
padding: 10px;
}
class AdminsController < ApplicationController
before_action :sign_out_current_user
before_action :sign_in_validation_admin, only: [:index, :destroy]
def index
@cities = City.all
@industries = Industry.all
@count_apply_job = JobApplied.count
@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]}
binding.pry
@days = ['None']
(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
def new
......@@ -30,9 +37,23 @@ class AdminsController < ApplicationController
redirect_to root_path
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
def sign_out_current_user
sign_out if signed_in? && current_user.admin.nil?
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
class UsersController < ApplicationController
before_action :sign_out_admin
before_action :sign_in_validation, only: [:update, :my_page, :my_info]
def my_page
......@@ -38,6 +39,10 @@ class UsersController < ApplicationController
private
def sign_out_admin
sign_out if signed_in? && current_user.admin?
end
def sign_in_validation
return if signed_in?
store_location
......
class Admin < ApplicationRecord
before_create :create_remember_token
FIRST_DAY = 1
LAST_DAY = 31
def self.new_remember_token
SecureRandom.urlsafe_base64
end
FIRST_MONTH = 1
LAST_MONTH = 12
def self.digest(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def create_remember_token
self.remember_token = User.digest(User.new_remember_token)
end
FIRST_YEAR = 2015
LAST_YEAR = 2020
end
require 'csv'
class JobApplied < ApplicationRecord
before_save { self.email = email.downcase }
mount_uploader :cv_user, UserCvUploader
......@@ -12,4 +13,14 @@ class JobApplied < ApplicationRecord
validates :email, presence: true, length: { maximum: 200 }, format: { with: VALID_EMAIL_REGEX }
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
<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 @@
<% end %>
</select>
</div>
<div class="row">
<div class="text col-2">Date</div>
<select class="form-control" id="exampleFormControlSelect1">
<%= @months.each do |month| %>
<option><%= month %></option>
<% end %>
</select>
<div class="datetime">
<%= render "datetime" %>
</div>
<div class="search-btn">
<button class="btn btn-primary" type="submit">Search</button>
<div class="row">
<div class="col-3"></div>
<div class="search-btn col-4">
<button class="btn btn-primary" type="submit">Search</button>
</div>
<div class="download-csv-btn col-4">
<%= link_to('Download CSV', download_csv_path(format: :csv), class: 'btn btn-info')%>
</div>
</div>
</div>
......@@ -37,6 +37,7 @@ Rails.application.routes.draw do
get 'admin/login', to: 'admins#new', as: :admin_login
delete 'admin/logout', to: 'admins#destroy', as: :admin_logout
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 :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