Commit c94c7c13 by Thanh Hung Pham

Fix column csv

parent 03d01c20
...@@ -22,9 +22,12 @@ class AdminsController < ApplicationController ...@@ -22,9 +22,12 @@ class AdminsController < ApplicationController
@to_date_default = params[:to_date] if params[:to_date].present? @to_date_default = params[:to_date] if params[:to_date].present?
@data = Apply.search_email(@email).search_city(@city_id).search_city(@category_id).applied_at_between(@from_date_default, @to_date_default) @data = Apply.search_email(@email).search_city(@city_id).search_city(@category_id).applied_at_between(@from_date_default, @to_date_default)
attributes_to_apply = %w[id applied_at]
attributes_to_job = %w[id name]
attributes_to_user = %w[id fullname]
respond_to do |format| respond_to do |format|
format.html { redirect_to root_url } format.html { redirect_to root_url }
format.csv { send_data @data.to_csv } format.csv { send_data @data.to_csv(attributes_to_apply, attributes_to_job, attributes_to_user) }
end end
end end
end end
...@@ -18,4 +18,22 @@ class Apply < ApplicationRecord ...@@ -18,4 +18,22 @@ class Apply < ApplicationRecord
end end
end end
end end
def self.to_csv(apply_attributes = column_names, job_attributes = job.column_names, user_attributes = user.column_names, options = {})
CSV.generate(options) do |csv|
csv.add_row apply_attributes + job_attributes + user_attributes
all.each do |apply|
values = apply.attributes.slice(*apply_attributes).values
if apply.job
values += apply.job.attributes.slice(*job_attributes).values
end
if apply.user
values += apply.user.attributes.slice(*user_attributes).values
end
csv.add_row values
end
end
end
end 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