acts_as_xlsx 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,103 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Acts As Xlsx (Axlsx)
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ relpath = '';
19
+ if (relpath != '') relpath += '/';
20
+ </script>
21
+
22
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
23
+
24
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
25
+
26
+
27
+ </head>
28
+ <body>
29
+ <script type="text/javascript" charset="utf-8">
30
+ if (window.top.frames.main) document.body.className = 'frames';
31
+ </script>
32
+
33
+ <div id="header">
34
+ <div id="menu">
35
+
36
+ <a href="_index.html">Index</a> &raquo;
37
+
38
+
39
+ <span class="title">Top Level Namespace</span>
40
+
41
+
42
+ <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
43
+ </div>
44
+
45
+ <div id="search">
46
+
47
+ <a id="class_list_link" href="#">Class List</a>
48
+
49
+ <a id="method_list_link" href="#">Method List</a>
50
+
51
+ <a id="file_list_link" href="#">File List</a>
52
+
53
+ </div>
54
+ <div class="clear"></div>
55
+ </div>
56
+
57
+ <iframe id="search_frame"></iframe>
58
+
59
+ <div id="content"><h1>Top Level Namespace
60
+
61
+
62
+
63
+ </h1>
64
+
65
+ <dl class="box">
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+ </dl>
75
+ <div class="clear"></div>
76
+
77
+ <h2>Defined Under Namespace</h2>
78
+ <p class="children">
79
+
80
+
81
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="Axlsx.html" title="Axlsx (module)">Axlsx</a></span>
82
+
83
+
84
+
85
+
86
+ </p>
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+ </div>
95
+
96
+ <div id="footer">
97
+ Generated on Thu Dec 1 00:04:39 2011 by
98
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
99
+ 0.7.3 (ruby-1.9.3).
100
+ </div>
101
+
102
+ </body>
103
+ </html>
@@ -0,0 +1,87 @@
1
+ # Axlsx is a gem or generating excel spreadsheets with charts, images and many other features.
2
+ #
3
+ # acts_as_xlsx provides integration into active_record for Axlsx.
4
+ #
5
+ require 'axlsx'
6
+
7
+ # Adding to the Axlsx module
8
+ # @see http://github.com/randym/axlsx
9
+ module Axlsx
10
+ # === Overview
11
+ # This module defines the acts_as_xlsx class method and provides to_xlsx support to both AR classes and instances
12
+ module Ar
13
+
14
+ def self.included(base) # :nodoc:
15
+ base.send :extend, ClassMethods
16
+ end
17
+
18
+ # Class methods for the mixin
19
+ module ClassMethods
20
+
21
+ # defines the class method to inject to_xlsx
22
+ # @option options [Array, Symbol] columns an array of symbols defining the columns and methods to call in generating sheet data for each row.
23
+ # @option options [String] i18n (default nil) The path to search for localization. When this is specified your i18n.t will be used to determine the labels for columns.
24
+ # @example
25
+ # class MyModel < ActiveRecord::Base
26
+ # acts_as_xlsx :columns=> [:id, :created_at, :updated_at], :i18n => 'activerecord.attributes'
27
+ def acts_as_xlsx(options={})
28
+ cattr_accessor :xlsx_i18n, :xlsx_columns
29
+ self.xlsx_i18n = options.delete(:i18n) || false
30
+ self.xlsx_columns = options.delete(:columns) || self.column_names.map { |c| c = c.to_sym }
31
+ extend Axlsx::Ar::SingletonMethods
32
+ end
33
+ end
34
+
35
+ # Singleton methods for the mixin
36
+ module SingletonMethods
37
+
38
+ # Maps the AR class to an Axlsx package
39
+ # options are passed into AR find
40
+ # @param [Array, Array] columns as an array of symbols or a symbol that defines the attributes or methods to render in the sheet.
41
+ # @option options [Integer] header_style to apply to the first row of field names
42
+ # @option options [Array, Symbol] an array of Axlsx types for each cell in data rows or a single type that will be applied to all types.
43
+ # @option options [Integer, Array] style The style to pass to Worksheet#add_row
44
+ # @option options [String] i18n The path to i18n attributes. (usually activerecord.attributes)
45
+ # @see Worksheet#add_row
46
+ def to_xlsx(options = {})
47
+
48
+ row_style = options.delete(:style)
49
+ header_style = options.delete(:header_style) || row_style
50
+ types = [options.delete(:types) || []].flatten
51
+
52
+ i18n = options.delete(:i18n) || self.xlsx_i18n
53
+ columns = options.delete(:columns) || self.xlsx_columns
54
+
55
+ p = Package.new
56
+ row_style = p.workbook.styles.add_style(row_style) unless row_style.nil?
57
+ header_style = p.workbook.styles.add_style(header_style) unless header_style.nil?
58
+
59
+ data = [*find(:all, options)]
60
+ data.compact!
61
+ data.flatten!
62
+
63
+ return p if data.empty?
64
+ p.workbook.add_worksheet(:name=>table_name.humanize) do |sheet|
65
+
66
+ col_labels = if i18n
67
+ columns.map { |c| I18n.t("#{i18n}.#{self.name.underscore}.#{c}") }
68
+ else
69
+ columns.map { |c| c.to_s.humanize }
70
+ end
71
+
72
+ sheet.add_row col_labels, :style=>header_style
73
+
74
+ data.each do |r|
75
+ sheet.add_row columns.map { |c| r.send(c) }, :style=>row_style, :types=>types
76
+ end
77
+ end
78
+ p
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ require 'active_record'
85
+ ActiveRecord::Base.send :include, Axlsx::Ar
86
+
87
+
Binary file
data/test/database.yml ADDED
@@ -0,0 +1,3 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: test/acts_as_xlsx.sqlite3.db
data/test/helper.rb ADDED
@@ -0,0 +1,72 @@
1
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
2
+ ActiveRecord::Base.establish_connection(config['sqlite3'])
3
+ ActiveRecord::Schema.define(:version => 0) do
4
+ begin
5
+ drop_table :author, :force => true
6
+ drop_table :authors, :force => true
7
+ drop_table :comments, :force => true
8
+ drop_table :posts, :force => true
9
+ rescue
10
+ #dont really care if the tables are not dropped
11
+ end
12
+
13
+ create_table(:authors, :force => true) do |t|
14
+ t.string :name
15
+ end
16
+
17
+ create_table(:comments, :force => true) do |t|
18
+ t.text :content
19
+ t.integer :post_id
20
+ t.integer :author_id
21
+ t.timestamps
22
+ end
23
+
24
+ create_table(:posts, :force => true) do |t|
25
+ t.string :name
26
+ t.string :title
27
+ t.text :content
28
+ t.integer :votes
29
+ t.timestamps
30
+ end
31
+
32
+ end
33
+
34
+ class Author < ActiveRecord::Base
35
+ acts_as_xlsx
36
+ has_many :comments
37
+ end
38
+
39
+ class Comment < ActiveRecord::Base
40
+ acts_as_xlsx
41
+ belongs_to :post
42
+ belongs_to :author
43
+ end
44
+
45
+ class Post < ActiveRecord::Base
46
+ acts_as_xlsx
47
+ has_many :comments
48
+ def ranking
49
+ a = Post.find(:all, :order =>"votes desc")
50
+ a.index(self) + 1
51
+ end
52
+ def last_comment
53
+ self.comments.last.content
54
+ end
55
+ end
56
+
57
+ posts = []
58
+ posts << Post.new(:name => "first post", :title => "This is the first post", :content=> "I am a very good first post!", :votes => 1)
59
+ posts << Post.new(:name => "second post", :title => "This is the second post", :content=> "I am the best post!", :votes => 7)
60
+ posts.each { |p| p.save! }
61
+
62
+ authors = []
63
+ authors << Author.new(:name => 'bob')
64
+ authors << Author.new(:name => 'joe')
65
+
66
+ comments = []
67
+ comments << Comment.new(:post => posts[0], :content => "wow, that was a nice post!", :author=>authors[1])
68
+ comments << Comment.new(:content => "Are you really the best post?", :post => posts[1], :author=>authors[0])
69
+ comments << Comment.new(:content => "Only until someone posts better!", :post => posts[1], :author=>authors[0])
70
+ comments.each { |c| c.save }
71
+
72
+
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby -w
2
+ require 'test/unit'
3
+ require "axlsx/acts_as_xlsx"
4
+ require 'active_record'
5
+
6
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
7
+
8
+ class TestActsAsXlsx < Test::Unit::TestCase
9
+
10
+ class Post < ActiveRecord::Base
11
+ acts_as_xlsx :columns=>[:name, :title, :content, :votes, :ranking], :i18n => 'activerecord.attributes'
12
+ end
13
+
14
+ def test_xlsx_options
15
+ assert_equal([:name, :title, :content, :votes, :ranking], Post.xlsx_columns)
16
+ assert_equal('activerecord.attributes', Post.xlsx_i18n)
17
+ end
18
+
19
+ end
20
+
21
+ class TestToXlsx < Test::Unit::TestCase
22
+
23
+ def test_xlsx_columns
24
+ assert_equal( Post.xlsx_columns, Post.column_names.map {|c| c.to_sym})
25
+ end
26
+
27
+ def test_to_xslx_vanilla
28
+ p = Post.to_xlsx
29
+ assert_equal("Id",p.workbook.worksheets.first.rows.first.cells.first.value)
30
+ assert_equal(2,p.workbook.worksheets.first.rows.last.cells.first.value)
31
+ end
32
+
33
+ def test_columns
34
+ p = Post.to_xlsx :columns => [:name, :title, :content, :votes]
35
+ sheet = p.workbook.worksheets.first
36
+ assert_equal(sheet.rows.first.cells.size, Post.xlsx_columns.size - 3)
37
+ assert_equal("Name",sheet.rows.first.cells.first.value)
38
+ assert_equal(7,sheet.rows.last.cells.last.value)
39
+ end
40
+
41
+ def test_method_in_columns
42
+ p = Post.to_xlsx :columns=>[:name, :votes, :content, :ranking]
43
+ sheet = p.workbook.worksheets.first
44
+ assert_equal("Name", sheet.rows.first.cells.first.value)
45
+ assert_equal(Post.last.ranking, sheet.rows.last.cells.last.value)
46
+ end
47
+ end
48
+
49
+
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_xlsx
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Randy Morgan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: axlsx
16
+ requirement: &2153137140 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.10
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2153137140
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &2153136660 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 2.3.9
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2153136660
36
+ - !ruby/object:Gem::Dependency
37
+ name: i18n
38
+ requirement: &2153135840 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 0.6.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2153135840
47
+ - !ruby/object:Gem::Dependency
48
+ name: yard
49
+ requirement: &2153135180 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *2153135180
58
+ - !ruby/object:Gem::Dependency
59
+ name: rdiscount
60
+ requirement: &2153134060 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *2153134060
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: &2153133120 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *2153133120
80
+ - !ruby/object:Gem::Dependency
81
+ name: rake
82
+ requirement: &2153132140 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: 0.9.2
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *2153132140
91
+ description: ! ' acts_as_xlsx lets you turn any ActiveRecord::Base inheriting class
92
+ into an excel spreadsheet.
93
+
94
+ '
95
+ email: digital.ipseity@gmail.com
96
+ executables: []
97
+ extensions: []
98
+ extra_rdoc_files: []
99
+ files:
100
+ - acts_as_xlsx.gemspec
101
+ - CHANGELOG.md
102
+ - Gemfile
103
+ - Gemfile.lock
104
+ - LICENSE
105
+ - Rakefile
106
+ - README.md
107
+ - lib/axlsx/acts_as_xlsx.rb
108
+ - doc/_index.html
109
+ - doc/Axlsx/Ar/ClassMethods.html
110
+ - doc/Axlsx/Ar/InstanceMethods.html
111
+ - doc/Axlsx/Ar/SingletonMethods.html
112
+ - doc/Axlsx/Ar.html
113
+ - doc/Axlsx.html
114
+ - doc/class_list.html
115
+ - doc/css/common.css
116
+ - doc/css/full_list.css
117
+ - doc/css/style.css
118
+ - doc/file.LICENSE.html
119
+ - doc/file.README.html
120
+ - doc/file_list.html
121
+ - doc/frames.html
122
+ - doc/index.html
123
+ - doc/js/app.js
124
+ - doc/js/full_list.js
125
+ - doc/js/jquery.js
126
+ - doc/method_list.html
127
+ - doc/top-level-namespace.html
128
+ - test/acts_as_xlsx.sqlite3.db
129
+ - test/database.yml
130
+ - test/helper.rb
131
+ - test/tc_acts_as_xlsx.rb
132
+ homepage: https://github.com/randym/acts_as_xlsx
133
+ licenses: []
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 1.8.10
153
+ signing_key:
154
+ specification_version: 3
155
+ summary: ActiveRecord support for Axlsx
156
+ test_files: []
157
+ has_rdoc: acts_as_xlsx