Commit 0f3a0def by Thanh Hung Pham

Export csv

parent eedcd09e
...@@ -12,4 +12,12 @@ class AdminsController < ApplicationController ...@@ -12,4 +12,12 @@ class AdminsController < ApplicationController
@applied_jobs = Apply.search_email(@email).search_city(@city_id).search_city(@category_id).applied_at_between(@from_date_default, @to_date_default).page params[:page] @applied_jobs = Apply.search_email(@email).search_city(@city_id).search_city(@category_id).applied_at_between(@from_date_default, @to_date_default).page params[:page]
end end
def export
@data = Apply.order(:created_at)
respond_to do |format|
format.html { redirect_to root_url }
format.csv { send_data @data.to_csv }
end
end
end end
...@@ -8,4 +8,14 @@ class Apply < ApplicationRecord ...@@ -8,4 +8,14 @@ class Apply < ApplicationRecord
scope :search_email, ->(email) { joins(:user).where('? IS NULL OR users.email = ?', email, email) } scope :search_email, ->(email) { joins(:user).where('? IS NULL OR users.email = ?', email, email) }
scope :search_city, ->(city_id) { joins(:job).where('? IS NULL OR jobs.city_id = ?', city_id, city_id) } scope :search_city, ->(city_id) { joins(:job).where('? IS NULL OR jobs.city_id = ?', city_id, city_id) }
scope :search_category, ->(category_id) { joins(job: [:job_category]).where('? IS NULL OR job_categories.category_id = ?', category_id, category_id) } scope :search_category, ->(category_id) { joins(job: [:job_category]).where('? IS NULL OR job_categories.category_id = ?', category_id, category_id) }
def self.to_csv(options = {})
CSV.generate(options) do |csv|
csv.add_row column_names
all.each do |apply|
values = apply.attributes.values
csv.add_row values
end
end
end
end end
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<%= submit_tag 'Search', class: 'btn btn-primary' %> <%= submit_tag 'Search', class: 'btn btn-primary' %>
<%= link_to 'CSV Download', root_path, class: 'btn btn-primary' %> <%= link_to 'CSV Download', admins_export_path(format: "csv"), class: 'btn btn-primary' %>
</div> </div>
<%- end -%> <%- end -%>
</div> </div>
......
require_relative 'boot' require_relative 'boot'
require File.expand_path('../boot', __FILE__)
require 'csv'
require 'rails/all' require 'rails/all'
# Require the gems listed in Gemfile, including any gems # Require the gems listed in Gemfile, including any gems
......
...@@ -22,6 +22,7 @@ Rails.application.routes.draw do ...@@ -22,6 +22,7 @@ Rails.application.routes.draw do
get 'admins/search', to: 'admins#search' get 'admins/search', to: 'admins#search'
post 'admins/search', to: 'admins#search' post 'admins/search', to: 'admins#search'
get 'admins/export', to: 'admins#export'
resource :cities resource :cities
devise_scope :user do devise_scope :user do
......
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