Skip to content

Commit baa0972

Browse files
author
Torie Joy-Warren
committed
Replace username with email
1 parent 29cc3cb commit baa0972

17 files changed

+62
-44
lines changed

app/controllers/sessions_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def new
55
end
66

77
def create
8-
user = User.find_by(username: params[:username])
8+
user = User.find_by(email: params[:email])
99
if user&.authenticate(params[:password])
1010
session[:user_id] = user.id
1111
redirect_to user_path(user)

app/controllers/users_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ def create
1717
end
1818

1919
def user_params
20-
params.require(:user).permit(:username, :password)
20+
params.require(:user).permit(:name, :email, :password)
2121
end
2222
end

app/models/user.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ class User < ApplicationRecord
22
has_many :recipes
33
has_one :grocery_list
44

5-
validates :username,
5+
validates :name,
6+
presence: true,
7+
allow_blank: false
8+
9+
validates :email,
610
presence: true,
711
allow_blank: false,
8-
uniqueness: { message: "\"%{value}\" is already taken. Please choose another!" }
12+
format: { with: URI::MailTo::EMAIL_REGEXP },
13+
uniqueness: { message: "An account with that email has already been created." }
914

1015
has_secure_password
1116

app/views/sessions/new.html.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
<%= form_tag "/login" do %>
55
<div class="field">
6-
<%= label_tag :username, nil, class: "label" %>
7-
<%= text_field_tag :username, nil, class: "input" %> <br/>
6+
<%= label_tag :email, nil, class: "label" %>
7+
<%= text_field_tag :email, nil, class: "input" %> <br/>
88
</div>
99
<div class="field">
1010
<%= label_tag :password, nil, class: "label" %>

app/views/users/new.html.erb

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
<%= form_for(@user) do |f| %>
55
<div class="field">
6-
<%= f.label :username, class: "label" %>
7-
<%= f.text_field :username, class: "input" %> <br/>
6+
<%= f.label :name, class: "label" %>
7+
<%= f.text_field :name, class: "input" %> <br/>
8+
</div>
9+
<div class="field">
10+
<%= f.label :email, class: "label" %>
11+
<%= f.text_field :email, class: "input" %> <br/>
812
</div>
913
<div class="field">
1014
<%= f.label :password, class: "label" %>

app/views/users/show.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div id="user-profile">
22
<h1 id="user-welcome">
3-
<%= "Welcome, #{current_user.username}!" %>
3+
<%= "Welcome, #{current_user.name}!" %>
44
</h1>
55

66
<div class="columns">
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class CreateUsers < ActiveRecord::Migration[6.0]
22
def change
33
create_table :users do |t|
4-
t.string :username, null: false
4+
t.string :name, null: false
5+
t.string :email, null: false
56
t.string :password_digest, null: false
67

78
t.timestamps
89
end
9-
add_index :users, :username, unique: true
10+
add_index :users, :email, unique: true
1011
end
1112
end

db/structure.sql

