From e8bcd38ea2856c53e7f73092125d362c9ba0d6c0 Mon Sep 17 00:00:00 2001 From: David Reese Date: Thu, 12 Sep 2024 14:20:10 -0400 Subject: [PATCH 1/2] Add default_values configuration --- README.md | 9 +++++++++ lib/csv_importer.rb | 3 ++- lib/csv_importer/config.rb | 1 + lib/csv_importer/row.rb | 3 ++- spec/csv_importer_spec.rb | 12 ++++++++++++ 5 files changed, 26 insertions(+), 2 deletions(-) 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..2112c9c 100644 --- a/lib/csv_importer.rb +++ b/lib/csv_importer.rb @@ -71,7 +71,8 @@ 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, + default_values: config.default_values) end end diff --git a/lib/csv_importer/config.rb b/lib/csv_importer/config.rb index 1100b7a..c121997 100644 --- a/lib/csv_importer/config.rb +++ b/lib/csv_importer/config.rb @@ -9,6 +9,7 @@ class Config attribute :when_invalid, Symbol, default: proc { :skip } attribute :after_build_blocks, Array[Proc], default: [] attribute :after_save_blocks, Array[Proc], default: [] + attribute :default_values, Hash, default: {} def initialize_copy(orig) super diff --git a/lib/csv_importer/row.rb b/lib/csv_importer/row.rb index eb9021c..46a8112 100644 --- a/lib/csv_importer/row.rb +++ b/lib/csv_importer/row.rb @@ -12,6 +12,7 @@ class Row attribute :model_klass attribute :identifiers # Array[Symbol] or Proc attribute :after_build_blocks, Array[Proc], default: [] + attribute :default_values, Hash, default: {} attribute :skip, Virtus::Attribute::Boolean, default: false # The model to be persisted @@ -107,7 +108,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 From 2cdf3d188a1d1c3101550ba400e7d6749f146251 Mon Sep 17 00:00:00 2001 From: David Reese Date: Tue, 24 Sep 2024 15:19:54 -0400 Subject: [PATCH 2/2] Add after_set_attributes blocks --- lib/csv_importer.rb | 1 + lib/csv_importer/config.rb | 6 ++++++ lib/csv_importer/dsl.rb | 4 ++++ lib/csv_importer/row.rb | 4 ++++ 4 files changed, 15 insertions(+) diff --git a/lib/csv_importer.rb b/lib/csv_importer.rb index 2112c9c..cf2c46f 100644 --- a/lib/csv_importer.rb +++ b/lib/csv_importer.rb @@ -72,6 +72,7 @@ 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, + 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 c121997..c357ded 100644 --- a/lib/csv_importer/config.rb +++ b/lib/csv_importer/config.rb @@ -8,6 +8,7 @@ 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: {} @@ -17,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 46a8112..817ed23 100644 --- a/lib/csv_importer/row.rb +++ b/lib/csv_importer/row.rb @@ -12,6 +12,7 @@ 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 @@ -22,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 @@ -98,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?