Class: Copyable::Saver

Inherits:
Object
  • Object
show all
Defined in:
lib/copyable/saver.rb

Class Method Summary collapse

Class Method Details

.direct_sql_insert!(new_model) ⇒ Object



4
5
6
# File 'lib/copyable/saver.rb', line 4

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

.direct_sql_update!(new_model) ⇒ Object



8
9
10
# File 'lib/copyable/saver.rb', line 8

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

.save!(new_model, skip_validations = false) ⇒ Object

this is the algorithm for saving the new record



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/copyable/saver.rb', line 13

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