diff --git a/README.md b/README.md index 9e9937c..d8efc07 100644 --- a/README.md +++ b/README.md @@ -351,6 +351,15 @@ UserImport.new(file: my_file) do end ``` + +### Set default values + +You can set default values for every model record with the `default_values` option: + +```ruby +UserImport.new(file: csv_file, default_values: {batch: '2024a'}) +``` + ### Skip import You can skip the import of a model by calling `skip!` in an diff --git a/lib/csv_importer.rb b/lib/csv_importer.rb index 3ccff5d..cf2c46f 100644 --- a/lib/csv_importer.rb +++ b/lib/csv_importer.rb @@ -71,7 +71,9 @@ def header def rows csv.rows.map.with_index(2) do |row_array, line_number| Row.new(header: header, line_number: line_number, row_array: row_array, model_klass: config.model, - identifiers: config.identifiers, after_build_blocks: config.after_build_blocks) + identifiers: config.identifiers, after_build_blocks: config.after_build_blocks, + after_set_attributes_blocks: config.after_set_attributes_blocks, + default_values: config.default_values) end end diff --git a/lib/csv_importer/config.rb b/lib/csv_importer/config.rb index 1100b7a..c357ded 100644 --- a/lib/csv_importer/config.rb +++ b/lib/csv_importer/config.rb @@ -8,7 +8,9 @@ class Config attribute :identifiers # Array[Symbol] or Proc attribute :when_invalid, Symbol, default: proc { :skip } attribute :after_build_blocks, Array[Proc], default: [] + attribute :after_set_attributes_blocks, Array[Proc], default: [] attribute :after_save_blocks, Array[Proc], default: [] + attribute :default_values, Hash, default: {} def initialize_copy(orig) super @@ -16,12 +18,17 @@ def initialize_copy(orig) self.identifiers = orig.identifiers.dup self.after_save_blocks = orig.after_save_blocks.dup self.after_build_blocks = orig.after_build_blocks.dup + self.after_set_attributes_blocks = orig.after_set_attributes_blocks.dup end def after_build(block) self.after_build_blocks << block end + def after_set_attributes(block) + self.after_set_attributes_blocks << block + end + def after_save(block) self.after_save_blocks << block end diff --git a/lib/csv_importer/dsl.rb b/lib/csv_importer/dsl.rb index 2ca6e72..ae9740f 100644 --- a/lib/csv_importer/dsl.rb +++ b/lib/csv_importer/dsl.rb @@ -24,6 +24,10 @@ def after_build(&block) config.after_build(block) end + def after_set_attributes(&block) + config.after_set_attributes(block) + end + def after_save(&block) config.after_save(block) end diff --git a/lib/csv_importer/row.rb b/lib/csv_importer/row.rb index eb9021c..817ed23 100644 --- a/lib/csv_importer/row.rb +++ b/lib/csv_importer/row.rb @@ -12,6 +12,8 @@ class Row attribute :model_klass attribute :identifiers # Array[Symbol] or Proc attribute :after_build_blocks, Array[Proc], default: [] + attribute :after_set_attributes_blocks, Array[Proc], default: [] + attribute :default_values, Hash, default: {} attribute :skip, Virtus::Attribute::Boolean, default: false # The model to be persisted @@ -21,6 +23,7 @@ def model set_attributes(model) + after_set_attributes_blocks.each { |block| instance_exec(model, &block) } after_build_blocks.each { |block| instance_exec(model, &block) } model end @@ -97,6 +100,8 @@ def find_model model = build_model set_attributes(model) + after_set_attributes_blocks.each { |block| instance_exec(model, &block) } + identifiers = model_identifiers(model) return nil if identifiers.empty? @@ -107,7 +112,7 @@ def find_model end def build_model - model_klass.new + model_klass.new(default_values) end def skip! diff --git a/spec/csv_importer_spec.rb b/spec/csv_importer_spec.rb index e2b610d..cf90072 100644 --- a/spec/csv_importer_spec.rb +++ b/spec/csv_importer_spec.rb @@ -470,6 +470,18 @@ class ImportUserCSVByFirstName }.to_not raise_error(NoMethodError, "undefined method `downcase' for nil:NilClass") end + it "sets default attributes" do + csv_content = "Email,Confirmed,First name,last_name + bob@example.com , true, bob ,," + default_values = {created_by_user_id: 123} + import = ImportUserCSV.new(content: csv_content, default_values: default_values) + + import.run! + + model = import.report.created_rows.first.model + expect(model).to have_attributes({created_by_user_id: 123}) + end + describe "#when_invalid" do it "could abort" do csv_content = "email,confirmed,first_name,last_name