Skip to content
This repository has been archived by the owner on Nov 17, 2017. It is now read-only.

Commit

Permalink
Exportation as CSV.
Browse files Browse the repository at this point in the history
It's based on the hard-coded use of faster_csv, so it
should certainly have to be changed a bit in order to
prepare for Ruby 1.9.
  • Loading branch information
baldowl committed Jul 14, 2008
1 parent e26084f commit 0330e20
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README
Expand Up @@ -124,6 +124,8 @@ because the pagination mechanism has been removed from the Rails core base.

git://github.com/mislav/will_paginate.git

You also need the faster_csv gem.


How do I use it?

Expand Down
1 change: 1 addition & 0 deletions init.rb
Expand Up @@ -15,6 +15,7 @@ def draw_with_admin

map.connect "#{prefix}/:model/:action", :controller => 'auto_admin', :action => 'list',
:requirements => { :action => /[^0-9].*/, :id => nil }
map.connect "#{prefix}/:model.:format", :controller => 'auto_admin', :action => 'list'
map.connect "#{prefix}/:model/:id/:action", :controller => 'auto_admin', :action => 'edit',
:requirements => { :id => /\d+/ }
yield map
Expand Down
17 changes: 12 additions & 5 deletions lib/auto_admin_controller.rb
@@ -1,4 +1,3 @@

class AutoAdminController < AutoAdmin::AutoAdminConfiguration.controller_super_class
include AutoAdminHelper
self.view_paths = AutoAdmin::AutoAdminConfiguration.view_directory
Expand Down Expand Up @@ -113,10 +112,18 @@ def list
if params[:search] && model.searchable?
model.append_search_condition! params[:search], options
end
options.update(:page => params[:page], :per_page => (params[:per_page] || model.paginate_every).to_i)
@objects = model.paginate(options)
session[:admin_list_params] ||= {}
session[:admin_list_params][params[:model]] = params
respond_to do |format|
format.html do
options.update(:page => params[:page], :per_page => (params[:per_page] || model.paginate_every).to_i)
@objects = model.paginate(options)
session[:admin_list_params] ||= {}
session[:admin_list_params][params[:model]] = params
end
format.csv do
@objects = model.find(:all, options)
export_into_csv_excel(model, @objects)
end
end
end

def save
Expand Down
23 changes: 22 additions & 1 deletion lib/auto_admin_helper.rb
@@ -1,3 +1,5 @@
require 'faster_csv'

module AutoAdminHelper
def model name=nil
AutoAdmin::AutoAdminConfiguration.model( name || params[:model] )
Expand Down Expand Up @@ -115,5 +117,24 @@ def admin_table(options={}, &proc)
}.update(options)
fields_for(nil, nil, opts, &proc)
end
end

def tweak_csv_excel_content_type
if request.respond_to?(:user_agent)
request.user_agent =~ /windows/i ? 'application/vnd.ms-excel' : 'text/csv'
else
'text/csv'
end
end

def export_into_csv_excel(model, collection)
content_type = tweak_csv_excel_content_type
csv_content = FasterCSV.generate do |csv|
csv << model.columns.map {|col| col.human_name}
collection.each do |o|
csv << model.columns.map {|col| o.send(col.name.to_sym)}
end
end
send_data csv_content, :type => content_type,
:filename => "#{params[:model]}.csv"
end
end
3 changes: 2 additions & 1 deletion themes/django/views/list.html.erb
Expand Up @@ -2,7 +2,8 @@
@body_class = 'change-list'
@content_class = 'flex'
@title = "Select #{human_model.downcase} to change"
@tools = [link_to( "Add #{human_model.downcase}", { :model => params[:model], :action => 'edit' }, { :class => 'addlink' } )]
@tools = [link_to( "Add #{human_model.downcase}", { :model => params[:model], :action => 'edit' }, { :class => 'addlink' } ),
link_to( "Save as Excel/CSV", { :model => params[:model], :format => 'csv', :filter => params[:filter], :sort => params[:sort], :sort_reverse => params[:sort_reverse], :search => params[:search] } )]
%>
<div class="module filtered" id="changelist">

Expand Down

0 comments on commit 0330e20

Please sign in to comment.