Rails Importer
Rails Importer (XML, XLS, CSV)
How to install
Add it to your Gemfile:
gem 'rails-importer'
Run the following command to install it:
$ bundle install
$ rails generate rails_importer:install
Generators
You can generate importers app/importers/example_importer.rb
$ rails generate rails_importer:importer example
Generator will make a file with content like:
class ExampleImporter < RailsImporter::Base
importer do
fields :name, :email, :age
#or fields({:name => "Name", :email => "E-mail", :age => "Idade"})
each_record do |record, params|
MyModel.find_or_create_by(name: record[:name], email: record[:email], age: record[:age])
return record # or return wherever
end
end
# importer :simple do
# xml_structure :root, :row
# fields :name, :email, :age
# each_record do |record, params|
# ...
# end
# end
end
How to use
You can call import
from Importers objects:
file = params[:import][:file]
records = ExampleImporter.import(file, extra_param: 'Extra')
Or with context:
file = params[:import][:file]
records = ExampleImporter.import(file, context: :simple, extra_param: 'Extra')