Sha256: d3b97ce0e0a58f579bb79d8aebb4c3fdd20cabc84f0a0193e8f77b3cf24bfc74

Contents?: true

Size: 794 Bytes

Versions: 5

Compression:

Stored size: 794 Bytes

Contents

module Copyable
  class Saver

    def self.direct_sql_insert!(new_model)
      new_model.send(:_create_record) # bypass all callbacks and validations
    end

    def self.direct_sql_update!(new_model)
      new_model.send(:_update_record) # bypass all callbacks and validations
    end

    # this is the algorithm for saving the new record
    def self.save!(new_model, skip_validations=false)
      unless skip_validations || new_model.valid?(:create)
        raise(ActiveRecord::RecordInvalid.new(new_model))
      end

      if new_model.class.all_callbacks_disabled && new_model.id.nil?
        self.direct_sql_insert!(new_model)
      elsif new_model.class.all_callbacks_disabled
        self.direct_sql_update!(new_model)
      else
        new_model.save!
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
copyable-0.3.5 lib/copyable/saver.rb
copyable-0.3.3 lib/copyable/saver.rb
copyable-0.3.2 lib/copyable/saver.rb
copyable-0.3.1 lib/copyable/saver.rb
copyable-0.3.0 lib/copyable/saver.rb