Beam
A rubygem to simplifiy repetitive csv upload process for ActiveRecord models in rails applications. Supports bulk upload with activerecord-import
Usage
Add it the application's Gemfile:
gem 'beam'Run the generator
rails g beam:installThis will create a config file for you to over-ride default options of upload process.
create config/initializers/beam.rbAdd activerecord-import gem to the application's Gemfile:
gem 'activerecord-import', '0.4.1' # for rails-4.1 app gem 'activerecord-import', '0.4.0' # for rails-4.0 app gem 'activerecord-import', '0.3.1' # for rails-3.1+ appAdd it to the model you want to import csv file
extend Beam::UploadUpload zipped csv file, e.g. users.csv.zip
Model.upload_file(file_name, file_path) # where users.csv has headers and rows, e.g.: # name,email # Test1, # Test2,[email protected] # Test3,[email protected] # Test4,[email protected]Get the output as:
# response hash, e.g. {:errors=>1, :status=>200, :total_rows=>4, :error_rows=>[["Test1", nil, "is invalid"]]} # error file, e.g. for users.csv file, it creates errors_users.csv at the same path # see records being saved in batch(by default) of 1_000 with activerecord-import gem
See beam/upload.rb for more details
TO DO
SPECS!
Contributing
- Fork it
- 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 new Pull Request