From 49410bb55afbb46ef5e48f73fb564e2664115354 Mon Sep 17 00:00:00 2001 From: Alexis Reigel <mail@koffeinfrei.org> Date: Fri, 1 Jun 2018 10:44:24 +0200 Subject: [PATCH] document transaction support --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/README.md b/README.md index 2f7602b..15713b9 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,47 @@ import.total # => 3 ``` +### Transactions + +When setting `transaction true` no record is inserted if any one of them has an error. + +```ruby +class UserImporter < Excelsior::Importer + # declare the source file + source "static/ftp/users.xlsx" + + # only insert all rows if none of them have an error + transaction true + + # declare the mapping + map "First Name", to: :firstname + map "Last Name", to: :lastname + map "E-Mail", to: :email +end +``` + +If a block is passed to `run` the block needs to raise an error in order to +roll back the transaction. + +This means that the following will trigger a rollback if the model is not +valid: + +```ruby +UserImport.new.run do |row| + User.create!(row) +end +``` + +On the other hand, the following won't trigger a rollback if the model is +invalid: + +```ruby +UserImport.new.run do |row| + User.create(row) +end +``` + + ### Extended API You may want to pass an excel file per instance. You can also define your own -- GitLab