Our admins are always data-greedy, so i provided a method that lets them get detailed data on any search result they like. Be careful to not let normal users access this (hide email addresses etc).
Installation
- Provide a current_model method that returns which model the controller is working on.
- Store the objects you want to render in @current_objects.
- add this block to every index action response (in a DRY way…)
Usage
- Visit /xxx/anything.csv to get all data in csv
format.csv do
#collect data
keys = current_model.new.attributes.keys.sort
csv_string = FasterCSV.generate do |csv|
csv << keys
@current_objects.each do |record|
csv << record.attributes.sort_by{|k,v|k}.map{|arr|arr[1]}
end
end
#send data
filename = current_model.to_s.downcase.gsub(/[^0-9a-z]/, "_") + ".csv"
send_data(csv_string,
:type => 'text/csv; charset=utf-8; header=present',
:filename => filename
)
end