to_xls-rails 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +9 -6
- data/Gemfile +2 -0
- data/README.rdoc +40 -23
- data/VERSION +1 -1
- data/lib/to_xls-rails.rb +20 -8
- data/to_xls-rails.gemspec +4 -4
- metadata +8 -9
data/CHANGELOG
CHANGED
@@ -1,18 +1,21 @@
|
|
1
1
|
0.1.0
|
2
|
-
|
2
|
+
First commit
|
3
3
|
|
4
4
|
0.1.1
|
5
|
-
|
5
|
+
Added gem dependency on gemspec file
|
6
6
|
|
7
7
|
0.1.2
|
8
|
-
|
8
|
+
Update README.rdoc
|
9
9
|
|
10
10
|
1.0.0
|
11
|
-
|
11
|
+
Fixed typo
|
12
12
|
|
13
13
|
1.1.0
|
14
14
|
Translation of a column-value
|
15
|
-
|
15
|
+
Added options :header
|
16
16
|
|
17
17
|
1.1.1
|
18
|
-
|
18
|
+
Added options :header_columns
|
19
|
+
|
20
|
+
1.1.2
|
21
|
+
Added prepend option. Prepend some data above header and access current array element index in &block
|
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -1,26 +1,36 @@
|
|
1
|
-
== to_xls
|
1
|
+
== to_xls-rails
|
2
2
|
|
3
|
-
This simple plugin gives you the ability to call to_xls to a collection of activerecords
|
3
|
+
This simple plugin gives you the ability to call to_xls to a collection of activerecords for Rails.
|
4
|
+
|
5
|
+
|
6
|
+
== Install
|
7
|
+
gem install spreadsheet
|
8
|
+
gem install to_xls-rails
|
4
9
|
|
5
10
|
|
6
11
|
== Usage
|
7
12
|
|
8
13
|
add below to RAILS_ROOT/config/initializers/mime_types.rb
|
9
14
|
Mime::Type.register_alias "text/excel", :xls
|
10
|
-
|
15
|
+
|
11
16
|
@posts = Post.all
|
12
|
-
|
17
|
+
|
13
18
|
#
|
14
|
-
#
|
19
|
+
# default are export header and all fileds
|
15
20
|
#
|
16
|
-
|
21
|
+
|
17
22
|
@posts.to_xls
|
18
23
|
@posts.to_xls(:only => [:title, :body])
|
19
24
|
@posts.to_xls(:except => [:id])
|
20
25
|
@posts.to_xls(:header => false)
|
21
26
|
|
27
|
+
# Will prepend above header
|
28
|
+
# | Col 0, Row 0 | Col 1, Row 0 |
|
29
|
+
# | Col 0, Row 1 | |
|
30
|
+
@posts.to_xls(:prepend => [["Col 0, Row 0", "Col 1, Row 0"], ["Col 0, Row 1"]])
|
31
|
+
|
22
32
|
# Translation of a column-value
|
23
|
-
@posts.to_xls{|column, value| column==:salutation ? t(value) : value}
|
33
|
+
@posts.to_xls{|column, value, row_index| column==:salutation ? t(value) + " at #{row_index}" : value}
|
24
34
|
|
25
35
|
|
26
36
|
== Example
|
@@ -28,34 +38,41 @@ This simple plugin gives you the ability to call to_xls to a collection of activ
|
|
28
38
|
class PostsController < ApplicationController
|
29
39
|
def index
|
30
40
|
@posts = Post.all
|
31
|
-
|
41
|
+
|
32
42
|
respond_to do |format|
|
33
43
|
format.xls { send_data(@posts.to_xls) }
|
34
|
-
#format.xls {
|
35
|
-
|
36
|
-
#
|
37
|
-
#}
|
44
|
+
# format.xls {
|
45
|
+
# filename = "posts-#{Time.now.strftime("%Y%m%d%H%M%S")}.xls"
|
46
|
+
# send_data(@posts.to_xls, :type => "application/excel; charset=utf-8; header=present", :filename => filename)
|
47
|
+
# }
|
38
48
|
end
|
39
49
|
end
|
40
50
|
end
|
41
51
|
|
42
52
|
|
43
|
-
|
53
|
+
Works on Rails 3:
|
44
54
|
|
45
|
-
|
46
|
-
|
55
|
+
class PostsController < ApplicationController
|
56
|
+
def index
|
57
|
+
# add this to config/initializers/mime_types.rb
|
58
|
+
Mime::Type.register "application/vnd.ms-excel", :xls
|
47
59
|
|
48
|
-
|
60
|
+
@posts = Post.all
|
61
|
+
|
62
|
+
respond_to do |format|
|
63
|
+
format.xls {
|
64
|
+
send_data @posts.to_xls
|
65
|
+
return # prevet Rails to seek for index.xls.erb
|
66
|
+
}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
49
70
|
|
50
|
-
with gem
|
51
|
-
gem install to_xls-rails
|
52
71
|
|
53
|
-
|
54
|
-
gem 'to_xls-rails'
|
55
|
-
bundle install
|
72
|
+
== Contributors
|
56
73
|
|
57
|
-
|
58
|
-
|
74
|
+
* {Juan Pablo Marzetti}[http://github.com/yonpols] Added Support for Value-Substitution
|
75
|
+
* {Denis Yagofarov}[http://github.com/denyago] Added prepend some data above header and access current array element index in &block
|
59
76
|
|
60
77
|
|
61
78
|
== Note
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.2
|
data/lib/to_xls-rails.rb
CHANGED
@@ -3,8 +3,10 @@ require 'spreadsheet'
|
|
3
3
|
class Array
|
4
4
|
|
5
5
|
def to_xls(options = {}, &block)
|
6
|
-
|
7
|
-
|
6
|
+
|
7
|
+
return '' if self.empty? && options[:prepend].blank?
|
8
|
+
|
9
|
+
columns = []
|
8
10
|
options.reverse_merge!(:header => true)
|
9
11
|
|
10
12
|
xls_report = StringIO.new
|
@@ -13,23 +15,33 @@ class Array
|
|
13
15
|
|
14
16
|
if options[:only]
|
15
17
|
columns = Array(options[:only]).map(&:to_sym)
|
16
|
-
|
18
|
+
elsif !self.empty?
|
17
19
|
columns = self.first.class.column_names.map(&:to_sym) - Array(options[:except]).map(&:to_sym)
|
18
20
|
end
|
19
21
|
|
20
|
-
return '' if columns.empty?
|
22
|
+
return '' if columns.empty? && options[:prepend].blank?
|
23
|
+
|
24
|
+
sheet_index = 0
|
25
|
+
|
26
|
+
unless options[:prepend].blank?
|
27
|
+
options[:prepend].each do |array|
|
28
|
+
sheet.row(sheet_index).concat(array)
|
29
|
+
sheet_index += 1
|
30
|
+
end
|
31
|
+
end
|
21
32
|
|
22
33
|
if options[:header]
|
23
|
-
sheet.row(
|
34
|
+
sheet.row(sheet_index).concat(options[:header_columns].blank? ? columns.map(&:to_s).map(&:humanize) : options[:header_columns])
|
35
|
+
sheet_index += 1
|
24
36
|
end
|
25
37
|
|
26
38
|
self.each_with_index do |obj, index|
|
27
|
-
index = options[:header] ? (index + 1) : index
|
28
39
|
if block
|
29
|
-
sheet.row(
|
40
|
+
sheet.row(sheet_index).replace(columns.map { |column| block.call(column, obj.send(column), index) })
|
30
41
|
else
|
31
|
-
sheet.row(
|
42
|
+
sheet.row(sheet_index).replace(columns.map { |column| obj.send(column) })
|
32
43
|
end
|
44
|
+
sheet_index += 1
|
33
45
|
end
|
34
46
|
|
35
47
|
book.write(xls_report)
|
data/to_xls-rails.gemspec
CHANGED
@@ -5,16 +5,16 @@ version = File.read(File.expand_path("../VERSION", __FILE__)).strip
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = 'to_xls-rails'
|
7
7
|
s.version = version
|
8
|
-
s.author = "
|
8
|
+
s.author = "Mike Liang"
|
9
9
|
s.email = "liangwenke.com@gmail.com"
|
10
10
|
s.homepage = "http://github.com/liangwenke/to_xls-rails"
|
11
|
-
s.summary = "Export data to
|
12
|
-
s.description = "This simple plugin gives you the ability to call to_xls to a collection of activerecords
|
11
|
+
s.summary = "Export Rails ActiveRecord data to excel file"
|
12
|
+
s.description = "This simple plugin gives you the ability to call to_xls to a collection of activerecords for Rails."
|
13
13
|
|
14
14
|
s.files = Dir["{lib,test}/**/*", "[a-zA-Z]*", "init.rb"] - ["Gemfile.lock"]
|
15
15
|
s.require_path = "lib"
|
16
16
|
|
17
|
-
s.add_dependency('spreadsheet', '~>0.6.5')
|
17
|
+
s.add_dependency('spreadsheet', '~> 0.6.5')
|
18
18
|
|
19
19
|
s.platform = Gem::Platform::RUBY
|
20
20
|
s.required_rubygems_version = ">= 1.3.4"
|
metadata
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: to_xls-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
-
-
|
8
|
+
- Mike Liang
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-01-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: spreadsheet
|
16
|
-
requirement: &
|
16
|
+
requirement: &70151257218860 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,9 @@ dependencies:
|
|
21
21
|
version: 0.6.5
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70151257218860
|
25
25
|
description: This simple plugin gives you the ability to call to_xls to a collection
|
26
|
-
of activerecords
|
27
|
-
the :include.
|
26
|
+
of activerecords for Rails.
|
28
27
|
email: liangwenke.com@gmail.com
|
29
28
|
executables: []
|
30
29
|
extensions: []
|
@@ -59,8 +58,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
58
|
version: 1.3.4
|
60
59
|
requirements: []
|
61
60
|
rubyforge_project:
|
62
|
-
rubygems_version: 1.8.
|
61
|
+
rubygems_version: 1.8.12
|
63
62
|
signing_key:
|
64
63
|
specification_version: 3
|
65
|
-
summary: Export data to
|
64
|
+
summary: Export Rails ActiveRecord data to excel file
|
66
65
|
test_files: []
|