Export ActiveModels to CSV by declaring headers and fields in your model with the exportable
class method and block.
Add this line to your application's Gemfile:
gem 'easy_export'
And then execute:
$ bundle
Or install it yourself as:
$ gem install easy_export
In a model, add the exportable
declaration:
class User < ActiveRecord::Base
exportable do
scope -> { User.all }
fields [
['Header 1', -> { value to return }],
['Header 2', :method_name],
['Header 3', 'static value'], ...
]
end
end
Now, use the Exporter
to get the CSV data:
exporter = Exportable::Exporter.new params[:export]
exporter.data # The CSV data
exporter.file_type # 'text/csv'
exporter.file_name # 'users-<timestamp>.csv'
With those you can initiate a download from a controller:
send_data exporter.data, {
filetype: exporter.file_type,
filename: exporter.file_name,
disposition: 'attachment'
}
- Support more export types: XML, JSON, YML
- Add method to write the data to a file
- Clean up the way
fields
are declared.
- Fork it ( https://github.com/[my-github-username]/easy_export/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request