+4-3
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ CREATE TABLE public.schema_migrations (
219219

220220
CREATE TABLE public.users (
221221
id bigint NOT NULL,
222-
username character varying NOT NULL,
222+
name character varying NOT NULL,
223+
email character varying NOT NULL,
223224
password_digest character varying NOT NULL,
224225
created_at timestamp(6) without time zone NOT NULL,
225226
updated_at timestamp(6) without time zone NOT NULL
@@ -408,10 +409,10 @@ CREATE INDEX index_recipes_on_user_id ON public.recipes USING btree (user_id);
408409

409410

410411
--
411-
-- Name: index_users_on_username; Type: INDEX; Schema: public; Owner: -
412+
-- Name: index_users_on_email; Type: INDEX; Schema: public; Owner: -
412413
--
413414

414-
CREATE UNIQUE INDEX index_users_on_username ON public.users USING btree (username);
415+
CREATE UNIQUE INDEX index_users_on_email ON public.users USING btree (email);
415416

416417

417418
--

spec/controllers/recipes_controller_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
describe RecipesController do
44
include Helpers::ControllerAuthentication
55

6-
let(:user) { User.create!(username: "user1234", password: "password") }
6+
let(:user) { User.create!(name: "name", email: "[email protected]", password: "password") }
77

88
describe "GET #new" do
99
before do

spec/controllers/sessions_controller_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
describe "POST #create" do
1414
subject { post :create, params: user_params }
1515
let(:user_params) {
16-
{ username: username, password: password }
16+
{ email: email, password: password }
1717
}
18-
let(:username) { "user1234" }
18+
let(:email) { "[email protected]" }
1919
let(:password) { "password" }
2020

2121
context "user exists" do
22-
let!(:user) { User.create!(username: username, password: password) }
22+
let!(:user) { User.create!(name: "name", email: email, password: password) }
2323

2424
context "user credentials are valid" do
2525
it "logs in the user" do
@@ -30,7 +30,7 @@
3030

3131
context "user credentials are invalid" do
3232
let(:user_params) {
33-
{ username: username, password: "badpassword" }
33+
{ email: email, password: "badpassword" }
3434
}
3535

3636
it "does not log in the user" do

spec/controllers/users_controller_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
describe "POST #create" do
1414
subject { post :create, params: { user: user_params } }
1515
let(:user_params) {
16-
{ username: "user1234", password: "password" }
16+
{ email: "[email protected]", name: "name", password: "password" }
1717
}
1818

1919
it "redirects to the user path" do
@@ -24,21 +24,21 @@
2424
expect { subject }.to change { User.count }.by 1
2525
end
2626

27-
context "no username is provided" do
27+
context "no email is provided" do
2828
let(:user_params) {
29-
{ username: nil, password: "password" }
29+
{ email: nil, name: "name", password: "password" }
3030
}
3131

3232
it "renders the sign up form with an error" do
3333
expect(subject).to render_template(:new)
3434

35-
expect(flash.alert).to eq "Username can't be blank"
35+
expect(flash.alert).to eq "Email can't be blank, Email is invalid"
3636
end
3737
end
3838

3939
context "no password is provided" do
4040
let(:user_params) {
41-
{ username: "user1234", password: nil }
41+
{ email: "[email protected]", name: "user1234", password: nil }
4242
}
4343

4444
it "renders the sign up form with an error" do

spec/factories/user.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FactoryBot.define do
22
factory :user do
3-
username { Faker::Name.unique.name }
3+
name { Faker::Name.unique.name }
4+
email { Faker::Internet.email }
45
password { "password" }
56
end
67
end

spec/features/user_log_out_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
include Helpers::FeatureAuthentication
55

66
context "user is logged in" do
7-
let(:user) { User.create!(username: "user1234", password: "password") }
7+
let(:user) { create(:user) }
88

99
before do
1010
sign_in(user)

spec/features/user_login_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
require "rails_helper"
22

33
feature "User login" do
4-
let(:username) { "user1234" }
4+
let(:email) { "[email protected]" }
55
let(:password) { "password" }
66

77
context "user provides valid login credentials" do
8-
let!(:user) { User.create!(username: username, password: password) }
8+
let!(:user) { create(:user, email: email, password: password) }
99

1010
it "redirects to the user's profile page" do
1111
visit login_path
1212

13-
fill_in :username, with: username
13+
fill_in :email, with: email
1414
fill_in :password, with: password
1515

1616
click_button "Log In"
@@ -23,7 +23,7 @@
2323
it "notifies the user" do
2424
visit login_path
2525

26-
fill_in :username, with: username
26+
fill_in :email, with: email
2727
fill_in :password, with: password
2828

2929
click_button "Log In"

spec/features/user_profile_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
include Helpers::FeatureAuthentication
55

66
context "user is logged in" do
7-
let(:user) { User.create!(username: "user1234", password: "password") }
7+
let(:user) { create(:user) }
88
let!(:recipe1) {
99
create(:recipe, user: user, instructions: "Mix & bake :)", notes: "notes on this recipe")
1010
}
@@ -18,7 +18,7 @@
1818

1919
it "shows the user's profile" do
2020
expect(current_path).to eq user_path(user)
21-
expect(page).to have_text "Welcome, #{user.username}!"
21+
expect(page).to have_text "Welcome, #{user.name}!"
2222

2323
expect(page).to have_text "Recipes"
2424
expect(page).to have_link recipe1.name

spec/features/user_sign_up_spec.rb

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
require "rails_helper"
22

33
feature "User sign up" do
4-
let(:username) { "user1234" }
4+
let(:email) { "[email protected]" }
5+
let(:name) { "name" }
56

67
def click_submit
78
click_button "Create account"
@@ -12,14 +13,16 @@ def click_submit
1213

1314
expect(page).to have_text "Create an account"
1415

15-
fill_in "user_username", with: username
16+
fill_in "user_name", with: name
17+
fill_in "user_email", with: email
1618
fill_in "user_password", with: "securepassword"
1719

1820
expect { click_submit }.to change { User.count }.by 1
1921

2022
new_user = User.last
2123

22-
expect(new_user.username).to eq username
24+
expect(new_user.name).to eq name
25+
expect(new_user.email).to eq email
2326
expect(new_user.password_digest).to_not be nil
2427
expect(current_path).to eq user_path(new_user)
2528
end
@@ -28,38 +31,41 @@ def click_submit
2831
it "does not allow the user to submit the form" do
2932
visit new_user_path
3033

31-
fill_in "user_username", with: username
34+
fill_in "user_email", with: email
35+
fill_in "user_name", with: name
3236
click_submit
3337

3438
expect(page).to have_text "Password can't be blank"
3539
end
3640
end
3741

38-
context "no username is provided" do
42+
context "no email is provided" do
3943
it "does not allow the user to submit the form" do
4044
visit new_user_path
4145

46+
fill_in "user_name", with: name
4247
fill_in "user_password", with: "password"
4348
click_submit
4449

45-
expect(page).to have_text "Username can't be blank"
50+
expect(page).to have_text "Email can't be blank"
4651
end
4752
end
4853

49-
context "username has already been taken" do
54+
context "email has already been taken" do
5055
before do
51-
User.create!(username: username, password: "password")
56+
create(:user, email: email)
5257
end
5358

54-
it "prompts the user to choose another username" do
59+
it "prompts the user to choose another email" do
5560
visit new_user_path
5661

57-
fill_in "user_username", with: username
62+
fill_in "user_name", with: name
63+
fill_in "user_email", with: email
5864
fill_in "user_password", with: "securepassword"
5965

6066
click_submit
6167

62-
expect(page).to have_text "Username \"#{username}\" is already taken. Please choose another!"
68+
expect(page).to have_text "An account with that email has already been created."
6369
end
6470
end
6571
end

spec/support/helpers/authentication.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module FeatureAuthentication
33
def sign_in(user)
44
visit login_path
55

6-
fill_in :username, with: user.username
6+
fill_in :email, with: user.email
77
fill_in :password, with: user.password
88

99
click_button "Log In"

0 commit comments

Comments
 (0)