Skip to content

Commit

Permalink
Create email confirmation. close #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloschiavon committed May 23, 2014
1 parent 6575d33 commit f7b7a1e
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 2 deletions.
12 changes: 12 additions & 0 deletions app/controllers/confirmations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class ConfirmationsController < ApplicationController
def show
user = User.find_by(confirmation_token: params[:token])

if user.present?
user.confirm!
redirect_to user, notice: I18n.t('confirmations.success')
else
redirect_to root_path, notice: I18n.t('confirmations.unsuccess')
end
end
end
17 changes: 17 additions & 0 deletions app/mailers/signup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Signup < ActionMailer::Base
default from: 'no-reply@colcho.net'

def confirm_email(user)
@user = user

@confirmation_link = confirmation_url({
token: @user.confirmation_token
})

mail({
to: user.email,
bcc: ['sign ups <signups@colcho.net>'],
subject: I18n.t('signup.confirm_email.subject')
})
end
end
15 changes: 15 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,19 @@ class User < ActiveRecord::Base
allow_blank: false

has_secure_password

before_create do |user|
user.confirmation_token = SecureRandom.urlsafe_base64
end

def confirm!
return if confirmed?
self.confirmed_at = Time.current
self.confirmation_token = ''
save!
end

def confirmed?
confirmed_at.present?
end
end
11 changes: 11 additions & 0 deletions app/views/signup/confirm_email.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1><%= t '.title' %> </h1>

<p>
<%= t '.body', full_name: @user.full_name %>
</p>
<p>
<%= t '.confirm_link_html',link: link_to(@confirmation_link, @confirmation_link) %>
</p>
<p>
<%= t '.thanks_html', link: link_to('Colcho.net', root_url) %>
</p>
6 changes: 6 additions & 0 deletions app/views/signup/confirm_email.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= t '.title' %>

%= t '.body', full_name: @user.full_name %>
<%= t '.confirm_link_html', link: @confirmation_link %>
<%= t '.thanks_html', link: root_url %>
19 changes: 18 additions & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,24 @@
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

# indica o host para o ambiente de desenvolvimento
# impostante para o envio de e-mails
config.action_mailer.default_url_options = {
host: "localhost:3000"
}

# Aponta o host para o ambiente de desenvolvimento
config.action_mailer.default_url_options = {
host: "localhost:3000"
}

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "localhost",
port: 1025
}
end
14 changes: 14 additions & 0 deletions config/locales/pt.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pt:
confirmations:
success: "Email confirmado com sucesso, obrigado!"
rooms:
new:
title: Cadastro
Expand All @@ -19,6 +21,17 @@ pt:
edit: Ediar perfil
location: "Localização: %{location}"
bio: "Bio: %{bio}"
signup:
confirm_email:
body: |
Seja bem vindo ao Colcho.net, %{full_name}.
O Colcho.net é o lugar ideal para você alugar
aquele quarto sobrando na sua casa e ainda conhecer
gente do mundo inteiro.
confirm_link_html: "Para você começar a usar o site, acesse o link %{link}"
subject: "Colcho.net - Confirme seu email"
thanks_html: "Obrigado por se cadastrar no %{link}."
title: "Seja bem vindo ao Colcho.net!"
links:
back: Voltar
layout:
Expand All @@ -42,3 +55,4 @@ pt:
location: Localização
password: Senha
password_confirmation: "Confirme sua senha"

2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
resources :users
end

resource :confirmation, only: [:show]

get '/:locale' => 'home#index', locale: /en|pt/
root 'home#index'

Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20140523221343_add_confirmation_fields_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AddConfirmationFieldsToUsers < ActiveRecord::Migration
def change
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_token, :string
end

#change_table :users do |t|
# t.datetime :confirmed_at
# t.string :confirmation_token
#end
end

4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140523002950) do
ActiveRecord::Schema.define(version: 20140523221343) do

create_table "rooms", force: true do |t|
t.string "title"
Expand All @@ -29,6 +29,8 @@
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
t.datetime "confirmed_at"
t.string "confirmation_token"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true
Expand Down

0 comments on commit f7b7a1e

Please sign in to comment.