Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

col_active_importer_starter

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

col_active_importer_starter

  • 0.1.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

col_active_importer_starter

col_active_importer_starter is a starter(or wrapper) to active_importer - https://github.com/continuum/active_importer gem.

Features

col_active_importer_starter makes full use of active_importer gem to import tabular data from spreadsheets or similar sources into Active Record models.

Installation

Add this line to your application's Gemfile:

gem 'col_active_importer_starter'

And then execute:

bundle

Or install it yourself as:

gem install col_active_importer_starter

Usages

Suppose there is a ActiveRecord model Article:

class Article < ApplicationRecord

end

and tabular data file data/Articles.xlsx

TitleBody
Article.1.titleArticle.1.body
Article.2.titleArticle.2.body
  1. Create a ArticleImporter extend ColActiveImporterStarter::BaseImporter
# app/importers/article_import.rb

class ArticleImporter < ColActiveImporterStarter::BaseImporter
  imports Article

  transactional
  module ColumnName
    Title = "Title"
    Body = "Body"
  end

  column ColumnName::Title, :title
  column ColumnName::Body, :body

  def handle_fetch_model
    params = {
      title: row[ColumnName::Title],
    }

    model = Article.find_or_initialize_by(params)

    model
  end

  fetch_model do
    handle_fetch_model
  end

  def handle_skip_rows_if?
    row[ColumnName::Title].blank?
  end

  skip_rows_if do
    handle_skip_rows_if?
  end

  # ArticleImporter.execute
  def self.execute(file = "#{Rails.root}/data/Articles.1.xlsx")
    params = {
      cache: {},
      file: file,
      result_index: 10,
    }
    import(file, params: params)
  end

  private
end
  1. Import data from a file.
ArticleImporter.import("#{Rails.root}/data/Articles.1.xlsx")

# Or use ArticleImporter instance.
# importer = ArticleImporter.new(file, {params: params(file)})
# importer.import
# puts importer.row_count

Or specify more arguments.

params = {
  cache: {},
  file: "#{Rails.root}/data/Articles.1.xlsx",
  result_index: 3,
}

ArticleImporter.import(file, {params: params})

# Or use ArticleImporter instance.
# importer = ArticleImporter.new(file, {params: params(file)})
# importer.import
# puts importer.row_count

For more examples to see ./test/dummy/test/importers/article_importer_test.rb.

  1. Then, check tmp/importers directory to find the result file.
TitleBodyResult IDResult Message
Article.1.titleArticle.1.body1success
Article.2.titleArticle.2.body2success

Testing

Run rails test to execute all test cases in test/dummy/test directory.

rails test test/dummy/test

Inspire

Inspire by active_importer - https://github.com/continuum/active_importer.

Contributing

Contributions are welcome! Take a look at our contributions guide for details. The basic workflow for contributing is the following:

License

The gem is available as open source under the terms of the MIT License - https://opensource.org/licenses/MIT.

References

[1] [CloudoLife-Rails/col_active_importer_starter: col_active_importer_starter is a starter(or wrapper) to active_importer. - https://github.com/CloudoLife-Rails/col_active_importer_starter

[2] col_active_importer_starter | RubyGems.org | your community gem host - https://rubygems.org/gems/col_active_importer_starter

[3] continuum/active_importer: Define importers that load tabular data from spreadsheets or CSV files into any ActiveRecord-like ORM. - https://github.com/continuum/active_importer

[4] weshatheleopard/rubyXL: Ruby lib for reading/writing/modifying .xlsx and .xlsm files - https://github.com/weshatheleopard/rubyXL

[5] rubyXL | RubyGems.org | your community gem host - https://rubygems.org/gems/rubyXL/

[6] RubyGems.org | your community gem host - https://rubygems.org/

FAQs

Package last updated on 12 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc