CSVManager
This gem helps import/export a CSV file in a very easy and simple way.
# Import a CSV file and parse the file
import = CSVManager::Import.new
import.parse('tmp/default.csv')
import.create(Product)
# Export a CSV file and download it
export = CSVManager::Export.new
export.download(products_controller, Product.all, "my_file.csv")
Installation
Add this line to Gemfile, then execute $ bundle
.
gem 'csv_manager'
Or install it yourself as:
$ gem install csv_manager
Usage
How to Import
First instantiate CSVManager::Import
class.
@import = CSVManager::Import.new
Import a CSV file and parse
the file. Then @import
can call cols
, rows
, entire
, count
and get_contents
.
@import.parse('tmp/default.csv')
@import.cols #=> ["name", "category", "quantity"]
@import.rows #=> [["eraser", "stationery", 100], ["spoon", "kitchen", 110]]
@import.entire #=> [{"name"=>"eraser", "category"=>"stationery", "quantity"=>100}, {"name"=>"spoon", "category"=>"kitchen", "quantity"=>110}]
@import.count #=> 2
@import.get_contents("category") #=> ["stationery", "kitchen"]
Customized headers can be sent as a second argument to parse
.
@import.parse('tmp/default.csv', ["Name", "Type", "Count"])
Populate database with data from @import
@import.create(Product)
#<Product id: 27, name: "eraser", category: "stationery", quantity: 100, created_at: "2017-01-06 17:14:09", updated_at: "2017-01-06 17:14:09">
#<Product id: 28, name: "spoon", category: "kitchen", quantity: 110, created_at: "2017-01-06 17:14:09", updated_at: "2017-01-06 17:14:09">CSV
- Example 1) Choose a CSV file and parse
- Example 2) Create custom headers
- Example 3) Specify a CSV file path
- Example 4) Populate Database
How to Export
First instantiate CSVManager::Export
class.
@export = CSVManager::Export.new
Create a CSV file with the given model.
@export.download(products_controller, Product.all, "my_file.csv")
In the view, make sure to send format: "csv"
to the path.
<%= link_to 'Download CSV', download_products_path(format: "csv") %>
Demo App
To see the CSVManager
in action you can take a look at this app - Inventory.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/ghbooth12/csv_manager. 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.
License
The gem is available as open source under the terms of the MIT License.