Skip to content

Instantly share code, notes, and snippets.

@flockonus
Created March 4, 2011 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flockonus/855729 to your computer and use it in GitHub Desktop.
Save flockonus/855729 to your computer and use it in GitHub Desktop.
Rails 3 Model
class Admin::Bulky
CADASTRADO = "CADASTRADO"
INVALIDO = "INVALIDO"
# requires w: a workingsheet from the GoogleSpreadsheet lib
def self.import_restaurants( w )
# The headers I expect to be there, in String
fields = ['nome', 'tipo', 'telefone', 'endereço', 'cep', 'funcionamento', 'status']
# A Hash to have keys as headers and values as indexes
fields_h = fields.to_hash_true
# The conversion to Regexp of each header
fields_match = fields.to_regexp_arr
rows = w.rows
# # # # VALIDAÇÕES
return "É esperado que a célula A1 contenha o nome da cidade" if rows[0][0].blank? || rows[0][0].to_s.size < 2
unless head_match?( fields_match, rows[0] )
return "Cabeçalhos desta planilha não batem com o esperado. \nEsperado~ #{fields.inspect} \nRecebido: #{rows[0].inspect}"
end
# # # # PERSISTIR
# get the index of each key
fields.each_with_index do |field,i|
fields_h[field] = rows[0].index(){ |x| x.to_s =~ fields_match[i] }# + 1
end
city = rows[0][0]
records = []
total = adicionados = ignorados = invalidos = 0 # just4fun
rows[1..-1].each_with_index do |line,i|
# Skip a row that has any status OR nome is empty
#puts ">>> #{line[fields_h['status']].to_s}, #{line[fields_h['nome']]}"
if line[fields_h['status']].to_s == CADASTRADO || line[fields_h['nome']].blank?
ignorados += 1
next
end
total += 1
r = Store.new( :bulky => true )
r.cidade = city
r.nome = line[fields_h['nome']]
# OH WAIT TODO assoc.... r.tipo = line[fields_h['tipo']]
r.telefone = line[fields_h['telefone']]
r.funcionamento = line[fields_h['funcionamento']]
r.endereco = line[fields_h['endereço']]
r.cep = line[fields_h['cep']]
if !r.bulky_acceptable?
w[i+2,fields_h['status']+1] = INVALIDO
# Bad... too many errors! w[i+2,fields_h['status']+2] = r.errors.inspect
w[i+2,fields_h['status']+2] = "É requerido ao menos Nome e CEP da loja."
invalidos += 1
elsif r.bulky_dup? # duplicado
w[i+2,fields_h['status']+1] = INVALIDO
w[i+2,fields_h['status']+2] = "Duplicado por ter mesmo nome associado ao CEP."
invalidos += 1
elsif r.save(false) # successfuly added
adicionados += 1
records.push r
# when is valid and saved, mark it
w[i+2,fields_h['status']+1] = CADASTRADO
w[i+2,fields_h['status']+2] = ""
end
w.save #if added > 0
end
{:city => city,
:ignorados => ignorados,
:total => total,
:adicionados => adicionados,
:invalidos => invalidos,
:records => records
}
end
protected
# return true if all headers are present
def self.head_match?( regs, header=[] )
# also need to have a field to mark status.
to_go = regs.size
regs.each do |r|
# look for a string that matches the Regexp
to_go -= 1 if header.index { |x| x.to_s =~ r } # array_has_match?( header, r )
end
# need to have all headers here!
(to_go == 0) ? true : false
end
# # # # # # # # # # # # # # DEBUG
def self.get_sheet!
s = GoogleSpreadsheet.login('yo@user', 'yo_pass')
s.spreadsheet_by_key( 't0SmcVr8NMBLZns9V0zJA_as_yo_model_sheet' ).worksheets[0]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment