Advertisement
Guest User

Untitled

a guest
May 24th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. # Export companies and customers to local DB
  2. CSV.foreach('./risotest.csv', headers: true, header_converters: :symbol, encoding: 'UTF-8') do |row|
  3. company = Company.find_or_create_by_id(row[:dealerid], {name: row[:name], custom_fields: {dealerid: row[:dealerid]}})
  4. company.customers.find_or_create_by_id(row[:techid], {
  5. first_name: row[:first_name],
  6. last_name: row[:last_name],
  7. custom_fields: {techid: row[:techid]},
  8. title: row[:title],
  9. addresses: [{
  10. type: 'work',
  11. # remove the , if any address field is empty
  12. value: "#{(row[:address].to_s.strip.empty? ? row[:address].to_s : (row[:address] +", ")) +
  13. (row[:city].to_s.strip.empty? ? row[:city].to_s : (row[:city] + ", ")) +
  14. (row[:state].to_s.strip.empty? ? row[:state].to_s : (row[:state] + " ")) +
  15. (row[:zip].to_s.strip.empty? ? row[:zip].to_s : (row[:zip] + ", ")) +
  16. (row[:country].to_s)}"
  17. }].select { |x| x[:value].gsub(/,/, '').present? },
  18. phone_numbers: [{type: 'work', value: row[:phone_numbers]}].select { |x| x[:value].present? }
  19. emails: [{type: 'work', value: row[:emails]}].select {|x| x.present?}
  20. })
  21. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement