https://github.com/madebylotus/xporter
ruby gem for DSL creating streaming CSV exports
https://github.com/madebylotus/xporter
Last synced: 7 months ago
JSON representation
ruby gem for DSL creating streaming CSV exports
- Host: GitHub
- URL: https://github.com/madebylotus/xporter
- Owner: madebylotus
- License: mit
- Created: 2018-12-18T01:02:54.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-20T20:28:56.000Z (over 2 years ago)
- Last Synced: 2024-10-29T01:11:30.359Z (8 months ago)
- Language: Ruby
- Size: 30.3 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Xporter
ruby gem for DSL creating streaming CSV exports.[ ](https://app.codeship.com/projects/319101)
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'xporter'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install xporter
## Usage
In most cases, you'll use this library in a rails app, to organize different exporters and optionally use Live Streaming from the controller to browser to begin streaming results straight from the database to the client.### Define Exporter
To define an exporter, simply place a file in the `app/exporters` directory in your rails app (restarting app server if creating directory for the first time).```ruby
# app/exporters/admin_exporter.rb
class AdminExporter < Xporter::Exporter
model User
decorates AdminDecoratorcolumn(:name)
column(:email)
column(:dynamic) { |record| record.object_id }
end
```### Generate CSV String
With an exporter defined, you'll want to convert a collection of objects into a CSV string.```ruby
exporter = AdminExporter.new
exporter.generate(User.all)
# => "Name,Email,Dynamic\nJustin,[email protected],12345511\n"
```If provided an `ActiveRecord::Relation`, we'll fetch records lazily in batches from ActiveRecord.
### Stream CSV to Browser
With an exporter defined, you'll want to convert a collection of objects and stream to the browser immediately.```ruby
class AdministratorsController < ApplicationController
include Xporter::FileStreamerdef index
@users = User.allrespond_to do |format|
format.html
format.csv do
stream_file "administrators-export-#{ SecureRandom.uuid }".parameterize, 'csv' do |stream|
AdminExporter.stream(@users, stream)
end
end
end
end
```## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/xporter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the Xporter project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/madebylotus/xporter/blob/master/CODE_OF_CONDUCT.md).