Module: ActiveRecord::SaveMany::ClassMethods

Included in:
Base
Defined in:
lib/activerecord_save_many/save_many.rb

Instance Method Summary collapse

Instance Method Details

#save_many(values, options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/activerecord_save_many/save_many.rb', line 76

def save_many(values, options={})
  Functions::check_options(ActiveRecord::SaveMany::OPTIONS_KEYS , options)
  return if values.nil? || values.empty?

  columns, values = Functions::add_columns(self, values, options)

  # if more than max_rows, execute multiple sql statements
  max_rows = options[:max_rows] || save_many_max_rows
  batches = Functions::slice_array(max_rows, values)

  timestamp = Time.now
  batches.each do |rows|
    rows = rows.map do |obj|
      if obj.is_a? ActiveRecord::Base
        obj.send( :callback, :before_save )
        if obj.id
          obj.send( :callback, :before_update)
          set_update_timestamps(timestamp, obj)
        else
          obj.send( :callback, :before_create )
          set_create_timestamps(timestamp, obj)
        end
        raise "#{obj.errors.full_messages.join(', ')}" if !obj.valid?
      end
      columns.map{|col| obj[col]}
    end

    sql = connection.save_many_sql(self,
                                   table_name, 
                                   columns, 
                                   rows, 
                                   { :replace=>options[:replace],
                                     :ignore=>options[:ignore],
                                     :async=>options[:async] && !Functions::disable_async?(),
                                     :update=>options[:update] || options[:updates],
                                     :updates=>options[:updates] || {} })
    
    connection.execute_raw sql
  end
end

#save_many_max_rowsObject



56
57
58
# File 'lib/activerecord_save_many/save_many.rb', line 56

def save_many_max_rows
  @save_many_max_rows || ActiveRecord::SaveMany::default_max_rows
end

#save_many_max_rows=(max_rows) ⇒ Object



52
53
54
# File 'lib/activerecord_save_many/save_many.rb', line 52

def save_many_max_rows=(max_rows)
  @save_many_max_rows=max_rows
end

#set_create_timestamps(timestamp, instance) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/activerecord_save_many/save_many.rb', line 60

def set_create_timestamps(timestamp, instance)
  if instance.record_timestamps
    instance.write_attribute('created_at', timestamp) if instance.respond_to?(:created_at) && instance.created_at.nil?
    instance.write_attribute('created_on', timestamp) if instance.respond_to?(:created_on) && instance.created_on.nil?
    instance.write_attribute('updated_at', timestamp) if instance.respond_to?(:updated_at) && instance.updated_at.nil?
    instance.write_attribute('updated_on', timestamp) if instance.respond_to?(:updated_on) && instance.updated_on.nil?
  end
end

#set_update_timestamps(timestamp, instance) ⇒ Object



69
70
71
72
73
74
# File 'lib/activerecord_save_many/save_many.rb', line 69

def set_update_timestamps(timestamp, instance)
  if instance.record_timestamps
    instance.write_attribute('updated_at', timestamp) if instance.respond_to?(:updated_at)
    instance.write_attribute('updated_on', timestamp) if instance.respond_to?(:updated_on)
  end
end