[CloudoLife-RoR] col_active_importer_starter: A starter(or wrapper) to active_importer gem

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:

1
gem 'col_active_importer_starter'

And then execute:

1
bundle

Or install it yourself as:

1
gem install col_active_importer_starter

Usages

Suppose there is a ActiveRecord model Article:

1
2
3
class Article < ApplicationRecord

end

and tabular data file data/Articles.xlsx

Title Body
Article.1.title Article.1.body
Article.2.title Article.2.body
  1. Create a ArticleImporter extend ColActiveImporterStarter::BaseImporter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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.
1
ArticleImporter.execute("#{Rails.root}/data/Articles.1.xlsx")

Or specify more arguments.

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

ArticleImporter.import(file, params: params)
  1. Then, check tmp/importers directory to find the result file.
Title Body Result ID Result Message
Article.1.title Article.1.body 1 success
Article.2.title Article.2.body 2 success

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-RoR/col_active_importer_starter: col_active_importer_starter is a starter(or wrapper) to active_importer. - https://github.com/CloudoLife-RoR/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] RubyGems.org | your community gem host - https://rubygems.org/