to_xls-rails 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -8,4 +8,8 @@
8
8
  update documentation
9
9
 
10
10
  1.0.0
11
- fix typo
11
+ fix typo
12
+
13
+ 1.1.0
14
+ Translation of a column-value
15
+ Add options :header
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in refinerycms-multisite.gemspec
4
+ gemspec
data/README.rdoc CHANGED
@@ -19,6 +19,9 @@ This simple plugin gives you the ability to call to_xls to a collection of activ
19
19
  @posts.to_xls(:except => [:id])
20
20
  @posts.to_xls(:header => false)
21
21
 
22
+ # Translation of a column-value
23
+ @posts.to_xls{|column, value| column==:salutation ? t(value) : value}
24
+
22
25
 
23
26
  == Example
24
27
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
data/lib/to_xls-rails.rb CHANGED
@@ -2,30 +2,37 @@ require 'spreadsheet'
2
2
 
3
3
  class Array
4
4
 
5
- def to_xls( options = {} )
5
+ def to_xls(options = {}, &block)
6
6
  return '' if self.empty?
7
7
 
8
+ options.reverse_merge!(:header => true)
9
+
8
10
  xls_report = StringIO.new
9
11
  book = Spreadsheet::Workbook.new
10
12
  sheet = book.create_worksheet
11
-
13
+
12
14
  if options[:only]
13
15
  columns = Array(options[:only]).map(&:to_sym)
14
16
  else
15
17
  columns = self.first.class.column_names.map(&:to_sym) - Array(options[:except]).map(&:to_sym)
16
18
  end
17
-
19
+
18
20
  return '' if columns.empty?
19
-
20
- sheet.row(0).concat(columns.map(&:to_s).map(&:humanize))
21
-
21
+
22
+ sheet.row(0).concat(columns.map(&:to_s).map(&:humanize)) if options[:header]
23
+
22
24
  self.each_with_index do |obj, index|
23
- sheet.row(index + 1).replace( columns.map{ |column| obj.send(column) } )
25
+ index = options[:header] ? (index + 1) : index
26
+ if block
27
+ sheet.row(index).replace(columns.map { |column| block.call(column, obj.send(column)) })
28
+ else
29
+ sheet.row(index).replace(columns.map { |column| obj.send(column) })
30
+ end
24
31
  end
25
-
32
+
26
33
  book.write(xls_report)
27
-
34
+
28
35
  xls_report.string
29
36
  end
30
-
37
+
31
38
  end
data/to_xls-rails.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- version = File.read(File.expand_path("../VERSION",__FILE__)).strip
3
+ version = File.read(File.expand_path("../VERSION", __FILE__)).strip
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = 'to_xls-rails'
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
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_development_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
@@ -2,7 +2,7 @@
2
2
  name: to_xls-rails
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.0
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - LiangWenKe
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-01 00:00:00 Z
13
+ date: 2011-07-27 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: spreadsheet
@@ -21,7 +21,7 @@ dependencies:
21
21
  - - ~>
22
22
  - !ruby/object:Gem::Version
23
23
  version: 0.6.5
24
- type: :development
24
+ type: :runtime
25
25
  version_requirements: *id001
26
26
  description: 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.
27
27
  email: liangwenke.com@gmail.com
@@ -34,6 +34,7 @@ extra_rdoc_files: []
34
34
  files:
35
35
  - lib/to_xls-rails.rb
36
36
  - CHANGELOG
37
+ - Gemfile
37
38
  - init.rb
38
39
  - MIT-LICENSE
39
40
  - Rakefile