table2csv 0.1.4 → 1.0.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 CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZTQxMTEwYjJlN2MzMDcyOTIzYmE5YjY3NjgxYWNhMmUzODFhZTFiNg==
5
- data.tar.gz: !binary |-
6
- NGJmOWRjZDM0ZDAwODI3OWE1MWY2YWI1NzQwZWYyYmEzNGFmMDQ5Yw==
2
+ SHA256:
3
+ metadata.gz: bfafecc7ad87af9075be3fa9bc2c94906cd58ac732d961caeed8b11b160df179
4
+ data.tar.gz: 7b8da084106f20f6e778cefeb79a954b66a07c2de831046a2ce2ab644475b5a0
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZTIzYjI0NWVkOGVkYmJmYzFkZjIwYjhiYmQ0NzBjMmQ0MjA3MjQyNTAzNGRi
10
- M2QzZWNhODU0ZGRlNWVjZjM1MDhhNTk0NTY1YjVkMThjNTc4OGRhM2VkZDk0
11
- NmMwOWMwYzgxYTFmYWFjZjg3ZGZlY2NhMDI4ZjgwMzdkZjdkYTE=
12
- data.tar.gz: !binary |-
13
- ZDAwYmYxMmNlN2ZlYzg2YzBmM2I3MjRjMzAwY2RiNjM2NWViZWYxNmZhZmIw
14
- MjBiNDEzOGUyZjM0NTRhYjgzZDNhN2RlMmU1ZGIxMWMyODMyMDMyNWYzYjE0
15
- OThiY2IxMTBjYjI5MTM2OWNkYzM1NWE3MmU2ZTFmYTg3ZjBhYTI=
6
+ metadata.gz: c892cc33110e7f1cc670e6cd77f9e0e0185b37feb0391511991b8dc417dcd43acba2ec9cd3b05a9e92f36c813c2b9a2496b5eab40a1af7223842665f095607ea
7
+ data.tar.gz: d91bee960074d1a56f05dfad1e4f7d870999b587f70bcbd83ddbbd4fefe669851f94ce74e2cca01b741f173e5fec3496bd2d11f1d14dec260214bb4a160a0f8d
@@ -0,0 +1,55 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [ main, master ]
6
+ pull_request:
7
+ branches: [ main, master ]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ fail-fast: false
14
+ matrix:
15
+ ruby: ['3.0', '3.1', '3.2', '3.3']
16
+ rails: ['6.0', '6.1', '7.0', '7.1', '7.2', '8.0']
17
+ exclude:
18
+ # Rails 7.2+ requires Ruby 3.1+
19
+ - ruby: '3.0'
20
+ rails: '7.2'
21
+ - ruby: '3.0'
22
+ rails: '8.0'
23
+
24
+ env:
25
+ RAILS_VERSION: ${{ matrix.rails }}
26
+
27
+ steps:
28
+ - uses: actions/checkout@v4
29
+
30
+ - name: Set up Ruby
31
+ uses: ruby/setup-ruby@v1
32
+ with:
33
+ ruby-version: ${{ matrix.ruby }}
34
+ bundler-cache: true
35
+
36
+ - name: Run tests
37
+ run: bundle exec rake
38
+
39
+ - name: Run RuboCop
40
+ run: bundle exec rubocop
41
+
42
+ security:
43
+ runs-on: ubuntu-latest
44
+
45
+ steps:
46
+ - uses: actions/checkout@v4
47
+
48
+ - name: Set up Ruby
49
+ uses: ruby/setup-ruby@v1
50
+ with:
51
+ ruby-version: '3.3'
52
+ bundler-cache: true
53
+
54
+ - name: Security audit
55
+ run: bundle exec bundle-audit --update
data/.gitignore CHANGED
@@ -1,9 +1,38 @@
1
+ ## Ruby/Bundler
1
2
  /.bundle/
2
- /.yardoc
3
+ /vendor/bundle/
3
4
  /Gemfile.lock
5
+
6
+ ## Documentation
7
+ /.yardoc/
4
8
  /_yardoc/
5
- /coverage/
6
9
  /doc/
7
- /pkg/
10
+ /rdoc/
11
+
12
+ ## Testing
13
+ /coverage/
8
14
  /spec/reports/
15
+ /test/tmp/
16
+ /test/version_tmp/
17
+
18
+ ## Build artifacts
19
+ /pkg/
9
20
  /tmp/
