Skip to content

Instantly share code, notes, and snippets.

@developer88
Created March 5, 2013 13:49
Show Gist options
  • Save developer88/5090430 to your computer and use it in GitHub Desktop.
Save developer88/5090430 to your computer and use it in GitHub Desktop.
Add custom method to export table as CSV file for ActiveAdmin Resource + scope items from filter + save csv file compatible with Russian Microsoft Office Excel
filter :reception_reception_date, as: :date_range, label: Reception.human_attribute_name(:reception_date_full)
collection_action :download_report, :method => :get do
services = Service.includes(:order, reception: [{medic: :clinic_medics}]).where{ orders.orderable_id != nil } # necessary model
if params[:q] && params[:q][:reception_reception_date_gte].length > 1
services = services.where("receptions.reception_date >= ?", params[:q][:reception_reception_date_gte])
end
if params[:q] && params[:q][:reception_reception_date_lte].length > 1
services = services.where("receptions.reception_date < ?", params[:q][:reception_reception_date_lte])
end
csv = CSV.generate( encoding: 'Windows-1251' ) do |csv|
# add headers
csv << [
I18n.t('services.finance_report.order_date'),
I18n.t('services.finance_report.order_number')
]
# add data
services.each do |s|
medic = s.reception.medic
performer_id = medic.performer_id
agreeement_number = medic.proper_agreeement_number
csv << [ s.created_at, s.order.order_number ]
end
end
# send file to user
send_data csv.encode('Windows-1251'), type: 'text/csv; charset=windows-1251; header=present', disposition: "attachment; filename=accounting_report.csv"
end
action_item only: :index do
link_to(I18n.t('active_admin.save_csv'), params.merge(:action => :download_report))
end
index :download_links => false do
# necessary fields
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment