From 1f02857aa3e056197d91d3f01b649c534d43a4ed Mon Sep 17 00:00:00 2001 From: Saurabh Bhatia Date: Mon, 24 Feb 2014 14:48:32 +0800 Subject: [PATCH] Mailer for Confirmation of CLient --- app/controllers/api/v1/clients_controller.rb | 1 + app/controllers/clients_controller.rb | 25 +++++++++++- app/mailers/confirm_admin_mailer.rb | 9 +++++ app/models/client.rb | 38 +++++++++++++++++++ app/views/clients/post_confirmation.html.erb | 3 ++ .../clients/resend_confirmation.html.erb | 5 +++ .../admin_confirmation_email.html.erb | 4 ++ config/environments/development.rb | 1 + config/routes.rb | 9 ++++- test/mailers/confirm_admin_mailer_test.rb | 7 ++++ 10 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 app/mailers/confirm_admin_mailer.rb create mode 100644 app/views/clients/post_confirmation.html.erb create mode 100644 app/views/clients/resend_confirmation.html.erb create mode 100644 app/views/confirm_admin_mailer/admin_confirmation_email.html.erb create mode 100644 test/mailers/confirm_admin_mailer_test.rb diff --git a/app/controllers/api/v1/clients_controller.rb b/app/controllers/api/v1/clients_controller.rb index 112d7ba..1925b7e 100644 --- a/app/controllers/api/v1/clients_controller.rb +++ b/app/controllers/api/v1/clients_controller.rb @@ -6,6 +6,7 @@ module Api def create @client = Client.new(client_params) if @client.save + @client.send_confirmation_email render :json => { :success => true, :client => @client.to_json } else render :json => { :errors => @client.errors.full_messages , :status => 422}.to_json diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index 3c32428..ddd0600 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -1,6 +1,6 @@ class ClientsController < ApplicationController 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.json @@ -22,6 +22,29 @@ class ClientsController < ApplicationController def edit 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.json def create diff --git a/app/mailers/confirm_admin_mailer.rb b/app/mailers/confirm_admin_mailer.rb new file mode 100644 index 0000000..4445f45 --- /dev/null +++ b/app/mailers/confirm_admin_mailer.rb @@ -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 diff --git a/app/models/client.rb b/app/models/client.rb index da08203..45cf441 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -8,7 +8,45 @@ class Client field :email, type: String field :country, type: String field :url, type: String + field :confirmation_token, type: String + + index({ confirmation_token: 1}, { unique: true }) validates :site_id, :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 \ No newline at end of file diff --git a/app/views/clients/post_confirmation.html.erb b/app/views/clients/post_confirmation.html.erb new file mode 100644 index 0000000..88bb57b --- /dev/null +++ b/app/views/clients/post_confirmation.html.erb @@ -0,0 +1,3 @@ +<%=h flash[:notice] %> + +

In case there are any issues with your conrimation please click <%= link_to "here", resend_confirmation_clients_path %> to reconfirm

\ No newline at end of file diff --git a/app/views/clients/resend_confirmation.html.erb b/app/views/clients/resend_confirmation.html.erb new file mode 100644 index 0000000..2c1111c --- /dev/null +++ b/app/views/clients/resend_confirmation.html.erb @@ -0,0 +1,5 @@ +<%= form_tag reconfirm_client_clients_path do%> +

Please Enter your email so that we can resend you confirmation instructions

+ <%= text_field_tag :email, params[:email]%> + <%= submit_tag "Send", :email => nil %> +<% end %> \ No newline at end of file diff --git a/app/views/confirm_admin_mailer/admin_confirmation_email.html.erb b/app/views/confirm_admin_mailer/admin_confirmation_email.html.erb new file mode 100644 index 0000000..2fb13b1 --- /dev/null +++ b/app/views/confirm_admin_mailer/admin_confirmation_email.html.erb @@ -0,0 +1,4 @@ +

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.

+ + +<%= link_to 'Click here to confirm', confirm_client_clients_url(:token => @confirmation_token)%> \ No newline at end of file diff --git a/config/environments/development.rb b/config/environments/development.rb index bf1a224..3be0352 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -27,4 +27,5 @@ Mtstore::Application.configure do config.assets.debug = true config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 } + config.action_mailer.default_url_options = { host: "localhost", :port => 3000, protocol: "http" } end diff --git a/config/routes.rb b/config/routes.rb index a3a6839..2cdbfbf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,14 @@ require 'api_constraints' 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/show" diff --git a/test/mailers/confirm_admin_mailer_test.rb b/test/mailers/confirm_admin_mailer_test.rb new file mode 100644 index 0000000..a45f559 --- /dev/null +++ b/test/mailers/confirm_admin_mailer_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ConfirmAdminMailerTest < ActionMailer::TestCase + # test "the truth" do + # assert true + # end +end