21
+ *.gem
22
+
23
+ ## IDE and OS files
24
+ .DS_Store
25
+ .idea/
26
+ .vscode/
27
+ *.swp
28
+ *.swo
29
+ *~
30
+ .ruby-version
31
+ .ruby-gemset
32
+
33
+ ## Environment
34
+ .env
35
+ .env.local
36
+
37
+ ## Logs
38
+ npm-debug.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,34 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+ Exclude:
6
+ - 'vendor/**/*'
7
+ - 'bin/**/*'
8
+
9
+ Style/Documentation:
10
+ Enabled: false
11
+
12
+ Style/StringLiterals:
13
+ Enabled: true
14
+ EnforcedStyle: double_quotes
15
+
16
+ Style/FrozenStringLiteralComment:
17
+ Enabled: true
18
+ EnforcedStyle: always
19
+
20
+ Layout/LineLength:
21
+ Max: 120
22
+ Exclude:
23
+ - '*.gemspec'
24
+
25
+ Metrics/BlockLength:
26
+ Exclude:
27
+ - 'spec/**/*'
28
+ - '*.gemspec'
29
+
30
+ Gemspec/DevelopmentDependencies:
31
+ Enabled: false
32
+
33
+ Gemspec/AddRuntimeDependency:
34
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,91 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.0.0] - 2026-05-29
9
+
10
+ ### ✨ Added
11
+
12
+ - **New CSVExporter JavaScript class** - Replaces jQuery-based implementation with vanilla ES6+
13
+ - **Auto-initialization** - Automatically initializes on DOM ready without manual setup
14
+ - **JSDoc documentation** - Complete documentation for all JavaScript methods
15
+ - **GitHub Actions CI/CD** - Automated testing and linting on every push
16
+ - **RuboCop configuration** - Standardized Ruby code style guidelines
17
+ - **Comprehensive documentation** - Added CONTRIBUTING.md, MIGRATION.md, and updated README
18
+ - **Browser support matrix** - Clear compatibility information for all major browsers
19
+ - **Enhanced .gitignore** - Improved patterns for Ruby, IDE, and environment files
20
+ - **Security audit in CI** - Bundler Audit runs automatically in CI pipeline
21
+ - **Development dependencies** - RSpec, RuboCop, and Bundler Audit
22
+
23
+ ### 🔄 Changed
24
+
25
+ - **JavaScript rewrite** - Removed jQuery dependency, now uses vanilla JavaScript
26
+ - **Rails support** - Updated minimum Rails version to 6.0
27
+ - **Ruby version requirement** - Updated to Ruby 3.0+
28
+ - **Explicit railties dependency** - Added runtime dependency (>= 6.0, < 9.0)
29
+ - **CI matrix** - Ruby 3.0–3.3 × Rails 6.0–8.0 with GitHub Actions
30
+ - **Gem metadata** - Added source code, documentation, changelog URIs and MFA requirement
31
+ - **Export button styling** - More flexible and modern styling options
32
+ - **Helper method** - Enhanced `table_export_tag` with better defaults
33
+
34
+ ### ⚠️ Breaking Changes
35
+
36
+ - **jQuery no longer required** - The gem no longer depends on jQuery
37
+ - **Minimum Ruby version** - Now requires Ruby 3.0 or higher (was 1.9.3)
38
+ - **Minimum Rails version** - Now requires Rails 6.0 or higher
39
+ - **JavaScript initialization** - No longer requires manual `//= require jquery.tableToCSV.min`
40
+ - **Button attribute** - Uses `data-export` attribute instead of jQuery plugin
41
+
42
+ See [MIGRATION.md](MIGRATION.md) for detailed upgrade instructions.
43
+
44
+ ### 🐛 Fixed
45
+
46
+ - Improved error handling in CSV export
47
+ - Better handling of special characters in CSV output
48
+ - Fixed memory leaks in event listeners
49
+ - Improved browser compatibility
50
+
51
+ ### 🗑️ Removed
52
+
53
+ - jQuery dependency from JavaScript
54
+ - Legacy jQuery vendor asset (`jquery.tableToCSV.min.js`)
55
+ - Legacy Travis CI configuration
56
+ - Old security badges (replaced with GitHub Actions)
57
+ - Hakiri security badge (now using Bundler Audit)
58
+
59
+ ### 📚 Documentation
60
+
61
+ - Complete README rewrite with modern formatting
62
+ - New CONTRIBUTING.md with development guidelines
63
+ - New MIGRATION.md for 0.x users upgrading to 1.0.0
64
+ - Changelog (this file) added
65
+
66
+ ### 🔒 Security
67
+
68
+ - Bundler Audit integrated into CI pipeline
69
+ - Regular dependency security scanning
70
+ - All dependencies kept up-to-date
71
+
72
+ ---
73
+
74
+ ## [0.1.4] - 2020-XX-XX
75
+
76
+ ### Added
77
+
78
+ - Initial gem release
79
+ - Basic table-to-CSV export functionality
80
+ - Rails helper method (`table_export_tag`)
81
+ - jQuery-based JavaScript implementation
82
+
83
+ ### Features
84
+
85
+ - Export HTML tables to CSV files
86
+ - Customizable export button styling
87
+ - Support for specific table selection by ID
88
+
89
+ [1.0.0]: https://github.com/ethirajsrinivasan/table2csv/compare/v0.1.4...v1.0.0
90
+ [0.1.4]: https://github.com/ethirajsrinivasan/table2csv/releases/tag/v0.1.4
91
+
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,84 @@
1
+ # Contributing to Table2CSV
2
+
3
+ We welcome bug reports, feature requests, and pull requests!
4
+
5
+ ## Code of Conduct
6
+
7
+ This project adheres to the [Contributor Covenant](CODE_OF_CONDUCT.md).
8
+
9
+ ## Reporting Issues
10
+
11
+ Before reporting, check [existing issues](https://github.com/ethirajsrinivasan/table2csv/issues).
12
+
13
+ Include:
14
+ - Clear description
15
+ - Steps to reproduce
16
+ - Expected vs actual behavior
17
+ - Ruby and Rails versions
18
+ - Browser and OS info
19
+
20
+ ## Pull Requests
21
+
22
+ 1. Fork the repo and create a branch: `git checkout -b feature/name`
23
+ 2. Make your changes and follow code style guidelines
24
+ 3. Add/update tests: `bundle exec rake`
25
+ 4. Lint your code: `bundle exec rubocop`
26
+ 5. Run security audit: `bundle exec bundle-audit --update`
27
+ 6. Push and open a PR with clear description
28
+
29
+ ## Development Setup
30
+
31
+ ```bash
32
+ git clone https://github.com/ethirajsrinivasan/table2csv
33
+ cd table2csv
34
+ bundle install
35
+ ```
36
+
37
+ Run tests:
38
+ ```bash
39
+ bundle exec rake
40
+ ```
41
+
42
+ Run linter:
43
+ ```bash
44
+ bundle exec rubocop
45
+ ```
46
+
47
+ ## Code Style
48
+
49
+ **Ruby:**
50
+ - 2-space indentation
51
+ - snake_case for methods/variables
52
+ - PascalCase for classes
53
+ - 120 character line limit
54
+ - Add YARD documentation for public methods
55
+
56
+ **JavaScript:**
57
+ - 2-space indentation
58
+ - camelCase for variables/methods
59
+ - Single quotes
60
+ - Add JSDoc comments for public methods
61
+
62
+ ## Testing
63
+
64
+ - Write tests for all features
65
+ - Update tests when fixing bugs
66
+ - Aim for >80% coverage
67
+ - Run full test suite before submitting PR
68
+
69
+ ## Commit Messages
70
+
71
+ Use format: `type: description`
72
+
73
+ Examples:
74
+ - `feat: add CSV export`
75
+ - `fix: handle special characters`
76
+ - `docs: update README`
77
+ - `test: add edge case tests`
78
+ - `chore: update dependencies`
79
+
80
+ ## Questions?
81
+
82
+ Open an [issue](https://github.com/ethirajsrinivasan/table2csv/issues) or [discussion](https://github.com/ethirajsrinivasan/table2csv/discussions).
83
+
84
+ Thanks for contributing! 🎉
data/Gemfile CHANGED
@@ -1,4 +1,9 @@
1
- source 'https://rubygems.org'
1
+ # frozen_string_literal: true
2
2
 
3
- # Specify your gem's dependencies in table2csv.gemspec
4
- gemspec
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ if (rails_version = ENV.fetch("RAILS_VERSION", nil))
8
+ gem "railties", "~> #{rails_version}.0"
9
+ end
data/MIGRATION.md ADDED
@@ -0,0 +1,198 @@
1
+ # Migration Guide: Table2CSV 0.x to 1.0.0
2
+
3
+ This guide helps you upgrade from Table2CSV **0.1.4** (and earlier 0.x) to **1.0.0**.
4
+
5
+ **1.0.0** is the first modern release on RubyGems. It combines Ruby/Rails modernization with a rewritten vanilla JavaScript exporter.
6
+
7
+ ## Breaking Changes
8
+
9
+ ### 1. jQuery is No Longer Required
10
+
11
+ **0.x (jQuery-based):**
12
+ ```erb
13
+ //= require jquery.tableToCSV.min
14
+ <%= table_export_tag %>
15
+ ```
16
+
17
+ **1.0.0 (vanilla JavaScript):**
18
+ ```erb
19
+ //= require table2csv
20
+ <%= table_export_tag %>
21
+ ```
22
+
23
+ ### 2. Minimum Ruby Version
24
+
25
+ | | 0.x | 1.0.0 |
26
+ |---|-----|-------|
27
+ | Ruby | 1.9.3+ | **3.0+** |
28
+
29
+ Update your `.ruby-version` file and Gemfile if needed.
30
+
31
+ ### 3. Minimum Rails Version
32
+
33
+ | | 0.x | 1.0.0 |
34
+ |---|-----|-------|
35
+ | Rails | 3.0+ | **6.0+** |
36
+
37
+ Check your Gemfile dependencies.
38
+
39
+ ### 4. JavaScript Button Attribute
40
+
41
+ The button uses a `data-export` attribute instead of jQuery plugin binding.
42
+
43
+ **Automatic:** Works out of the box with `table_export_tag` helper.
44
+
45
+ **Manual:** If using custom HTML:
46
+
47
+ ```erb
48
+ <!-- 0.x -->
49
+ <button id="export_table_to_csv" data-id="my-table">Export</button>
50
+
51
+ <!-- 1.0.0 (add data-export) -->
52
+ <button id="export_table_to_csv" data-export="export" data-id="my-table">Export</button>
53
+ ```
54
+
55
+ ## Step-by-Step Upgrade
56
+
57
+ ### Step 1: Update Gemfile
58
+
59
+ ```ruby
60
+ # Old
61
+ gem 'table2csv'
62
+
63
+ # New
64
+ gem 'table2csv', '~> 1.0'
65
+ ```
66
+
67
+ Run:
68
+
69
+ ```bash
70
+ bundle update table2csv
71
+ ```
72
+
73
+ ### Step 2: Update Ruby Version (if needed)
74
+
75
+ In `.ruby-version` or Gemfile:
76
+
77
+ ```ruby
78
+ ruby '3.0.0' # or higher
79
+ ```
80
+
81
+ ### Step 3: Update Rails (if needed)
82
+
83
+ In Gemfile:
84
+
85
+ ```ruby
86
+ gem 'rails', '~> 6.0' # or higher
87
+ ```
88
+
89
+ ### Step 4: Remove jQuery Requirements
90
+
91
+ If you were explicitly requiring the old asset:
92
+
93
+ **Before (`application.js` or similar):**
94
+ ```javascript
95
+ //= require jquery.tableToCSV.min
96
+ ```
97
+
98
+ **After:**
99
+ ```javascript
100
+ //= require table2csv
101
+ ```
102
+
103
+ ### Step 5: Test in Development
104
+
105
+ ```bash
106
+ bundle install
107
+ rails server
108
+ ```
109
+
110
+ Visit your page with a table and verify export works.
111
+
112
+ ### Step 6: Deploy
113
+
114
+ Normal deployment process. No special steps needed.
115
+
116
+ ## Option Name Changes
117
+
118
+ All option names remain the same:
119
+
120
+ | Option | 0.x | 1.0.0 | Notes |
121
+ |--------|-----|-------|-------|
122
+ | export_link_text | ✓ | ✓ | No change |
123
+ | width | ✓ | ✓ | No change |
124
+ | height | ✓ | ✓ | No change |
125
+ | color | ✓ | ✓ | No change |
126
+ | bgcolor | ✓ | ✓ | No change |
127
+
128
+ Usage example (unchanged):
129
+
130
+ ```erb
131
+ <%= table_export_tag('my-table', {
132
+ export_link_text: 'Download CSV',
133
+ width: '150px',
134
+ height: '40px',
135
+ color: '#fff',
136
+ bgcolor: '#007bff'
137
+ }) %>
138
+ ```
139
+
140
+ ## Removed Features
141
+
142
+ ### jQuery Plugin
143
+
144
+ The jQuery plugin (`tableToCSV()`) is no longer available. Use the helper instead:
145
+
146
+ ```javascript
147
+ // 0.x - Manual jQuery usage
148
+ $('#my-table').tableToCSV();
149
+
150
+ // 1.0.0 - Use the helper (recommended)
151
+ <%= table_export_tag('my-table') %>
152
+ ```
153
+
154
+ ## New in 1.0.0
155
+
156
+ - `CSVExporter` class (vanilla ES6+)
157
+ - Better error handling
158
+ - No jQuery dependency
159
+ - GitHub Actions CI/CD and Bundler Audit
160
+
161
+ See [CHANGELOG.md](CHANGELOG.md) for full details.
162
+
163
+ ## Troubleshooting
164
+
165
+ ### Export button doesn't work
166
+
167
+ 1. Verify `table2csv.js` is loaded in browser DevTools
168
+ 2. Check browser console for errors
169
+ 3. Ensure table has proper HTML structure
170
+ 4. Verify button ID is `export_table_to_csv`
171
+
172
+ ### Styling issues
173
+
174
+ The button styling is applied via inline `style` from helper options. Example:
175
+
176
+ ```erb
177
+ <%= table_export_tag('my-table', {
178
+ width: '150px',
179
+ height: '40px'
180
+ }) %>
181
+ ```
182
+
183
+ ## Getting Help
184
+
185
+ - [Open an issue](https://github.com/ethirajsrinivasan/table2csv/issues)
186
+ - Check [README.md](README.md) for documentation
187
+ - Review [spec/](spec/) for examples
188
+
189
+ ## Version Comparison
190
+
191
+ | Feature | 0.x | 1.0.0 |
192
+ |---------|-----|-------|
193
+ | jQuery dependency | ✓ | ✗ |
194
+ | ES6+ JavaScript | ✗ | ✓ |
195
+ | Ruby 3.0+ | ✗ | ✓ |
196
+ | Rails 6.0+ | optional | ✓ |
197
+ | GitHub Actions CI | ✗ | ✓ |
198
+ | Security audit (Bundler Audit) | ✗ | ✓ |
data/README.md CHANGED
@@ -1,11 +1,21 @@
1
- # Table2csv
1
+ # Table2CSV
2
2
 
3
- [![Build Status](https://travis-ci.org/ethirajsrinivasan/table2csv.svg?branch=master)](https://travis-ci.org/ethirajsrinivasan/table2csv)
4
- [![Code Climate](https://codeclimate.com/github/ethirajsrinivasan/table2csv/badges/gpa.svg)](https://codeclimate.com/github/ethirajsrinivasan/table2csv)
5
- [![security](https://hakiri.io/github/ethirajsrinivasan/table2csv/master.svg)](https://hakiri.io/github/ethirajsrinivasan/table2csv/master)
3
+ [![CI](https://github.com/ethirajsrinivasan/table2csv/actions/workflows/ci.yml/badge.svg)](https://github.com/ethirajsrinivasan/table2csv/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/table2csv.svg)](https://badge.fury.io/rb/table2csv)
6
5
 
6
+ Export HTML tables to CSV files with ease. A lightweight Rails gem with a view helper and vanilla JavaScript.
7
7
 
8
- Table2csv gem allows you to export a table to csv.
8
+ ## Requirements
9
+
10
+ - Ruby >= 3.0
11
+ - Rails >= 6.0
12
+
13
+ ## Features
14
+
15
+ - Vanilla ES6+ JavaScript with no jQuery dependency
16
+ - `table_export_tag` helper for export buttons
17
+ - Customizable button styling
18
+ - Cross-browser support on modern browsers
9
19
 
10
20
  ## Installation
11
21
 
@@ -17,46 +27,121 @@ gem 'table2csv'
17
27
 
18
28
  And then execute:
19
29
 
20
- $ bundle
30
+ ```bash
31
+ bundle install
32
+ ```
33
+
34
+ Or install it yourself:
35
+
36
+ ```bash
37
+ gem install table2csv
38
+ ```
21
39
 
22
- Or install it yourself as:
40
+ ### Asset Pipeline Setup
23
41
 
24
- $ gem install table2csv
42
+ **For Rails 6.x / 7.x with Sprockets:**
25
43
 
44
+ In your `app/assets/javascripts/application.js`:
26
45
 
27
- To use this gem add this require statement to your application.js file:
46
+ ```javascript
47
+ //= require table2csv
48
+ ```
28
49
 
29
- //= require table2csv
50
+ Optionally precompile in `config/initializers/assets.rb`:
30
51
 
52
+ ```ruby
53
+ Rails.application.config.assets.precompile += %w[table2csv.js]
54
+ ```
31
55
 
56
+ **For Rails 7+ with Import Maps:**
32
57
 
33
- ## Usage
58
+ Add to your `config/importmap.rb`:
34
59
 
35
- Use the table_export_tag helper like any other regular tag helper next to your table:
60
+ ```ruby
61
+ pin "table2csv", to: "table2csv.js"
62
+ ```
36
63
 
37
- <%= table_export_tag %>
64
+ Then import in your `application.js`:
38
65
 
39
- In order to select a particular table use id of the table as follows
66
+ ```javascript
67
+ import "table2csv"
68
+ ```
40
69
 
41
- <%= table_export_tag('id of the table') %>
70
+ **For Rails with Webpacker/Shakapacker:**
42
71
 
43
- Options can be provided as second parameter.Following options are available:
72
+ The gem works with the asset pipeline. If using Webpacker, you may need to configure it to load from the gem's assets directory.
44
73
 
45
- export_link_text: 'Download'
46
- width: '400px'
47
- height: '500px'
48
- color: '#444'
49
- bgcolr: '#000' (background-color)
74
+ ## Quick Start
50
75
 
51
- <%= table_export_tag('name',{export_link_text: 'Download', width: '100px', height: '50px', color: '#E01111', bgcolor: '#444'})%>
76
+ ### Basic Usage
52
77
 
53
- ## Contributing
78
+ ```erb
79
+ <table id="my-table">
80
+ <thead>
81
+ <tr>
82
+ <th>Name</th>
83
+ <th>Email</th>
84
+ </tr>
85
+ </thead>
86
+ <tbody>
87
+ <tr>
88
+ <td>John Doe</td>
89
+ <td>john@example.com</td>
90
+ </tr>
91
+ </tbody>
92
+ </table>
54
93
 
55
- Bug reports and pull requests are welcome on GitHub at https://github.com/ethirajsrinivasan/table2csv. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
94
+ <%= table_export_tag %>
95
+ ```
56
96
 
97
+ ### With Table Selection
57
98
 
58
- ## License
99
+ ```erb
100
+ <%= table_export_tag("my-table") %>
101
+ ```
102
+
103
+ ### With Styling Options
104
+
105
+ ```erb
106
+ <%= table_export_tag("data-table", {
107
+ export_link_text: "Download CSV",
108
+ width: "150px",
109
+ height: "40px",
110
+ color: "#ffffff",
111
+ bgcolor: "#007bff"
112
+ }) %>
113
+ ```
114
+
115
+ ## Available Options
116
+
117
+ | Option | Type | Default | Description |
118
+ |--------|------|---------|-------------|
119
+ | `export_link_text` | String | "Export" | Text displayed on the button |
120
+ | `width` | String | — | CSS width value |
121
+ | `height` | String | — | CSS height value |
122
+ | `color` | String | — | Text color |
123
+ | `bgcolor` | String | — | Background color |
124
+
125
+ ## Upgrading from 0.x to 1.0
126
+
127
+ Version 1.0.0 is the first modern release on RubyGems. It introduces breaking changes to support current Ruby and Rails versions:
59
128
 
60
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
129
+ - **Ruby**: Minimum version increased to 3.0
130
+ - **Rails**: Minimum version increased to 6.0
131
+ - **JavaScript**: jQuery is no longer required
132
+
133
+ See [MIGRATION.md](MIGRATION.md) for detailed upgrade instructions.
134
+
135
+ ## Documentation
136
+
137
+ - [Migration Guide (0.x → 1.0)](MIGRATION.md)
138
+ - [Contributing Guidelines](CONTRIBUTING.md)
139
+ - [Changelog](CHANGELOG.md)
140
+
141
+ ## Contributing
142
+
143
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ethirajsrinivasan/table2csv. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](CODE_OF_CONDUCT.md) code of conduct.
144
+
145
+ ## License
61
146
 
62
- # table2csv
147
+ The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
data/Rakefile CHANGED
@@ -1,4 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require "rspec/core/rake_task"
3
- RSpec::Core::RakeTask.new
4
- task :default => :spec
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -1,11 +1,169 @@
1
- //= require jquery.tableToCSV.min
2
-
3
- $(document).ready(function(){
4
- $(document).on('click', 'button#export_table_to_csv', function() {
5
- var id = $("button#export_table_to_csv").data('id')
6
- if(id != undefined && id!= '')
7
- $("table"+"#"+id).tableToCSV();
8
- else
9
- $("table").tableToCSV();
10
- });
11
- });
1
+ /**
2
+ * Table2CSV - Export HTML tables to CSV format
3
+ * Modern vanilla JavaScript implementation (ES6+)
4
+ * No jQuery required!
5
+ */
6
+
7
+ /**
8
+ * CSVExporter - Handles the conversion of HTML tables to CSV format
9
+ * @class
10
+ */
11
+ class CSVExporter {
12
+ /**
13
+ * Creates a new CSVExporter instance
14
+ * @param {string|null} tableId - The HTML ID of the table to export
15
+ */
16
+ constructor(tableId = null) {
17
+ this.tableId = tableId;
18
+ this.filename = 'export.csv';
19
+ }
20
+
21
+ /**
22
+ * Gets the table element to export
23
+ * @returns {HTMLTableElement|null} The table element or null if not found
24
+ * @private
25
+ */
26
+ getTable() {
27
+ if (this.tableId) {
28
+ return document.getElementById(this.tableId);
29
+ }
30
+ return document.querySelector('table');
31
+ }
32
+
33
+ /**
34
+ * Extracts text content from a cell, handling nested elements
35
+ * @param {HTMLTableCellElement} cell - The table cell element
36
+ * @returns {string} The cell content
37
+ * @private
38
+ */
39
+ getCellContent(cell) {
40
+ return (cell.textContent || cell.innerText || '').trim();
41
+ }
42
+
43
+ /**
44
+ * Escapes special characters in CSV content
45
+ * @param {string} text - The text to escape
46
+ * @returns {string} The escaped text
47
+ * @private
48
+ */
49
+ escapeCSV(text) {
50
+ if (text === null || text === undefined) {
51
+ return '';
52
+ }
53
+
54
+ const str = String(text);
55
+
56
+ // Escape quotes and wrap in quotes if contains comma, newline, or quotes
57
+ if (str.includes(',') || str.includes('\n') || str.includes('"')) {
58
+ return `"${str.replace(/"/g, '""')}"`;
59
+ }
60
+
61
+ return str;
62
+ }
63
+
64
+ /**
65
+ * Converts the table to CSV format
66
+ * @returns {string} CSV formatted data
67
+ * @private
68
+ */
69
+ tableToCSV() {
70
+ const table = this.getTable();
71
+
72
+ if (!table) {
73
+ throw new Error(`Table ${this.tableId ? `with ID "${this.tableId}"` : ''} not found`);
74
+ }
75
+
76
+ const rows = [];
77
+
78
+ // Process all rows (thead, tbody, tfoot)
79
+ const allRows = table.querySelectorAll('tr');
80
+
81
+ allRows.forEach((row) => {
82
+ const cells = row.querySelectorAll('td, th');
83
+ const rowData = Array.from(cells).map(cell => this.escapeCSV(this.getCellContent(cell)));
84
+ rows.push(rowData.join(','));
85
+ });
86
+
87
+ return rows.join('\n');
88
+ }
89
+
90
+ /**
91
+ * Exports the table to CSV and triggers download
92
+ * @returns {void}
93
+ */
94
+ export() {
95
+ try {
96
+ const csv = this.tableToCSV();
97
+ this.downloadCSV(csv);
98
+ } catch (error) {
99
+ console.error('CSV Export Error:', error.message);
100
+ throw error;
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Triggers a CSV file download in the browser
106
+ * @param {string} csv - The CSV content
107
+ * @private
108
+ */
109
+ downloadCSV(csv) {
110
+ const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
111
+ const link = document.createElement('a');
112
+ const url = URL.createObjectURL(blob);
113
+
114
+ link.setAttribute('href', url);
115
+ link.setAttribute('download', this.filename);
116
+ link.style.visibility = 'hidden';
117
+
118
+ document.body.appendChild(link);
119
+ link.click();
120
+ document.body.removeChild(link);
121
+
122
+ // Clean up the URL object
123
+ URL.revokeObjectURL(url);
124
+ }
125
+
126
+ /**
127
+ * Set the filename for the downloaded CSV file
128
+ * @param {string} filename - The desired filename (should include .csv extension)
129
+ * @returns {CSVExporter} Returns this for method chaining
130
+ */
131
+ setFilename(filename) {
132
+ this.filename = filename;
133
+ return this;
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Handle clicks on export buttons (event delegation — works when scripts load in <head>
139
+ * and with Turbo navigation; no dependency on DOMContentLoaded timing).
140
+ */
141
+ function handleTable2CsvExportClick(event) {
142
+ const exportButton = event.target.closest('#export_table_to_csv');
143
+ if (!exportButton) {
144
+ return;
145
+ }
146
+
147
+ event.preventDefault();
148
+ event.stopPropagation();
149
+
150
+ try {
151
+ const tableId = exportButton.getAttribute('data-id') || null;
152
+ const exporter = new CSVExporter(tableId);
153
+
154
+ const filename = exportButton.getAttribute('data-filename');
155
+ if (filename) {
156
+ exporter.setFilename(filename);
157
+ }
158
+
159
+ exporter.export();
160
+ } catch (error) {
161
+ console.error('Failed to export CSV:', error.message);
162
+ alert('Failed to export table to CSV. Please check the browser console for details.');
163
+ }
164
+ }
165
+
166
+ if (!window.__table2csvExportBound) {
167
+ window.__table2csvExportBound = true;
168
+ document.addEventListener('click', handleTable2CsvExportClick);
169
+ }
@@ -1,12 +1,33 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TableToCsvHelper
2
- def table_export_tag(id='',options={})
3
- content_tag(:button,"#{options[:export_link_text] || 'Export'}" ,
4
- id: "export_table_to_csv",
5
- "data-export" =>"export",
6
- "data-id" => id,
7
- :style => ("width:#{options[:width]};
8
- height:#{options[:height]};
9
- color: #{options[:color]};
10
- background-color: #{options[:bgcolor]}"))
4
+ def table_export_tag(id = "", options = {})
5
+ html_options = {
6
+ id: "export_table_to_csv",
7
+ type: "button",
8
+ data: {
9
+ export: "export",
10
+ id: id
11
+ },
12
+ style: export_button_style(options)
13
+ }
14
+ html_options[:class] = options[:class] if options[:class]
15
+
16
+ content_tag(
17
+ :button,
18
+ options[:export_link_text] || "Export",
19
+ html_options
20
+ )
21
+ end
22
+
23
+ private
24
+
25
+ def export_button_style(options)
26
+ [
27
+ ("width:#{options[:width]}" if options[:width]),
28
+ ("height:#{options[:height]}" if options[:height]),
29
+ ("color:#{options[:color]}" if options[:color]),
30
+ ("background-color:#{options[:bgcolor]}" if options[:bgcolor])
31
+ ].compact.join(";")
11
32
  end
12
33
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Table2csv
2
- VERSION = "0.1.4"
4
+ VERSION = "1.0.0"
3
5
  end
data/lib/table2csv.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "table2csv/version"
2
4
 
3
5
  module Table2csv
data/table2csv.gemspec CHANGED
@@ -1,33 +1,43 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'table2csv/version'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/table2csv/version"
5
4
 
6
5
  Gem::Specification.new do |spec|
7
6
  spec.name = "table2csv"
8
7
  spec.version = Table2csv::VERSION
9
- spec.authors = ["ethiraj"]
8
+ spec.authors = ["ethi"]
10
9
  spec.email = ["ethirajsrinivasan@gmail.com"]
11
10
 
12
- spec.summary = "Export a html table into csv file"
13
- spec.description = "Export a html table into csv file"
11
+ spec.summary = "Export a HTML table into CSV file"
12
+ spec.description = "A Rails gem for exporting HTML tables to CSV files with a view helper and vanilla JavaScript"
14
13
  spec.homepage = "https://github.com/ethirajsrinivasan/table2csv"
15
14
  spec.license = "MIT"
16
15
 
17
- # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
- # delete this section to allow pushing this gem to any host.
19
- if spec.respond_to?(:metadata)
20
- spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
- else
22
- raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
16
+ spec.metadata = {
17
+ "allowed_push_host" => "https://rubygems.org",
18
+ "homepage_uri" => spec.homepage,
19
+ "source_code_uri" => "https://github.com/ethirajsrinivasan/table2csv",
20
+ "bug_tracker_uri" => "https://github.com/ethirajsrinivasan/table2csv/issues",
21
+ "changelog_uri" => "https://github.com/ethirajsrinivasan/table2csv/blob/master/CHANGELOG.md",
22
+ "documentation_uri" => "https://github.com/ethirajsrinivasan/table2csv/blob/master/README.md",
23
+ "rubygems_mfa_required" => "true"
24
+ }
25
+
26
+ spec.files = Dir.chdir(__dir__) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
28
  end
24
29
 
25
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
30
  spec.bindir = "exe"
27
31
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
32
  spec.require_paths = ["lib"]
29
- spec.required_ruby_version = '>= 1.9.3'
30
- spec.add_development_dependency "bundler"
31
- spec.add_development_dependency "rake"
32
- spec.add_development_dependency "rspec"
33
+
34
+ spec.required_ruby_version = ">= 3.0"
35
+
36
+ spec.add_runtime_dependency "railties", ">= 6.0", "< 9.0"
37
+
38
+ spec.add_development_dependency "bundler", "~> 2.4"
39
+ spec.add_development_dependency "bundler-audit", "~> 0.9"
40
+ spec.add_development_dependency "rake", "~> 13.0"
41
+ spec.add_development_dependency "rspec", "~> 3.12"
42
+ spec.add_development_dependency "rubocop", "~> 1.50"
33
43
  end
metadata CHANGED
@@ -1,69 +1,122 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table2csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
- - ethiraj
8
- autorequire:
7
+ - ethi
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2016-01-10 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: railties
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '6.0'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '9.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '6.0'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '9.0'
13
32
  - !ruby/object:Gem::Dependency
14
33
  name: bundler
15
34
  requirement: !ruby/object:Gem::Requirement
16
35
  requirements:
17
- - - ! '>='
36
+ - - "~>"
37
+ - !ruby/object:Gem::Version
38
+ version: '2.4'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - "~>"
44
+ - !ruby/object:Gem::Version
45
+ version: '2.4'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler-audit
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
18
51
  - !ruby/object:Gem::Version
19
- version: '0'
52
+ version: '0.9'
20
53
  type: :development
21
54
  prerelease: false
22
55
  version_requirements: !ruby/object:Gem::Requirement
23
56
  requirements:
24
- - - ! '>='
57
+ - - "~>"
25
58
  - !ruby/object:Gem::Version
26
- version: '0'
59
+ version: '0.9'
27
60
  - !ruby/object:Gem::Dependency
28
61
  name: rake
29
62
  requirement: !ruby/object:Gem::Requirement
30
63
  requirements:
31
- - - ! '>='
64
+ - - "~>"
32
65
  - !ruby/object:Gem::Version
33
- version: '0'
66
+ version: '13.0'
34
67
  type: :development
35
68
  prerelease: false
36
69
  version_requirements: !ruby/object:Gem::Requirement
37
70
  requirements:
38
- - - ! '>='
71
+ - - "~>"
39
72
  - !ruby/object:Gem::Version
40
- version: '0'
73
+ version: '13.0'
41
74
  - !ruby/object:Gem::Dependency
42
75
  name: rspec
43
76
  requirement: !ruby/object:Gem::Requirement
44
77
  requirements:
45
- - - ! '>='
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '3.12'
81
+ type: :development
82
+ prerelease: false
83
+ version_requirements: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '3.12'
88
+ - !ruby/object:Gem::Dependency
89
+ name: rubocop
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
46
93
  - !ruby/object:Gem::Version
47
- version: '0'
94
+ version: '1.50'
48
95
  type: :development
49
96
  prerelease: false
50
97
  version_requirements: !ruby/object:Gem::Requirement
51
98
  requirements:
52
- - - ! '>='
99
+ - - "~>"
53
100
  - !ruby/object:Gem::Version
54
- version: '0'
55
- description: Export a html table into csv file
101
+ version: '1.50'
102
+ description: A Rails gem for exporting HTML tables to CSV files with a view helper
103
+ and vanilla JavaScript
56
104
  email:
57
105
  - ethirajsrinivasan@gmail.com
58
106
  executables: []
59
107
  extensions: []
60
108
  extra_rdoc_files: []
61
109
  files:
62
- - .gitignore
63
- - .travis.yml
110
+ - ".github/workflows/ci.yml"
111
+ - ".gitignore"
112
+ - ".rspec"
113
+ - ".rubocop.yml"
114
+ - CHANGELOG.md
64
115
  - CODE_OF_CONDUCT.md
116
+ - CONTRIBUTING.md
65
117
  - Gemfile
66
118
  - LICENSE.txt
119
+ - MIGRATION.md
67
120
  - README.md
68
121
  - Rakefile
69
122
  - app/assets/javascripts/table2csv.js
@@ -73,30 +126,32 @@ files:
73
126
  - lib/table2csv.rb
74
127
  - lib/table2csv/version.rb
75
128
  - table2csv.gemspec
76
- - vendor/assets/javascripts/jquery.tableToCSV.min.js
77
129
  homepage: https://github.com/ethirajsrinivasan/table2csv
78
130
  licenses:
79
131
  - MIT
80
132
  metadata:
81
133
  allowed_push_host: https://rubygems.org
82
- post_install_message:
134
+ homepage_uri: https://github.com/ethirajsrinivasan/table2csv
135
+ source_code_uri: https://github.com/ethirajsrinivasan/table2csv
136
+ bug_tracker_uri: https://github.com/ethirajsrinivasan/table2csv/issues
137
+ changelog_uri: https://github.com/ethirajsrinivasan/table2csv/blob/master/CHANGELOG.md
138
+ documentation_uri: https://github.com/ethirajsrinivasan/table2csv/blob/master/README.md
139
+ rubygems_mfa_required: 'true'
83
140
  rdoc_options: []
84
141
  require_paths:
85
142
  - lib
86
143
  required_ruby_version: !ruby/object:Gem::Requirement
87
144
  requirements:
88
- - - ! '>='
145
+ - - ">="
89
146
  - !ruby/object:Gem::Version
90
- version: 1.9.3
147
+ version: '3.0'
91
148
  required_rubygems_version: !ruby/object:Gem::Requirement
92
149
  requirements:
93
- - - ! '>='
150
+ - - ">="
94
151
  - !ruby/object:Gem::Version
95
152
  version: '0'
96
153
  requirements: []
97
- rubyforge_project:
98
- rubygems_version: 2.2.2
99
- signing_key:
154
+ rubygems_version: 3.6.7
100
155
  specification_version: 4
101
- summary: Export a html table into csv file
156
+ summary: Export a HTML table into CSV file
102
157
  test_files: []
data/.travis.yml DELETED
@@ -1,8 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 1.9.3
4
- - 2.0.0
5
- - 2.1.4
6
- - 2.1.5
7
- - 2.2.1
8
- before_install: gem install bundler -v 1.10.6
@@ -1 +0,0 @@
1
- jQuery.fn.tableToCSV=function(){var t=function(t){return t=t.replace(/"/g,'\\"').replace(/'/g,"\\'"),'"'+t+'"'};$(this).each(function(){var e=($(this),$(this).find("caption").text()),n=[],i=[];$(this).find("tr").each(function(){var e=[];$(this).find("th").each(function(){var e=t($(this).text());n.push(e)}),$(this).find("td").each(function(){var n=t($(this).text());e.push(n)}),e=e.join(","),i.push(e)}),n=n.join(","),i=i.join("\n");var c=n+i,a="data:text/csv;charset=utf-8,"+encodeURIComponent(c),o=document.createElement("a");o.href=a;var h=(new Date).getTime();o.download=""==e?h+".csv":e+"-"+h+".csv",document.body.appendChild(o),o.click(),document.body.removeChild(o)})};