xlsxtream_rails 0.1.0
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +83 -0
- data/Rakefile +3 -0
- data/lib/tasks/xlsxtream_rails_tasks.rake +4 -0
- data/lib/xlsxtream_rails/action_controller.rb +56 -0
- data/lib/xlsxtream_rails/railtie.rb +14 -0
- data/lib/xlsxtream_rails/template_handler.rb +19 -0
- data/lib/xlsxtream_rails/utils.rb +37 -0
- data/lib/xlsxtream_rails/version.rb +3 -0
- data/lib/xlsxtream_rails.rb +11 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f95f772a88e172129234f4283c232efc799fe20b752675e6b44d8f96d6fbf582
|
4
|
+
data.tar.gz: 0e910d29f10e28c3ea51c7ec10e32708199118cfc803bafc0b17f28a5e00ee28
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f2d2e2d691e850a3b6d50637ee3b97f186b66172af768273ff73ab091db36619f2399063e9724fc38668f2c179ca518eab6ada601189465830189c60b969920b
|
7
|
+
data.tar.gz: dc0a03fe157d825c586f6af81a59083ac33fe0599115ba9baa43b2e8c3678b691b9c26dbd625d6d1cdeeab8739662947abfba48cebf428bc5397b5ea06a86bb9
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2022 doabit
|
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.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# XlsxtreamRails
|
2
|
+
Xlsx stream download for rails with template.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem "xlsxtream_rails"
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
```bash
|
13
|
+
$ bundle
|
14
|
+
```
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
```bash
|
18
|
+
$ gem install xlsxtream_rails
|
19
|
+
```
|
20
|
+
|
21
|
+
## Controller
|
22
|
+
|
23
|
+
Set your instance variables in your controller and configure the response if needed:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class PostsController < ApplicationController
|
27
|
+
def index
|
28
|
+
@posts = Post.all
|
29
|
+
respond_to do |format|
|
30
|
+
format.xlsx
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
## Model
|
37
|
+
|
38
|
+
Add `xlsx_columns` method for model:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class Post < ApplicationRecord
|
42
|
+
def xlsx_columns
|
43
|
+
[:id, :title, :body]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
or
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
class Post < ApplicationRecord
|
52
|
+
belongs_to :category
|
53
|
+
|
54
|
+
def xlsx_columns
|
55
|
+
[
|
56
|
+
[:category, category.name],
|
57
|
+
:title,
|
58
|
+
:body
|
59
|
+
]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
## View
|
65
|
+
|
66
|
+
If you want to render with template, you can create the template with the .xlsx.xrb extension (index.xlsx.xrb for example)
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
xlsx.write_worksheet do |sheet|
|
70
|
+
@posts.find_each do |post|
|
71
|
+
sheet.add_row [post.id, post.title]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
## This gem is inspired by the following
|
77
|
+
|
78
|
+
[spreadsheet_architect](https://github.com/westonganger/spreadsheet_architect)
|
79
|
+
|
80
|
+
[caxlsx_rails](https://github.com/caxlsx/caxlsx_rails)
|
81
|
+
|
82
|
+
## License
|
83
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require "action_controller"
|
2
|
+
require "stringio"
|
3
|
+
|
4
|
+
Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx
|
5
|
+
|
6
|
+
ActionController::Renderers.add :xlsx do |data, options|
|
7
|
+
# force layout false
|
8
|
+
options[:layout] = false
|
9
|
+
# disposition / filename
|
10
|
+
options[:disposition] = options.delete(:disposition) || "attachment"
|
11
|
+
|
12
|
+
if defined?(ActiveRecord) && data.is_a?(ActiveRecord::Relation)
|
13
|
+
options[:filename] ||= data.klass.name.pluralize
|
14
|
+
end
|
15
|
+
|
16
|
+
options[:filename] = options[:filename] ? options[:filename].strip.sub(/\.xlsx$/i, "") : "data"
|
17
|
+
options[:filename] += ".xlsx"
|
18
|
+
|
19
|
+
headers["Content-Type"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
20
|
+
headers["Content-disposition"] = "attachment; filename=\"#{options[:filename]}.xlsx\""
|
21
|
+
headers["X-Accel-Buffering"] = "no"
|
22
|
+
headers["Cache-Control"] = "no-cache"
|
23
|
+
headers["Last-Modified"] = Time.zone.now.ctime.to_s
|
24
|
+
headers.delete("Content-Length")
|
25
|
+
self.response_body = Enumerator.new do |enumerator|
|
26
|
+
xlsx = Xlsxtream::Workbook.new(enumerator)
|
27
|
+
if lookup_context.template_exists?(options[:template], options[:prefixes])
|
28
|
+
source = lookup_context.find_template(options[:template], options[:prefixes], false).source
|
29
|
+
builder = StringIO.new
|
30
|
+
builder << "# encoding: utf-8\n"
|
31
|
+
builder << source
|
32
|
+
view_context.instance_eval(builder.string)
|
33
|
+
else
|
34
|
+
XlsxtreamRails::Utils.write_xls(xlsx, data)
|
35
|
+
end
|
36
|
+
|
37
|
+
xlsx.close
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# For respond_to default
|
42
|
+
begin
|
43
|
+
ActionController::Responder
|
44
|
+
rescue
|
45
|
+
else
|
46
|
+
class ActionController::Responder
|
47
|
+
def to_xlsx
|
48
|
+
@_action_has_layout = false
|
49
|
+
if @default_response
|
50
|
+
@default_response.call(options)
|
51
|
+
else
|
52
|
+
controller.render({xlsx: controller.action_name}.merge(options))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module XlsxtreamRails
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
initializer "xlsxtream_rails.initialization" do
|
4
|
+
ActiveSupport.on_load(:action_view) do
|
5
|
+
require "xlsxtream_rails/template_handler"
|
6
|
+
ActionView::Template.register_template_handler :xrb, ActionView::Template::Handlers::XrbBuilder.new
|
7
|
+
end
|
8
|
+
|
9
|
+
ActiveSupport.on_load(:action_controller) do
|
10
|
+
require "xlsxtream_rails/action_controller"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "action_view"
|
2
|
+
require "stringio"
|
3
|
+
|
4
|
+
module ActionView
|
5
|
+
module Template::Handlers
|
6
|
+
class XrbBuilder
|
7
|
+
def default_format
|
8
|
+
Mime[:xlsx].symbol
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(template, source = nil)
|
12
|
+
builder = StringIO.new
|
13
|
+
builder << "# encoding: utf-8\n"
|
14
|
+
builder << (source || template.source)
|
15
|
+
builder.string
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module XlsxtreamRails
|
2
|
+
module Utils
|
3
|
+
def self.write_xls(xlsx, data)
|
4
|
+
headers = []
|
5
|
+
needs_headers = true
|
6
|
+
needs_column_types = false
|
7
|
+
xlsx.write_worksheet(auto_format: false) do |sheet|
|
8
|
+
data.find_each do |instance, index|
|
9
|
+
row_data = []
|
10
|
+
instance_cols = instance.xlsx_columns
|
11
|
+
instance_cols.each_with_index do |x, i|
|
12
|
+
if x.is_a?(Array)
|
13
|
+
headers.push(x[0].to_s) if needs_headers
|
14
|
+
row_data.push(x[1].is_a?(Symbol) ? instance.send(x[1]) : x[1])
|
15
|
+
if needs_column_types
|
16
|
+
column_types[i] = x[2]
|
17
|
+
end
|
18
|
+
else
|
19
|
+
headers.push(str_titleize(x.to_s)) if needs_headers
|
20
|
+
row_data.push(x.is_a?(Symbol) ? instance.send(x) : x)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
sheet.add_row headers if needs_headers
|
24
|
+
needs_headers = false
|
25
|
+
sheet.add_row row_data
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.str_titleize(str)
|
31
|
+
str.sub(/\A_+/, "")
|
32
|
+
.gsub(/[_.]/, " ")
|
33
|
+
.sub(" rescue nil", "")
|
34
|
+
.gsub(/(\A|\ )\w/) { |x| x.upcase }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "xlsxtream"
|
2
|
+
require "xlsxtream_rails/utils"
|
3
|
+
require "xlsxtream_rails/version"
|
4
|
+
require "xlsxtream_rails/railtie"
|
5
|
+
|
6
|
+
module XlsxtreamRails
|
7
|
+
def xlsx_columns(opts = {})
|
8
|
+
column_names = self.class.column_names
|
9
|
+
column_names.map { |x| x.to_sym }
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xlsxtream_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- doabit
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-03-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: xlsxtream
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.4.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.4.0
|
41
|
+
description: Xlsx stream download for rails with template.
|
42
|
+
email:
|
43
|
+
- doinsist@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/tasks/xlsxtream_rails_tasks.rake
|
52
|
+
- lib/xlsxtream_rails.rb
|
53
|
+
- lib/xlsxtream_rails/action_controller.rb
|
54
|
+
- lib/xlsxtream_rails/railtie.rb
|
55
|
+
- lib/xlsxtream_rails/template_handler.rb
|
56
|
+
- lib/xlsxtream_rails/utils.rb
|
57
|
+
- lib/xlsxtream_rails/version.rb
|
58
|
+
homepage: https://github.com/ruby-gems/xlsxtream_rails
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata:
|
62
|
+
homepage_uri: https://github.com/ruby-gems/xlsxtream_rails
|
63
|
+
source_code_uri: https://github.com/ruby-gems/xlsxtream_rails
|
64
|
+
changelog_uri: https://github.com/ruby-gems/xlsxtream_rails
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubygems_version: 3.2.3
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Xlsx stream download for rails with template.
|
84
|
+
test_files: []
|