Permalink
Browse files

first commit

  • Loading branch information...
arydjmal committed Jan 11, 2009
0 parents commit a9dc61af97b2ee6fc4f22d02b82cb9db8b959ef0
Showing with 199 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +63 −0 README.rdoc
  3. +1 −0 init.rb
  4. +40 −0 lib/to_xls.rb
  5. +50 −0 test/to_xls_test.rb
  6. +25 −0 test/user_model.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2009 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.
@@ -0,0 +1,63 @@
+= to_xls plugin
+
+This simple plugin gives you the ability to call to_xls 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_xls
+ @users.to_xls(:only => [:last_name, :role])
+ @users.to_xls(:headers => false)
+ @users.to_xls(:except => [:last_name, :role])
+ @users.to_xls(:except => :role, :methods => :admin?)
+
+
+== Real life example
+
+In the controller where you want to export to excel, add the format.xls line.
+
+ class UserController < ApplicationController
+
+ def index
+ @users = User.all
+
+ respond_to do |format|
+ format.html
+ format.xml { render :xml => @users }
+ format.xls { send_data @users.to_xls }
+ end
+ end
+
+ def show...
+ def new...
+ def edit...
+ def create...
+ def update...
+ def destroy...
+
+ end
+
+
+== Dependencies
+
+ None.
+
+
+== Install
+
+ ./script/plugin install git://github.com/arydjmal/to_xls.git
+
+
+== Note
+
+Does not work on a single activerecord, ie, User.first.to_xls.
+
+
+
+Copyright (c) 2009 Ary Djmal, released under the MIT license
@@ -0,0 +1 @@
+require 'to_xls'
@@ -0,0 +1,40 @@
+class Array
+
+ def to_xls(options = {})
+ output = '<?xml version="1.0" encoding="UTF-8"?><Workbook xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"><Worksheet><Table>'
+
+ if self.any?
+
+ all_columns = self.first.class.columns.collect { |c| c.name.to_sym }
+
+ if options[:only]
+ columns = options[:only].to_a
+ else
+ columns = all_columns - options[:except].to_a
+ end
+
+ # get rid of invalid columns
+ columns = columns & all_columns
+
+ columns += options[:methods].to_a
+
+ if columns.any?
+ unless options[:headers] == false
+ output << "<Row>"
+ columns.each { |column| output << "<Cell><Data ss:Type=\"String\">#{column}</Data></Cell>" }
+ output << "</Row>"
+ end
+
+ self.each do |item|
+ output << "<Row>"
+ columns.each { |column| output << "<Cell><Data ss:Type=\"#{item.send(column).kind_of?(Integer) ? 'Number' : 'String'}\">#{item.send(column)}</Data></Cell>" }
+ output << "</Row>"
+ end
+ end
+
+ end
+
+ output << '</Table></Worksheet></Workbook>'
+ end
+
+end
@@ -0,0 +1,50 @@
+require 'test/unit'
+require 'rubygems'
+require File.dirname(__FILE__) + '/../lib/to_xls'
+require File.dirname(__FILE__) + '/user_model'
+
+class ToXlsTest < Test::Unit::TestCase
+
+ def setup
+ @users = []
+ @users << User.new(:id => 1, :name => 'Ary', :age => 24)
+ @users << User.new(:id => 2, :name => 'Nati', :age => 21)
+ end
+
+ def test_with_empty_array
+ assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet><Table></Table></Worksheet></Workbook>", [].to_xls )
+ end
+
+ def test_with_no_options
+ assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet><Table><Row><Cell><Data ss:Type=\"String\">id</Data></Cell><Cell><Data ss:Type=\"String\">name</Data></Cell><Cell><Data ss:Type=\"String\">age</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">1</Data></Cell><Cell><Data ss:Type=\"String\">Ary</Data></Cell><Cell><Data ss:Type=\"Number\">24</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">2</Data></Cell><Cell><Data ss:Type=\"String\">Nati</Data></Cell><Cell><Data ss:Type=\"Number\">21</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls )
+ end
+
+ def test_with_no_headers
+ assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet><Table><Row><Cell><Data ss:Type=\"Number\">1</Data></Cell><Cell><Data ss:Type=\"String\">Ary</Data></Cell><Cell><Data ss:Type=\"Number\">24</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">2</Data></Cell><Cell><Data ss:Type=\"String\">Nati</Data></Cell><Cell><Data ss:Type=\"Number\">21</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:headers => false) )
+ end
+
+ def test_with_only
+ assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet><Table><Row><Cell><Data ss:Type=\"String\">name</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:only => :name) )
+ end
+
+ def test_with_empty_only
+ assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet><Table></Table></Worksheet></Workbook>", @users.to_xls(:only => "") )
+ end
+
+ def test_with_only_and_wrong_column_names
+ assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet><Table><Row><Cell><Data ss:Type=\"String\">name</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:only => [:name, :yoyo]) )
+ end
+
+ def test_with_except
+ assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet><Table><Row><Cell><Data ss:Type=\"String\">age</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">24</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">21</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:except => [:id, :name]) )
+ end
+
+ def test_with_except_and_only_should_listen_to_only
+ assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet><Table><Row><Cell><Data ss:Type=\"String\">name</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:except => [:id, :name], :only => :name) )
+ end
+
+ def test_with_except
+ assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet><Table><Row><Cell><Data ss:Type=\"String\">id</Data></Cell><Cell><Data ss:Type=\"String\">name</Data></Cell><Cell><Data ss:Type=\"String\">age</Data></Cell><Cell><Data ss:Type=\"String\">is_old?</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">1</Data></Cell><Cell><Data ss:Type=\"String\">Ary</Data></Cell><Cell><Data ss:Type=\"Number\">24</Data></Cell><Cell><Data ss:Type=\"String\">false</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">2</Data></Cell><Cell><Data ss:Type=\"String\">Nati</Data></Cell><Cell><Data ss:Type=\"Number\">21</Data></Cell><Cell><Data ss:Type=\"String\">false</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:methods => [:is_old?]) )
+ end
+
+end
@@ -0,0 +1,25 @@
+class User
+ COLUMNS = %w(id name age)
+
+ COLUMNS.each {|column| attr_reader column }
+
+ def initialize(params={})
+ COLUMNS.each {|column| eval("@#{column} = params[:#{column}]")}
+ end
+
+ def self.columns
+ COLUMNS.collect { |column| Column.new(column) }
+ end
+
+ def is_old?
+ age > 40
+ end
+end
+
+class Column
+ attr_reader :name
+
+ def initialize(name)
+ @name = name
+ end
+end

0 comments on commit a9dc61a

Please sign in to comment.