export organisateurs

This commit is contained in:
Nicolas Bally 2020-12-21 16:24:49 +01:00
parent b77c13cf36
commit 4ffcbc1159
4 changed files with 92 additions and 23 deletions

View File

@ -23,6 +23,71 @@ class Admin::OrganisateursController < ApplicationController
@organisateurs = @organisateurs.page(page).per(per_page)
}
format.csv {
@headers = []
@columns = []
Organisateur.qi_table_order.each do |key, value|
if value.instance_of? Hash
name = value[:name]
else
name = value
end
if name != "Actions"
@headers << name.to_s
@columns << key
end
end
xlsx_package = Axlsx::Package.new
wb = xlsx_package.workbook
wb.add_worksheet(name: "import") do |sheet|
sheet.add_row @headers
@organisateurs.each do |p_customer|
line = []
@columns.each do |column|
if (p_customer.respond_to?("csv_"+column.to_s))
line << p_customer.send("csv_"+column.to_s)
elsif (p_customer.respond_to?(column))
if p_customer.send(column.to_s).class.to_s == "BigDecimal"
line << p_customer.send(column.to_s).to_s.gsub('.', ',')
else
line << p_customer.send(column.to_s)
end
else
line << column.to_s
end
end
sheet.add_row line, types: line.map{|t| self.cell_type_from_value(t)}
end
end
@final_file = "#{Rails.root}/private_medias/export-organisateurs-#{Time.now.to_s.to_slug}.xlsx"
xlsx_package.serialize(@final_file)
send_file @final_file
}
end
end

View File

@ -454,28 +454,7 @@ class Admin::PCustomersController < ApplicationController
end
def cell_type_from_value(v)
if v.is_a?(Date)
:date
elsif v.is_a?(String)
:string
elsif v.is_a?(Time)
:time
elsif v.is_a?(TrueClass) || v.is_a?(FalseClass)
:boolean
elsif v.to_s =~ /\A[+-]?\d+?\Z/ #numeric
:integer
elsif v.to_s =~ /\A[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\Z/ #float
:float
# \A(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])
# T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?
# (Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?\Z
elsif v.to_s =~/\A(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?\Z/
:iso_8601
else
:string
end
end
def show
@p_customer = PCustomer.find(params[:id])

View File

@ -162,6 +162,29 @@ class ApplicationController < ActionController::Base
end
def cell_type_from_value(v)
if v.is_a?(Date)
:date
elsif v.is_a?(String)
:string
elsif v.is_a?(Time)
:time
elsif v.is_a?(TrueClass) || v.is_a?(FalseClass)
:boolean
elsif v.to_s =~ /\A[+-]?\d+?\Z/ #numeric
:integer
elsif v.to_s =~ /\A[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\Z/ #float
:float
# \A(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])
# T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?
# (Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?\Z
elsif v.to_s =~/\A(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?\Z/
:iso_8601
else
:string
end
end
@ -240,5 +263,7 @@ class ApplicationController < ActionController::Base
helper_method :current_admin, :get_specific_pref, :sort_by_sorting, :current_p_customer
end

View File

@ -7,7 +7,7 @@
.qi_search_row
=form_tag "", :method => "get", :onsubmit => "" do
=render :partial => "qi/qi_ordered_table_search_footer", :locals => {:collection_object => @organisateurs}
=render :partial => "qi/qi_ordered_table_search_footer", :locals => {:collection_object => @organisateurs, :csv => true}
=render :partial => "qi/qi_ordered_table", :locals => {:qi_ordered_table_collection => @organisateurs}