A rubygem to simplifiy repetitive csv upload process for ActiveRecord models in rails applications. Supports bulk upload with activerecord-import
-
Add it the application's Gemfile:
gem 'beam'
Run the generator
rails g beam:install
This will create a config file for you to over-ride default options of upload process.
create config/initializers/beam.rb
Add 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+ app
-
Add it to the model you want to import csv file
extend Beam::Upload
-
Upload 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]
-
Only if you would like to use upload_controller_methods (to help you upload files zipped-csv files, as fake.csv.zip) include below routes in config/routes.rb (for fake_controller):
post "fake/upload", to: "fake#upload" get "fake/error_file", to: "fake#error_file"
include these methods in the controller:
class FakeController < ApplicationController include Beam::UploadControllerMethods end ... ...
and add view snippet to app/views/fake/upload_form.html.erb
<%= form_tag users_upload_path, :multipart => true do %> <%= file_field_tag 'upload[upload_file]'%> <%= submit_tag "Upload" %> <% end %>
-
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 specified in Beam.config (Rails.root+'/tmp') # see records being saved in batch(by default) of 1_000 with activerecord-import gem
-
Default configurations (to change these, update config/initializers/beam.rb)
Beam.config[:error_file_needed] = true Beam.config[:batch_process] = true Beam.config[:batch_size] = 1_000 Beam.config[:zipped] = true Beam.config[:data_upload_path] = "#{Rails.root}/tmp"
SideKiq & DelayedJob options
- 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