Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/controllers/account_activations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class AccountActivationsController < ApplicationController
end
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def create
redirect_to forwarding_url || user
else
flash.now[:danger] = 'Invalid Email/Password Combination'
render 'new', status: :unprocessable_entity
render 'new', status: :unprocessable_entity
end
end

Expand Down
11 changes: 7 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ def new
def create
@user = User.new(user_params)
if @user.save
reset_session
log_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
UserMailer.account_activation(@user).deliver_now
flash[:info] = "Please check your Email to activate your account."
redirect_to root_url
# reset_session
# log_in @user
# flash[:success] = "Welcome to the Sample App!"
# redirect_to @user
else
render 'new', status: :unprocessable_entity
end
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/account_activations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module AccountActivationsHelper
end
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
default from: "adesinaadewale28@yahoo.com"
layout "mailer"
end
26 changes: 26 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class UserMailer < ApplicationMailer
default_url_options[:host] = "localhost:3000" # Use your development or production host
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.user_mailer.account_activation.subject
#
def account_activation(user)
@user = user
mail to: @user.email, subject: "Account Activation"
# @greeting = "Hi"

# mail to: "to@example.org"
end

# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.user_mailer.password_reset.subject
#
def password_reset
@greeting = "Hi"

mail to: "to@example.org"
end
end
17 changes: 16 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class User < ApplicationRecord
before_save {email.downcase!}
attr_accessor :remember_token
attr_accessor :remember_token, :activation_token
before_save :downcase_email
before_create :create_activation_digest
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: {maximum: 255},
Expand Down Expand Up @@ -46,4 +48,17 @@ def authenicated?(remember_token)
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end

private

# Converts email to all lowercase
def downcase_email
self.email = email.downcase
end

# Creates and assigns the activation token and digest
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
end
9 changes: 9 additions & 0 deletions app/views/user_mailer/account_activation.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Sample App</h1>

<p> Hi <%= @user.name %>, </p>

<p>
Welcome to the Sample App! Click on the link below to activate your account:
</p>

<%= link_to "Activate", edit_account_activation_url(@user.activation_token, email: @user.email) %>
5 changes: 5 additions & 0 deletions app/views/user_mailer/account_activation.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Hi <%= @user.name %>,

Welcome to the Sample App! click on the link below to activate your account:

<%= edit_account_activation_url(@user.activation_token, email: @user.email) %>
5 changes: 5 additions & 0 deletions app/views/user_mailer/password_reset.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>User#password_reset</h1>

<p>
<%= @greeting %>, find me in app/views/user_mailer/password_reset.html.erb
</p>
3 changes: 3 additions & 0 deletions app/views/user_mailer/password_reset.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
User#password_reset

<%= @greeting %>, find me in app/views/user_mailer/password_reset.text.erb
2 changes: 1 addition & 1 deletion app/views/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
</ul>
<div class= "center" >
<%= paginate @users %>
</div>
</div>
4 changes: 4 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

host = 'localhost:3000' #local server

config.action_mailer.default_url_options = {host: 'localhost:3000', protocol: 'http'}

config.action_mailer.perform_caching = false

# Print deprecation notices to the Rails logger.
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
post "/login", to: "sessions#create"
delete "/logout", to: "sessions#destroy"
resources :users
resources :account_activations, only: [:edit]

# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20240806170845_add_activation_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddActivationToUsers < ActiveRecord::Migration[7.1]
def change
add_column :users, :activation_digest, :string
add_column :users, :activated, :boolean, default: false
add_column :users, :activated_at, :datetime
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
User.create!(name: "James",
email: "sample1@example.com",
password: "test123",
password_confirmation: "test123")
password_confirmation: "test123",
admin: true,
activated: true,
activated_at: Time.zone.now)

# Generate a bunch of additional users
99.times do |n|
Expand All @@ -21,5 +24,7 @@
User.create!(name: name,
email: email,
password: password,
password_confirmation: password)
password_confirmation: password,
activated: true,
activated_at: Time.zone.now)
end
7 changes: 7 additions & 0 deletions test/controllers/account_activations_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class AccountActivationsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
36 changes: 36 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,39 @@ michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
admin: true
activated: true
activated_at: <%= Time.zone.now%>

archer:
name: Archer Example
email: archer@example.com
password_digest: <%= User.digest('password') %>
admin: true
activated: true
activated_at: <%= Time.zone.now%>

lana:
name: Lana Example
email: lana@example.com
password_digest: <%= User.digest('password') %>
admin: true
activated: true
activated_at: <%= Time.zone.now%>

malory:
name: Malory Example
email: malory@example.com
password_digest: <%= User.digest('password') %>
admin: true
activated: true
activated_at: <%= Time.zone.now%>

<% 30.times do |n| %>
user_<%= n %>:
name: <%= "User #{n}" %>
email: <%= user-#{n}@example.com" %>
password_digest: <%= User.digest('password') %>
activated: true
activated_at: <%= Time.zone.now %>
<% end %>
16 changes: 16 additions & 0 deletions test/mailers/previews/user_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Preview all emails at http://localhost:3000/rails/mailers/user_mailer
class UserMailerPreview < ActionMailer::Preview

# Preview this email at http://localhost:3000/rails/mailers/user_mailer/account_activation
def account_activation
user = User.first
user.activation_token = User.new_token
UserMailer.account_activation(user)
end

# Preview this email at http://localhost:3000/rails/mailers/user_mailer/password_reset
def password_reset
UserMailer.password_reset
end

end
20 changes: 20 additions & 0 deletions test/mailers/user_mailer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "test_helper"

class UserMailerTest < ActionMailer::TestCase
test "account_activation" do
mail = UserMailer.account_activation
assert_equal "Account activation", mail.subject
assert_equal ["to@example.org"], mail.to
assert_equal ["from@example.com"], mail.from
assert_match "Hi", mail.body.encoded
end

test "password_reset" do
mail = UserMailer.password_reset
assert_equal "Password reset", mail.subject
assert_equal ["to@example.org"], mail.to
assert_equal ["from@example.com"], mail.from
assert_match "Hi", mail.body.encoded
end

end