acts_as_caxlsx 1.0.8

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 448c5c50dbff1b5d6a745ae7335f445069fab20539b98df37d257edda4f8c9a9
4
+ data.tar.gz: d64c0d7721f2b13f6fc6c67f088b4f8ab4c38252aa5c3fbe9bd883b3356bd2b5
5
+ SHA512:
6
+ metadata.gz: c624c6baa51827f86e78fdd32d334b44fa2d23ced1fcf7f301a4635ad16ed26158d4f71b331e5030e652ceb9a4aaa69e70077da7ef882eaea4f291767a580d08
7
+ data.tar.gz: 2f93f2401e12d28d56577b3b07f59d3056b8097a88b5cd9ea6b46f08dd8d6a847de50821d5128d3068a4129e0525595b53caac9f74ed0ae817d56029d8c1efdf
data/.yardopts ADDED
@@ -0,0 +1,7 @@
1
+ --protected
2
+ --no-private
3
+ --main README.md
4
+ --title "acts_as_xlsx: a rails plug in for axlsx"
5
+ -
6
+ LICENSE
7
+ CHANGELOG.md
data/CHANGELOG.md ADDED
@@ -0,0 +1,27 @@
1
+ CHANGELOG
2
+ ---------
3
+
4
+ - **July.27.12**: 1.0.6
5
+ - conditionaly register XLSX mime type
6
+
7
+ - **February.14.12**: 1.0.5
8
+ - acts_as_xlsx propery declares it's dependancy on i18n instead of relying on the parent gem.
9
+
10
+ - **December.7.11**: 1.0.4
11
+ - acts_as_xlsx now supports specifying the Axlsx package the export will be added to
12
+ - Support for custom named and I18n names for worksheets.
13
+
14
+ - **December.6.11**: 1.0.3
15
+ - Added Mime type definition for rails to support respond_to |format| style handling in controllers.
16
+
17
+ - **December.3.11**: 1.0.2
18
+ Added support for chained method columns like :'model.association.attribute'
19
+
20
+ - **October.30.11**: 1.0.1
21
+ - Patch for inclusion error
22
+
23
+ - **October.30.11**: 1.0.0
24
+ - First release
25
+
26
+ - **October.30.11**: 1.0.0a
27
+ - First pre release
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2011 Randy Morgan
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # Acts as XLSX
2
+
3
+ [![Build Status](https://secure.travis-ci.org/caxlsx/acts_as_caxlsx.png)](http://travis-ci.org/caxlsx/acts_as_caxlsx/)
4
+
5
+ ## Notice: Community Axlsx Organization
6
+
7
+ To better maintain the Axlsx ecosystem, all related gems have been forked or moved to the following community organization:
8
+
9
+ http://github.com/caxlsx
10
+
11
+ [Join the Caxlsx Slack channel](https://join.slack.com/t/caxlsx/shared_invite/enQtOTI5OTM0MzI1Njk5LTBlMDQzNDk2YzkwODMxMmVkODMyYzJiZGU5NTQ3YTg5NTBlN2IwZTlmNTRjNzhiY2E0MDY2OTEyYmFlODI5NjA)
12
+
13
+ ## Synopsis
14
+
15
+ Acts_as_xlsx is an active record plugin for automatic Office Open XML Spreadsheet Generation using Axlsx. It makes generating excel spreadsheets from any subclass of ActiveRecord::Base as simple as a couple of lines of code.
16
+
17
+ ## Feature List
18
+
19
+ 1. Mixes into active record base to provide to_xlsx
20
+
21
+ 2. Can work at the end of any series of finder methods.
22
+
23
+ 3. Can accept any set of find options
24
+
25
+ 4. Automates localization of column heading with i18n support
26
+
27
+ 5. Lets you specify columns and methods chains you want to call to populate your table in one go.
28
+
29
+ 6. Gives you access to the axlsx package so you can add styles, charts and pictures to satisfy those flashy sales guys.
30
+
31
+ 7. Plays nicely with both ruby 1.8.7 + rails 2.3 as well as ruby 1.9.3 + rails 3
32
+
33
+ 8. Automatically registers xlsx Mime type for use in respond_to web-service support.
34
+
35
+ 9. Allows you to specify the Axlsx package to add your data to so you can create a single workbook with a sheet for each to_xlsx call.
36
+
37
+
38
+ ## Install
39
+
40
+ To install, add the following to your Gemfile:
41
+
42
+ ```ruby
43
+ gem 'acts_as_xlsx', git: 'https://github.com/caxlsx/acts_as_xlsx.git'
44
+ ```
45
+
46
+
47
+ ## Usage
48
+
49
+ The following Usage guides need to be extracted and added to this README
50
+
51
+ - http://axlsx.blog.randym.net/2011/12/using-actsasxlsx-to-generate-excel-data.html
52
+ - http://axlsx.blog.randym.net/2011/12/axlsx-making-excel-reports-with-ruby-on.html
53
+
54
+
55
+ ## Documentation
56
+
57
+ Detailed documentation is available at:
58
+
59
+ [https://www.rubydoc.info/gems/acts_as_xlsx/](https://www.rubydoc.info/gems/acts_as_xlsx/)
60
+
61
+
62
+ ## Credits
63
+
64
+ Originally created by Randy Morgan - @randym
65
+
66
+ Forked in 2019, to enable the community to maintain the Axlsx ecosystem - http://github.com/caxlsx
67
+
68
+ Open source software is a community effort. None of this could have been done without the help of [our Contributors](https://github.com/caxlsx/acts_as_xlsx/graphs/contributors).
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/lib/acts_as_xlsx/version.rb')
2
+
3
+ task :build => :gendoc do
4
+ system "gem build acts_as_xlsx.gemspec"
5
+ end
6
+
7
+ task :gendoc do
8
+ system "yardoc"
9
+ end
10
+
11
+ task :test do
12
+ require 'rake/testtask'
13
+ Rake::TestTask.new do |t|
14
+ t.libs << 'test'
15
+ t.test_files = FileList['test/**/tc_*.rb']
16
+ t.verbose = true
17
+ end
18
+ end
19
+
20
+ task :release => :build do
21
+ system "gem push acts_as_xlsx-#{Axlsx::Ar::VERSION}.gem"
22
+ end
23
+
24
+ task :default => :test
@@ -0,0 +1 @@
1
+ require 'acts_as_xlsx.rb'
@@ -0,0 +1,100 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Axlsx is a gem or generating excel spreadsheets with charts, images and many other features.
3
+ #
4
+ # acts_as_xlsx provides integration into active_record for Axlsx.
5
+ #
6
+ require 'axlsx'
7
+
8
+ # Adding to the Axlsx module
9
+ # @see http://github.com/randym/axlsx
10
+ module Axlsx
11
+ # === Overview
12
+ # This module defines the acts_as_xlsx class method and provides to_xlsx support to both AR classes and instances
13
+ module Ar
14
+
15
+ def self.included(base) # :nodoc:
16
+ base.send :extend, ClassMethods
17
+ end
18
+
19
+ # Class methods for the mixin
20
+ module ClassMethods
21
+
22
+ # defines the class method to inject to_xlsx
23
+ # @option options [Array, Symbol] columns an array of symbols defining the columns and methods to call in generating sheet data for each row.
24
+ # @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.
25
+ # @example
26
+ # class MyModel < ActiveRecord::Base
27
+ # acts_as_xlsx :columns=> [:id, :created_at, :updated_at], :i18n => 'activerecord.attributes'
28
+ def acts_as_xlsx(options={})
29
+ cattr_accessor :xlsx_i18n, :xlsx_columns
30
+ self.xlsx_i18n = options.delete(:i18n) || false
31
+ self.xlsx_columns = options.delete(:columns)
32
+ extend Axlsx::Ar::SingletonMethods
33
+ end
34
+ end
35
+
36
+ # Singleton methods for the mixin
37
+ module SingletonMethods
38
+
39
+ # Maps the AR class to an Axlsx package
40
+ # options are passed into AR find
41
+ # @param [Array, Array] columns as an array of symbols or a symbol that defines the attributes or methods to render in the sheet.
42
+ # @option options [Integer] header_style to apply to the first row of field names
43
+ # @option options [Array, Symbol] types an array of Axlsx types for each cell in data rows or a single type that will be applied to all types.
44
+ # @option options [Integer, Array] style The style to pass to Worksheet#add_row
45
+ # @option options [String] i18n The path to i18n attributes. (usually activerecord.attributes)
46
+ # @option options [Package] package An Axlsx::Package. When this is provided the output will be added to the package as a new sheet. # @option options [String] name This will be used to name the worksheet added to the package. If it is not provided the name of the table name will be humanized when i18n is not specified or the I18n.t for the table name.
47
+ # @see Worksheet#add_row
48
+ def to_xlsx(options = {})
49
+ if self.xlsx_columns.nil?
50
+ self.xlsx_columns = self.column_names.map { |c| c = c.to_sym }
51
+ end
52
+
53
+ row_style = options.delete(:style)
54
+ header_style = options.delete(:header_style) || row_style
55
+ types = [options.delete(:types) || []].flatten
56
+
57
+ i18n = options.delete(:i18n) || self.xlsx_i18n
58
+ columns = options.delete(:columns) || self.xlsx_columns
59
+
60
+ p = options.delete(:package) || Package.new
61
+ row_style = p.workbook.styles.add_style(row_style) unless row_style.nil?
62
+ header_style = p.workbook.styles.add_style(header_style) unless header_style.nil?
63
+ i18n = self.xlsx_i18n == true ? 'activerecord.attributes' : i18n
64
+ sheet_name = options.delete(:name) || (i18n ? (self.xlsx_i18n == true ? self.model_name.human : I18n.t("#{i18n}.#{table_name.underscore}")) : table_name.humanize)
65
+ data = options.delete(:data) || where(options[:where]).order(options[:order]).to_a
66
+ data = data.compact.flatten
67
+
68
+
69
+ return p if data.empty?
70
+ p.workbook.add_worksheet(:name=>sheet_name) do |sheet|
71
+
72
+ col_labels = if i18n
73
+ columns.map { |c| self.xlsx_i18n == true ? self.human_attribute_name(c) : I18n.t("#{i18n}.#{self.name.underscore}.#{c}") }
74
+ else
75
+ columns.map { |c| c.to_s.humanize }
76
+ end
77
+
78
+ sheet.add_row col_labels, :style=>header_style
79
+
80
+ data.each do |r|
81
+ row_data = columns.map do |c|
82
+ if c.to_s =~ /\./
83
+ v = r; c.to_s.split('.').each { |method| !v.nil? ? v = v.send(method) : v = ""; }; v
84
+ else
85
+ r.send(c)
86
+ end
87
+ end
88
+ sheet.add_row row_data, :style=>row_style, :types=>types
89
+ end
90
+ end
91
+ p
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ require 'active_record'
98
+ ActiveRecord::Base.send :include, Axlsx::Ar
99
+
100
+
@@ -0,0 +1,6 @@
1
+ module Axlsx
2
+ module Ar
3
+ # The current version of the gem
4
+ VERSION = "1.0.8"
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ require 'acts_as_xlsx/ar.rb'
2
+ begin
3
+ # The mime type to be used in respond_to |format| style web-services in rails
4
+ unless defined? Mime::XLSX
5
+ Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx
6
+ end
7
+ rescue NameError
8
+ puts "Mime module not defined. Skipping registration of xlsx"
9
+ end
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.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,77 @@
1
+ #!/usr/bin/env ruby -w
2
+ require 'test/unit'
3
+ require "acts_as_xlsx.rb"
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
+ def test_to_xlsx_with_package
23
+ p = Post.to_xlsx
24
+ Post.to_xlsx :package=>p, :name=>'another posts'
25
+ assert_equal p.workbook.worksheets.size, 2
26
+ end
27
+
28
+ def test_to_xlsx_with_name
29
+ p = Post.to_xlsx :name=>'bob'
30
+ assert_equal(p.workbook.worksheets.first.name, 'bob')
31
+ end
32
+
33
+ def test_xlsx_columns
34
+ assert_equal( Post.xlsx_columns, Post.column_names.map {|c| c.to_sym})
35
+ end
36
+
37
+ def test_to_xslx_vanilla
38
+ p = Post.to_xlsx
39
+ assert_equal("Id",p.workbook.worksheets.first.rows.first.cells.first.value)
40
+ assert_equal(2,p.workbook.worksheets.first.rows.last.cells.first.value)
41
+ end
42
+
43
+
44
+ def test_to_xslx_with_provided_data
45
+ p = Post.to_xlsx :data => Post.where(:title => "This is the first post").all
46
+ assert_equal("Id",p.workbook.worksheets.first.rows.first.cells.first.value)
47
+ assert_equal(1,p.workbook.worksheets.first.rows.last.cells.first.value)
48
+ end
49
+
50
+
51
+ def test_columns
52
+ p = Post.to_xlsx :columns => [:name, :title, :content, :votes]
53
+ sheet = p.workbook.worksheets.first
54
+ assert_equal(sheet.rows.first.cells.size, Post.xlsx_columns.size - 3)
55
+ assert_equal("Name",sheet.rows.first.cells.first.value)
56
+ assert_equal(7,sheet.rows.last.cells.last.value)
57
+ end
58
+
59
+ def test_method_in_columns
60
+ p = Post.to_xlsx :columns=>[:name, :votes, :content, :ranking]
61
+ sheet = p.workbook.worksheets.first
62
+ assert_equal("Name", sheet.rows.first.cells.first.value)
63
+ assert_equal(Post.last.ranking, sheet.rows.last.cells.last.value)
64
+ end
65
+
66
+ def test_chained_method
67
+ p = Post.to_xlsx :columns=>[:name, :votes, :content, :ranking, :'comments.last.content', :'comments.first.author.name']
68
+ sheet = p.workbook.worksheets.first
69
+ assert_equal("Name", sheet.rows.first.cells.first.value)
70
+ assert_equal(Post.last.comments.last.author.name, sheet.rows.last.cells.last.value)
71
+ end
72
+
73
+
74
+
75
+ end
76
+
77
+
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_caxlsx
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.8
5
+ platform: ruby
6
+ authors:
7
+ - Randy Morgan
8
+ - Noel Peden
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-07-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: caxlsx
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: i18n
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.1
55
+ description: |2
56
+ acts_as_xlsx lets you turn any ActiveRecord::Base inheriting class into an excel spreadsheet.
57
+ It can be added to any finder method or scope chain and can use localized column and sheet names with I18n.
58
+ email: noel@peden.biz
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".yardopts"
64
+ - CHANGELOG.md
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - lib/acts_as_caxlsx.rb
69
+ - lib/acts_as_xlsx.rb
70
+ - lib/acts_as_xlsx/ar.rb
71
+ - lib/acts_as_xlsx/version.rb
72
+ - test/database.yml
73
+ - test/helper.rb
74
+ - test/tc_acts_as_xlsx.rb
75
+ homepage: https://github.com/caxlsx/acts_as_caxlsx
76
+ licenses: []
77
+ metadata: {}
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '2.3'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.6.7
93
+ specification_version: 4
94
+ summary: ActiveRecord support for Axlsx
95
+ test_files:
96
+ - test/database.yml
97
+ - test/helper.rb
98
+ - test/tc_acts_as_xlsx.rb