Git Product home page Git Product logo

row_boat's Introduction

RowBoat

Created by     DevMynd Logo

Gem Version    Build Status

A simple gem to help you import CSVs into your ActiveRecord models.

Check out the documentation!

It uses SmarterCSV and activerecord-import to import database records from your CSVs.

Contents

Installation

Add this line to your application's Gemfile:

gem "row_boat", "~> 0.4"

And then execute:

$ bundle

Basic Usage

Below we're defining the required methods (import_into and column_mapping) and a few additional options as well (via value_converters and options). Checkout API.md for the full documentation for more details :)

class ImportProduct < RowBoat::Base
  # required
  def import_into
    Product # The ActiveRecord class we want to import records into.
  end

  # required
  def column_mapping
    {
      # `:prdct_name` is the downcased and symbolized version
      # of our column header, while `:name` is the attribute
      # of our model we want to receive `:prdct_name`'s value
      prdct_name: :name,
      dllr_amnt: :price_in_cents,
      desc: :description
    }
  end

  # optional
  def value_converters
    {
      # Allows us to change values we want to import
      # before we import them
      price_in_cents: -> (value) { value * 1000 }
    }
  end

  # optional
  def preprocess_row(row)
    if row[:name] && row[:description] && row[:price]
      row
    else
      nil # return nil to skip a row
    end
    # we could also remove some attributes or do any
    # other kind of work we want with the given row.
  end

  #optional
  def options
    {
      # These are additional configurations that
      # are generally passed through to SmarterCSV
      # and activerecord-import
      validate: false, # this defaults to `true`
      wrap_in_transaction: false # this defaults to `true`
    }
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake to run the tests (run appraisal install and appraisal rake to run the tests against different combinations of dependencies). You can also run bin/console for an interactive prompt that will allow you to experiment.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/devmynd/row_boat. 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.

row_boat's People

Stargazers

Abel Sen avatar Alexander Tsirel avatar Joel Cahalan avatar Emile Bosch avatar Fabian Mersch avatar Nick Gorbikoff avatar Chris Cortez avatar Eryan Cobham avatar Sasha avatar Matt DuBois avatar ty avatar Joseph Hirn avatar Michael Crismali avatar

Watchers

Sarah Mei avatar JC Grubbs avatar James Cloos avatar Alex Spencer avatar Michael Crismali avatar Allen avatar Sasha avatar  avatar Matt DuBois avatar Eileen Duffner avatar Kitto Khrangtong avatar  avatar Dominic avatar Mina Slater avatar  avatar  avatar

Forkers

noma4i packfiles

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.