Sha256: 7d773d0968df4c0c69ef4ed19c89865ae48f2e9817ad8b1d56f3fdc52c853441

Contents?: true

Size: 920 Bytes

Versions: 1

Compression:

Stored size: 920 Bytes

Contents

class Array
  def to_insert_sql(batch_size=500)
    raise 'All element should be an ActiveRecord instance object' unless all? { |e| e.is_a?(ActiveRecord::Base) }
    group_by(&:class).map do |(klass, records)|
      klass.to_insert_sql(records, batch_size)
    end.join("\n")
  end

  def to_upsert_sql(batch_size=500)
    raise 'All element should be an ActiveRecord instance object' unless all? { |e| e.is_a?(ActiveRecord::Base) }
    group_by(&:class).map do |(klass, records)|
      klass.to_upsert_sql(records, batch_size)
    end.join("\n")
  end

  def v
    raise 'Empty array' unless present?
    raise 'All elements must be instances of the same ActiveRecord model class' unless map(&:class).uniq.size == 1 && first.is_a?(ActiveRecord::Base)
    t = []
    t << first.attribute_names
    t << nil
    each do |e|
      t << e.attributes.values_at(*first.attribute_names).map(&:as_json)
    end
    t
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
arql-0.1.12 lib/arql/ext/array.rb