to_xls-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,58 @@
1
+ == to_xls plugin
2
+
3
+ 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.
4
+
5
+
6
+ == Usage
7
+
8
+ add below to RAILS_ROOT/config/initializers/mime_types.rb
9
+ Mime::Type.register_alias "text/excel", :xls
10
+
11
+ @posts = Post.all
12
+
13
+ #
14
+ # default are export header and all fileds
15
+ #
16
+
17
+ @posts.to_xls
18
+ @posts.to_xls(:only => [:title, :body])
19
+ @posts.to_xls(:except => [:id])
20
+ @posts.to_xls(:header => false)
21
+
22
+
23
+ == Example
24
+
25
+ class PostsController < ApplicationController
26
+ def index
27
+ @posts = Post.all
28
+
29
+ respond_to do |format|
30
+ format.xls { send_data(@posts.to_xls) }
31
+ #format.xls {
32
+ # filename = "posts-#{Time.now.strftime("%Y%m%d%H%M%S")}.xls"
33
+ # send_data(@posts.to_xls, :type => "application/excel; charset=utf-8; header=present", :filename => filename)
34
+ #}
35
+ end
36
+ end
37
+ end
38
+
39
+
40
+ == Dependencies
41
+
42
+ sudo gem install spreadsheet
43
+
44
+
45
+ == Install
46
+
47
+ git clone http://github.com/wenke/to_xls.git OR
48
+
49
+ rails 2
50
+ ./script/plugin install git://github.com/wenke/to_xls.git
51
+
52
+ rails 3
53
+ rails plugin install git://github.com/wenke/to_xls.git
54
+
55
+
56
+ == Note
57
+
58
+ Copyright (c) 2010 liangwenke8@gmail.com, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the to_xls-rails plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'to_xls-rails'
@@ -0,0 +1,31 @@
1
+ require 'spreadsheet'
2
+
3
+ class Array
4
+
5
+ def to_xls( options = {} )
6
+ return '' if self.empty?
7
+
8
+ xls_report = StringIO.new
9
+ book = Spreadsheet::Workbook.new
10
+ sheet = book.create_worksheet
11
+
12
+ if options[:only]
13
+ columns = Array(options[:only]).map(&:to_sym)
14
+ else
15
+ columns = self.first.class.column_names.map(&:to_sym) - Array(options[:except]).map(&:to_sym)
16
+ end
17
+
18
+ return '' if columns.empty?
19
+
20
+ sheet.row(0).concat(columns.map(&:to_s).map(&:humanize))
21
+
22
+ self.each_with_index do |obj, index|
23
+ sheet.row(index + 1).replace( columns.map{ |column| obj.send(column) } )
24
+ end
25
+
26
+ book.write(xls_report)
27
+
28
+ xls_report.string
29
+ end
30
+
31
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ version = File.read(File.expand_path("../VERSION",__FILE__)).strip
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'to_xls-rails'
7
+ s.version = version
8
+ s.author = "LiangWenKe"
9
+ s.email = "liangwenke8@gmail.com"
10
+ s.homepage = "http://github.com/wenke/to_xls-rails"
11
+ s.summary = "Export data to xls on Rails"
12
+ s.description = "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."
13
+
14
+ s.files = Dir["{lib,test}/**/*", "[a-zA-Z]*", "init.rb"] - ["Gemfile.lock"]
15
+ s.require_path = "lib"
16
+
17
+ s.rubyforge_project = s.name
18
+ s.required_rubygems_version = ">= 1.3.4"
19
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_xls-rails
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - LiangWenKe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-20 00:00:00 +08:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: 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.
18
+ email: liangwenke8@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/to_xls-rails.rb
27
+ - init.rb
28
+ - MIT-LICENSE
29
+ - Rakefile
30
+ - README.rdoc
31
+ - to_xls-rails.gemspec
32
+ - VERSION
33
+ has_rdoc: true
34
+ homepage: http://github.com/wenke/to_xls-rails
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 1.3.4
54
+ requirements: []
55
+
56
+ rubyforge_project: to_xls-rails
57
+ rubygems_version: 1.6.2
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Export data to xls on Rails
61
+ test_files: []
62
+