From b0a42e0faec2d18b0b5a509c8c59111f7cd09a66 Mon Sep 17 00:00:00 2001 From: Edward Loveall Date: Mon, 21 Jan 2019 10:55:23 -0500 Subject: [PATCH] WIP Add usernames --- .../20190120112946_add_username_to_user.cr | 13 +++++++++++++ ...90120141952_generate_usernames_for_users.cr | 18 ++++++++++++++++++ .../20190120142017_require_username_on_user.cr | 9 +++++++++ spec/support/flows/authentication_flow.cr | 4 +++- spec/support/flows/reset_password_flow.cr | 5 ++++- 5 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 db/migrations/20190120112946_add_username_to_user.cr create mode 100644 db/migrations/20190120141952_generate_usernames_for_users.cr create mode 100644 db/migrations/20190120142017_require_username_on_user.cr diff --git a/db/migrations/20190120112946_add_username_to_user.cr b/db/migrations/20190120112946_add_username_to_user.cr new file mode 100644 index 0000000..a9c5771 --- /dev/null +++ b/db/migrations/20190120112946_add_username_to_user.cr @@ -0,0 +1,13 @@ +class AddUsernameToUser::V20190120112946 < LuckyRecord::Migrator::Migration::V1 + def migrate + alter :users do + add username : String?, fill_existing_with: :nothing, unique: true + end + end + + def rollback + alter :users do + remove :username + end + end +end diff --git a/db/migrations/20190120141952_generate_usernames_for_users.cr b/db/migrations/20190120141952_generate_usernames_for_users.cr new file mode 100644 index 0000000..f8e814d --- /dev/null +++ b/db/migrations/20190120141952_generate_usernames_for_users.cr @@ -0,0 +1,18 @@ +class GenerateUsernamesForUsers::V20190120141952 < LuckyRecord::Migrator::Migration::V1 + class User < BaseModel + table :users do + column username : String? + end + end + + def migrate + UserQuery.all.each do |user| + new_username = user.email.split("@").first + User::BaseForm.update!(user, username: new_username) + puts "Generated username #{new_username} for #{user.email}" + end + end + + def rollback + end +end diff --git a/db/migrations/20190120142017_require_username_on_user.cr b/db/migrations/20190120142017_require_username_on_user.cr new file mode 100644 index 0000000..0782d2a --- /dev/null +++ b/db/migrations/20190120142017_require_username_on_user.cr @@ -0,0 +1,9 @@ +class RequireUsernameOnUser::V20190120142017 < LuckyRecord::Migrator::Migration::V1 + def migrate + execute "ALTER TABLE users ALTER COLUMN username SET NOT NULL" + end + + def rollback + execute "ALTER TABLE users ALTER COLUMN username DROP NOT NULL" + end +end diff --git a/spec/support/flows/authentication_flow.cr b/spec/support/flows/authentication_flow.cr index dc9a17d..932f17d 100644 --- a/spec/support/flows/authentication_flow.cr +++ b/spec/support/flows/authentication_flow.cr @@ -1,12 +1,14 @@ class AuthenticationFlow < BaseFlow private getter email + private getter username - def initialize(@email : String) + def initialize(@email : String, @username : String) end def sign_up(password) visit SignUps::New fill_form SignUpForm, + username: username, email: email, password: password, password_confirmation: password diff --git a/spec/support/flows/reset_password_flow.cr b/spec/support/flows/reset_password_flow.cr index 6135f03..8e6d7b2 100644 --- a/spec/support/flows/reset_password_flow.cr +++ b/spec/support/flows/reset_password_flow.cr @@ -5,7 +5,10 @@ class ResetPasswordFlow < BaseFlow delegate email, to: user def initialize(@user : User) - @authentication_flow = AuthenticationFlow.new(user.email) + @authentication_flow = AuthenticationFlow.new( + email: user.email, + username: user.username + ) end def request_password_reset