Mailer for Confirmation of CLient

This commit is contained in:
Saurabh Bhatia 2014-02-24 14:48:32 +08:00
parent 9371542966
commit 1f02857aa3
10 changed files with 100 additions and 2 deletions

View File

@ -6,6 +6,7 @@ module Api
def create def create
@client = Client.new(client_params) @client = Client.new(client_params)
if @client.save if @client.save
@client.send_confirmation_email
render :json => { :success => true, :client => @client.to_json } render :json => { :success => true, :client => @client.to_json }
else else
render :json => { :errors => @client.errors.full_messages , :status => 422}.to_json render :json => { :errors => @client.errors.full_messages , :status => 422}.to_json

View File

@ -1,6 +1,6 @@
class ClientsController < ApplicationController class ClientsController < ApplicationController
before_action :set_client, only: [:show, :edit, :update, :destroy] before_action :set_client, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user! before_filter :authenticate_user!, except: [:confirm_client, :post_confirmation, :resend_confirmation, :reconfirm_client]
# GET /clients # GET /clients
# GET /clients.json # GET /clients.json
@ -22,6 +22,29 @@ class ClientsController < ApplicationController
def edit def edit
end end
def confirm_client
client = Client.confirm_email(params[:token])
redirect_to post_confirmation_clients_path
if client.first[:success].eql?("true")
flash[:notice] = "You have confirmed successfully"
else
flash[:notice] = "Error in confirmation please try again."
end
end
def resend_confirmation
end
def reconfirm_client
client = Client.send_reconformation_email(params[:email])
redirect_to post_confirmation_clients_path
end
def post_confirmation
end
# POST /clients # POST /clients
# POST /clients.json # POST /clients.json
def create def create

View File

@ -0,0 +1,9 @@
class ConfirmAdminMailer < ActionMailer::Base
default from: "noreply@store.tp.rulingcom.com"
def admin_confirmation_email(client)
email = client.email
@confirmation_token = client.confirmation_token
mail(:to => email, :subject => "Confirmation instructions")
end
end

View File

@ -8,7 +8,45 @@ class Client
field :email, type: String field :email, type: String
field :country, type: String field :country, type: String
field :url, type: String field :url, type: String
field :confirmation_token, type: String
index({ confirmation_token: 1}, { unique: true })
validates :site_id, :uniqueness => true validates :site_id, :uniqueness => true
validates :site_token, :uniqueness => true validates :site_token, :uniqueness => true
def generate_confirmation_token
self.confirmation_token = SecureRandom.hex(5)
self.save
end
def send_confirmation_email
self.generate_confirmation_token
ConfirmAdminMailer.admin_confirmation_email(self).deliver
end
def self.send_reconformation_email(email)
client = self.find_by(email: email) rescue nil
client_status = (client.present? && client.confirmation_token.present?)
case client_status
when true
client.send_confirmation_email
return[{:success => "true"}]
when false
return [{:success => "false"}]
end
end
def self.confirm_email(confirmation_token)
client = self.find_by(confirmation_token: confirmation_token) rescue nil
token_status = client.present?
case token_status
when true
client.confirmation_token = nil
client.save
return [{:success => "true"}]
when false
return [{:success => "false"}]
end
end
end end

View File

@ -0,0 +1,3 @@
<%=h flash[:notice] %>
<p>In case there are any issues with your conrimation please click <%= link_to "here", resend_confirmation_clients_path %> to reconfirm</p>

View File

@ -0,0 +1,5 @@
<%= form_tag reconfirm_client_clients_path do%>
<p>Please Enter your email so that we can resend you confirmation instructions</p>
<%= text_field_tag :email, params[:email]%>
<%= submit_tag "Send", :email => nil %>
<% end %>

View File

@ -0,0 +1,4 @@
<p>Thank you for registering your site with Orbit Store! Please click on the following link and confirm your site. This is to make sure we could provide you with a spam free experience. Thanks a lot.</p>
<%= link_to 'Click here to confirm', confirm_client_clients_url(:token => @confirmation_token)%>

View File

@ -27,4 +27,5 @@ Mtstore::Application.configure do
config.assets.debug = true config.assets.debug = true
config.action_mailer.delivery_method = :smtp config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 } config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
config.action_mailer.default_url_options = { host: "localhost", :port => 3000, protocol: "http" }
end end

View File

@ -2,7 +2,14 @@ require 'api_constraints'
Mtstore::Application.routes.draw do Mtstore::Application.routes.draw do
resources :clients resources :clients do
collection do
get 'confirm_client'
get 'post_confirmation'
get 'resend_confirmation'
post 'reconfirm_client'
end
end
# get "search/index" # get "search/index"
# get "search/show" # get "search/show"

View File

@ -0,0 +1,7 @@
require 'test_helper'
class ConfirmAdminMailerTest < ActionMailer::TestCase
# test "the truth" do
# assert true
# end
end