Repository is archived
No commit activity in last 3 years
No release in over 3 years
Simple email confirmation model implemented on top of ActiveModel::Model
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
>= 0

Runtime

>= 4.0.0
 Project Readme

ActiveModel::EmailConfirmation

ActiveModel::EmailConfirmation is a lightweight email confirmation model implemented on top of ActiveModel::Model. It does not require storing any additional information in the database. Resulting token is signed by ActiveSupport::MessageVerifier class, using secret_key_base and salt.

Installation

Add this line to your application's Gemfile:

gem "active_model-email_confirmation"

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_model-email_confirmation

Usage

The most popular workflow is:

class UsersController < ApplicationController
  def create
    # ...
    @email_confirmation = ActiveModel::EmailConfirmation.new(user: @user)
    UserMailer.confirm_email(@user.email, @email_confirmation.token).deliver
    # ...
  end
end

class EmailConfirmationsController < ApplicationController
  def show
    # find raises TokenInvalid, EmailInvalid exceptions
    @email_confirmation = ActiveModel::EmailConfirmation.find(params[:id])
    @user = @email_confirmation.user
    @user.update(confirmed_at: DateTime.now)
    # ...
  rescue ActiveModel::EmailConfirmation::Error
    raise ActiveRecord::RecordNotFound # display 404
  end
end

If you don't like the default behavior, you can always inherit the model and override some defaults:

class EmailConfirmation < ActiveModel::EmailConfirmation
  def email=(email)
    @email = email
    @user = Admin.find_by(email: email)
  end
end

Copyright

Copyright © 2014 Kuba Kuźma. See LICENSE for details.