Sha256: a45ecbfaffc715d5c9f94f5950fffb33977aa1b16276644ef86fbd37e307d4f5

Contents?: true

Size: 546 Bytes

Versions: 1

Compression:

Stored size: 546 Bytes

Contents

module CsvPiper
  module Processors
    class CreateActiveModel
      def initialize(model_class)
        @model_class = model_class
      end

      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["#{@model_class.name.underscore}_model".to_sym] = model
        [transformed, errors]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csv_piper-0.1.8 lib/csv_piper/processors/create_active_model.rb