Coder Social home page Coder Social logo

beam's Introduction

Beam

Gem Version Build Status Coverage Status

A rubygem to simplifiy repetitive csv upload process for ActiveRecord models in rails applications. Supports bulk upload with activerecord-import

Usage

  1. 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
  2. Add it to the model you want to import csv file

    extend Beam::Upload
  3. 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]
  4. 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 %>
  5. 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

Configuration options:

  1. 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"

TO DO

SideKiq & DelayedJob options

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.