class Inscriber::Uploader

Attributes

database[RW]
locale[RW]
records[RW]
table[RW]

Public Class Methods

new(opts) click to toggle source
# File lib/inscriber/uploader.rb, line 6
def initialize(opts)
  @database = opts[:database]
  @table    = opts[:table]
  @records  = opts[:records]
  @locale   = opts[:locale]
end

Public Instance Methods

upload() click to toggle source
# File lib/inscriber/uploader.rb, line 13
def upload
  records.each do |record|
    update_or_create_record(record)
  end
end

Private Instance Methods

created_at() click to toggle source
# File lib/inscriber/uploader.rb, line 49
def created_at
  return {} unless db_from_table.columns.include?(:created_at)
  { created_at: Time.now }
end
db_connection() click to toggle source
# File lib/inscriber/uploader.rb, line 21
def db_connection
  @db ||= database.connection
end
db_from_table() click to toggle source
# File lib/inscriber/uploader.rb, line 25
def db_from_table
  db_connection.from(table)
end
original_column() click to toggle source
# File lib/inscriber/uploader.rb, line 29
def original_column
  original_column_name(table)
end
update_or_create_record(record) click to toggle source
# File lib/inscriber/uploader.rb, line 33
def update_or_create_record(record)
  row = db_from_table.where(
    original_column => record[original_column],
    :locale => locale) 

  if row.empty?
    db_from_table.insert({
      locale: locale}
      .merge(created_at)
      .merge(updated_at)
      .merge(record))
  else
    row.update(record)
  end
end
updated_at() click to toggle source
# File lib/inscriber/uploader.rb, line 54
def updated_at 
  return {} unless db_from_table.columns.include?(:updated_at)
  { updated_at: Time.now }
end