Class: CsvPiper::Processors::CreateActiveModel

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_piper/processors/create_active_model.rb

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ CreateActiveModel

Returns a new instance of CreateActiveModel.



4
5
6
# File 'lib/csv_piper/processors/create_active_model.rb', line 4

def initialize(model_class)
  @model_class = model_class
end

Instance Method Details

#process(_source, transformed, errors) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/csv_piper/processors/create_active_model.rb', line 8

def process(_source, transformed, errors)
  model = @model_class.new(transformed)

  model.save if model.valid? && errors.empty?

  errors.errors.merge!(model.errors.to_hash) do |_key, old_val, new_val|
    old_val + new_val
  end

  transformed = transformed.merge({ "#{@model_class.name.underscore}_model".to_sym => model })
  [transformed, errors]
end