lp_csv_exportable_with_stream 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8b0e1113b747629d9b0b0cca628298e191d9d186fcfa2d5402c9af8874882a36
4
+ data.tar.gz: cc7e4df25168bfbeca22de97eb84034e90caa84177380abe434f82812991808e
5
+ SHA512:
6
+ metadata.gz: 4f92ce7f354a2b3bce3867fae10342d84e4e05051ebeef6e82a240dd97cbc474599b8f8c907b35381f76b2c1c18bfd33c3f2df55075d4069fb18dc2238607dd2
7
+ data.tar.gz: 0a764b3271b700ce5953a2fbd985bf8074b87c4b07193fc7fa41c6de3a646e44f787db5a74f02e66e254e0421b414b3bc7ca050799dcc53b6a0958dcdc21376d
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-03-31
4
+
5
+ - Initial release
6
+ - Add filename setting
7
+ - Default encode to UTF-8
8
+ - Download with Stream
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in lp_csv_exportable_with_stream.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "lp_csv_exportable"
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lp_csv_exportable_with_stream (0.1.0)
5
+ lp_csv_exportable
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (6.1.5)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ zeitwerk (~> 2.3)
16
+ concurrent-ruby (1.1.10)
17
+ i18n (1.10.0)
18
+ concurrent-ruby (~> 1.0)
19
+ lp_csv_exportable (0.2.9)
20
+ activesupport
21
+ minitest (5.15.0)
22
+ rake (13.0.6)
23
+ tzinfo (2.0.4)
24
+ concurrent-ruby (~> 1.0)
25
+ zeitwerk (2.5.4)
26
+
27
+ PLATFORMS
28
+ arm64-darwin-21
29
+
30
+ DEPENDENCIES
31
+ lp_csv_exportable
32
+ lp_csv_exportable_with_stream!
33
+ rake (~> 13.0)
34
+
35
+ BUNDLED WITH
36
+ 2.3.8
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 z01241
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # LpCSVExportableWithStream
2
+
3
+ This is an extension of [LaunchPadLab/lp_csv_exportable](https://github.com/LaunchPadLab/lp_csv_exportable), let you can export data with Live stream.
4
+
5
+ When Export lager data, rails will fetch all data what you need and make generate scv data. Your download will start after scv file generated, that's This will make your project seems to broken.
6
+
7
+ But Live Stream will generate file while downloading. It's good for your user experience
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'lp_csv_exportable_with_stream'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle install
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install lp_csv_exportable_with_stream
25
+
26
+ ## Usage
27
+
28
+ 1. Follow the [LaunchPadLab/lp_csv_exportable](https://github.com/LaunchPadLab/lp_csv_exportable) basic usage. Like this
29
+
30
+ ```ruby
31
+ class ExportUsers
32
+ include LpCSVExportable::CanExportAsCSV
33
+
34
+ column :first_name
35
+ column :last_name
36
+ column :email
37
+ column 'User Role', model_methods: %i[membership name]
38
+ end
39
+
40
+ ```
41
+
42
+ 2. And add `include LpCSVExportable::WithStream` below `include LpCSVExportable::CanExportAsCSV`
43
+ ```ruby
44
+ include LpCSVExportable::CanExportAsCSV
45
+ include LpCSVExportable::WithStream
46
+
47
+ column :first_name
48
+ ```
49
+
50
+ 3. finally, replace `.to_csv` with `.to_csv_lazy(self)`
51
+
52
+ ```ruby
53
+ def index
54
+ users = User.all
55
+
56
+ respond_to do |format|
57
+ format.csv do
58
+ export = ExportUsers.new
59
+ export.collection = users
60
+ export.to_csv_lazy(self)
61
+ end
62
+ end
63
+ end
64
+ ```
65
+
66
+ ## Filename Setting
67
+
68
+ You can setting your filename in Export Class, just add self.filename = "Your File Name"
69
+
70
+ ```ruby
71
+ class ExportUsers
72
+ include LpCSVExportable::CanExportAsCSV
73
+ include LpCSVExportable::WithStream
74
+
75
+ self.filename = "Your File Name" # This is base file name
76
+
77
+ ```
78
+
79
+ Also, you can add prefix ro suffix for your file name when export. maybe exported date and so on.
80
+
81
+ ```ruby
82
+ respond_to do |format|
83
+ format.csv do
84
+ export = ExportUsers.new
85
+ export.collection = users
86
+ export.filename_prefix = "#{Date.today.strftime("%Y-%m-%d %H:%M")}" # add prefix to your base filename
87
+ export.filename_suffix = "version 2" # add suffix to your base filename
88
+ export.to_csv_lazy(self)
89
+ end
90
+ end
91
+
92
+ ```
93
+
94
+
95
+ ## Prevent N + 1 query
96
+
97
+ If your need to prevent n + 1 query, you can add `self.collection_includes = [:your, :relations]`, it will automatically includes relations of your collection
98
+
99
+ ```ruby
100
+ class ExportUsers
101
+ include LpCSVExportable::CanExportAsCSV
102
+ include LpCSVExportable::WithStream
103
+
104
+ self.filename = "Your File Name"
105
+ self.collection_includes = [:address, :friends] # Equivalence with "collection.includes(:address, :friends)"
106
+ ```
107
+ #### Note
108
+ What's N + 1 Query?
109
+ [Faster Rails: Eliminating N+1 queries](https://semaphoreci.com/blog/2017/08/09/faster-rails-eliminating-n-plus-one-queries.html)
110
+
111
+
112
+ ## Development
113
+
114
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
115
+
116
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
117
+
118
+ ## Contributing
119
+
120
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lp_csv_exportable_with_stream.
121
+
122
+ ## License
123
+
124
+ 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,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LpCSVExportableWithStream
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,45 @@
1
+ module LpCSVExportable
2
+ module WithStream
3
+ attr_accessor :filename_prefix, :filename_suffix
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ def to_csv_lazy(formatter)
10
+ formatter.headers['Content-Type'] = 'text/csv; charset=utf-8'
11
+ formatter.headers['Content-Disposition'] =
12
+ "attachment; filename=\"#{filename_prefix}#{self.class.filename}#{filename_suffix}.csv\""
13
+ formatter.headers['X-Accel-Buffering'] = 'no'
14
+ formatter.headers['Cache-Control'] = 'no-cache'
15
+ formatter.headers['Last-Modified'] = Time.zone.now.ctime.to_s
16
+ formatter.headers.delete('Content-Length')
17
+ formatter.response_body = Enumerator.new do |yielder|
18
+ yielder << "\uFEFF" + CSV.generate_line(headers).to_s.force_encoding('UTF-8')
19
+ collection.includes(self.class.collection_includes || []).each do |obj|
20
+ yielder << CSV.generate_line(columns.map do |column|
21
+ column.format(retrieve_value(obj, column))
22
+ end).to_s.force_encoding('UTF-8')
23
+ end
24
+ end
25
+ end
26
+
27
+ module ClassMethods
28
+ def filename=(name)
29
+ @filename = name
30
+ end
31
+
32
+ def filename
33
+ @filename
34
+ end
35
+
36
+ def collection_includes=(collection_includes)
37
+ @collection_includes = collection_includes
38
+ end
39
+
40
+ def collection_includes
41
+ @collection_includes
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lp_csv_exportable_with_stream/version"
4
+
5
+ module LpCSVExportableWithStream
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
9
+
10
+ require 'lp_csv_exportable_with_stream/with_stream'
@@ -0,0 +1,4 @@
1
+ module LpCSVExportableWithStream
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lp_csv_exportable_with_stream
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - z22919456
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lp_csv_exportable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ description: Export file with stream, free your thread
28
+ email:
29
+ - z22919456@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/lp_csv_exportable_with_stream.rb
41
+ - lib/lp_csv_exportable_with_stream/version.rb
42
+ - lib/lp_csv_exportable_with_stream/with_stream.rb
43
+ - sig/lp_csv_exportable_with_stream.rbs
44
+ homepage: https://github.com/z22919456/lp_csv_exportable_with_stream
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/z22919456/lp_csv_exportable_with_stream
49
+ source_code_uri: https://github.com/z22919456/lp_csv_exportable_with_stream
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.4.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.0.9
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Extension of lp_csv_exportable, let export file can be download by stream
69
+ test_files: []