Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

writing a /registrations.csv method to be able to download the attend…

…ee list for various people who need it in excel
  • Loading branch information...
commit 1eb2009af34e4e3e98e2befa35c8c77f971e4c00 1 parent e9b172a
@croaky croaky authored
View
4 app/controllers/registrations_controller.rb
@@ -18,4 +18,8 @@ def create
# render :partial => 'form', :locals => { :registration => @registration }
# end
end
+
+ def index
+ send_data Registration.all.to_csv(:only => [:name, :role, :twitter], :methods => [:school_name])
+ end
end
View
2  config/routes.rb
@@ -1,7 +1,7 @@
Dddd::Application.routes.draw do
root :to => 'registrations#new'
- resources :registrations, :only => [:new]
+ resources :registrations, :only => [:index, :new]
resources :talks, :only => [:index, :show]
resources :designers, :only => [:index]
resources :developers, :only => [:index]
View
20 vendor/plugins/to_csv/MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008-2010 Ary Djmal
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
67 vendor/plugins/to_csv/README.rdoc
@@ -0,0 +1,67 @@
+= to_csv plugin
+
+This simple plugin gives you the ability to call to_csv to a collection of activerecords. The builder options are the same as to_json / to_xml, except for the :include.
+
+
+== Usage
+
+ @users = User.all
+
+ #
+ # defaults are export headers and all fields
+ #
+
+ @users.to_csv
+ @users.to_csv(:only => [:last_name, :role])
+ @users.to_csv(:headers => false)
+ @users.to_csv(:except => [:last_name, :role])
+ @users.to_csv(:methods => :admin?)
+
+
+== Real life example
+
+In the controller where you want to export to csv, add the format.csv line (as of rails 2.1 it is not necessary to register the csv myme_type)
+
+ class UserController < ApplicationController
+
+ def index
+ @users = User.all
+
+ respond_to do |format|
+ format.html
+ format.xml { render :xml => @users }
+ format.csv { send_data @users.to_csv }
+ end
+ end
+
+ def show...
+ def new...
+ def edit...
+ def create...
+ def update...
+ def destroy...
+
+ end
+
+
+== Dependencies
+
+ sudo gem install fastercsv
+
+
+== Install
+
+ ./script/plugin install git://github.com/arydjmal/to_csv.git
+
+
+== Ideas
+
+I got ideas and influence from Mike Clarks recipe #35 in Rails Recipes book, some anonymous pastie, and whoever wrote to_xml/to_json builders.
+
+
+== Note
+
+Does not work on a single activerecord, ie, User.first.to_csv.
+
+
+Copyright (c) 2008-2010 Ary Djmal, released under the MIT license
View
7 vendor/plugins/to_csv/init.rb
@@ -0,0 +1,7 @@
+if RUBY_VERSION >= "1.9.0"
+ require 'csv'
+else
+ require 'fastercsv'
+end
+
+require 'to_csv'
View
27 vendor/plugins/to_csv/lib/to_csv.rb
@@ -0,0 +1,27 @@
+class Array
+ def to_csv(options = {})
+ return '' if self.empty?
+
+ klass = self.first.class
+ attributes = self.first.attributes.keys.sort.map(&:to_sym)
+
+ if options[:only]
+ columns = Array(options[:only]) & attributes
+ else
+ columns = attributes - Array(options[:except])
+ end
+
+ columns += Array(options[:methods])
+
+ return '' if columns.empty?
+
+ output = CSV.generate do |csv|
+ csv << columns.map { |column| klass.human_attribute_name(column) } unless options[:headers] == false
+ self.each do |item|
+ csv << columns.collect { |column| item.send(column) }
+ end
+ end
+
+ output
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.