-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class CsvImportUsersService | ||
require 'csv' | ||
|
||
def call(file) | ||
file = File.open(file) | ||
csv = CSV.parse(file, headers: true, col_sep: ';') | ||
csv.each do |row| | ||
user_hash = {} | ||
user_hash[:name] = row['First Name'] | ||
user_hash[:surname] = row['Last Name'] | ||
user_hash[:email] = row['Email Address'] | ||
user_hash[:preferences] = row['Favorite Food'] | ||
user_hash[:phone] = row['Mobile phone number'] | ||
user_hash[:username] = row['Email Address'].split('@').first if row['Email Address'].present? | ||
User.find_or_create_by!(user_hash) | ||
# binding.b | ||
# p row | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
Rails.application.routes.draw do | ||
root to: redirect("/users") | ||
resources :users | ||
root to: redirect('/users') | ||
resources :users do | ||
collection do | ||
post :import | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
First Name;Last Name;Email Address;Favorite Food;Mobile phone number;; | ||
Yaro;Shm;yaro@example.com;Borsh;;; | ||
Yuli;Zah;yuli@example.com;;573335784;; | ||
Frodo;Baggins;;bread;;; | ||
James;Bond;;guns;;; |
Binary file not shown.