Sha256: 6e8181c721d0bbd1286f6f5308729a5d7c6a9083f31678b6ebf58293843e7205

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

require 'active_record'
require 'pg'

module Dino
  # Provides a simple way to do an create or update of an ActiveRecord object.
  module Upsert
    extend ActiveSupport::Concern
    # User.upsert
    module ClassMethods
      def upsert(*args, &block)
        Dino::Upsert.upsert(self, *args, &block)
      end
    end

    # klass: The ActiveRecord class we are upserting against.
    # conditions: what should match the upsert
    # options: what we are updating/inserting
    # If provided, the block gets the object before it's saved, in case
    #   there's special init options necessary for it.
    # rubocop:disable MethodLength
    def self.upsert(klass, data, options = {}, &block)
      retry_count = 0
      data_copy = {}
      data ||= []
      data.each do |k, v|
        if v.kind_of?(Hash)
          v = klass.column_for_attribute(k).type_cast_for_database(v)
        end
        data_copy[k] = v
      end
      begin
        klass.transaction(requires_new: true) do
          object = klass.where(data_copy).first_or_initialize
          block.call(object) if block
          object.tap do |t|
            t.assign_attributes(options)
            t.save! if t.changed?
          end
        end
      rescue PG::UniqueViolation, ActiveRecord::RecordNotUnique
        # If there's a unique violation, retry this. But only a certain amount
        # of times or we'll get into an infinite loop if something's messed up.
        # (like an incorrect unique index or something)
        if retry_count < 10
          retry_count += 1
          retry
        else
          raise
        end
      end
    end
  end
end

ActiveRecord::Base.send(:include, Dino::Upsert)

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dino_utils-0.1.12 lib/dino/upsert.rb
dino_utils-0.1.11 lib/dino/upsert.rb
dino_utils-0.1.10 lib/dino/upsert.rb
dino_utils-0.1.9 lib/dino/upsert.rb