Devise session and registration override, anlytics and welcome emails

James Peret 10 anos atrás
pai
commit
1dbf7f9b61

+ 0 - 1
app/controllers/application_controller.rb

@@ -59,7 +59,6 @@ class ApplicationController < ActionController::Base
59 59
     if @analytics == nil
60 60
       analytics
61 61
     end
62
-    @analytics.track_user_sign_in(current_user)
63 62
     stored_location_for(resource_or_scope) || dashboard_path
64 63
   end
65 64
   

+ 1 - 0
app/controllers/invites_controller.rb

@@ -15,6 +15,7 @@ class InvitesController < ApplicationController
15 15
     UserMailer.invite_message(@user, current_user, @message).deliver if @user.errors.empty?
16 16
 
17 17
     if @user.errors.empty? && @user.update(invitation_sent_at: Time.now.utc)
18
+      @analytics.track('Invite sent')
18 19
       flash[:notice] = "successfully sent invite to #{@user.email}"
19 20
       redirect_to invite_path
20 21
     else

+ 4 - 1
app/controllers/users/invitations_controller.rb

@@ -11,11 +11,14 @@ class Users::InvitationsController < Devise::InvitationsController
11 11
         resource.skip_password = true
12 12
         resource.update_attributes update_resource_params.except(:invitation_token)
13 13
       end
14
-      format.html do      
14
+      format.html do 
15
+        
15 16
         invitation_token = Devise.token_generator.digest(resource_class, :invitation_token, update_resource_params[:invitation_token])
16 17
         @user = User.where(email: params[:user][:email]).first
17 18
         @user.update(first_name: params[:user][:first_name], last_name: params[:user][:last_name])
18 19
         @user.update_attributes update_resource_params.except(:invitation_token)
20
+        @analytics.track_invitation_accepted(@user)
21
+        Resque.enqueue(SendSignupMessageSimple, @user.id) 
19 22
         super
20 23
       end
21 24
     end

+ 35 - 1
app/controllers/users/registrations_controller.rb

@@ -2,6 +2,40 @@ class Users::RegistrationsController < Devise::RegistrationsController
2 2
   
3 3
   # layout 'auth'
4 4
   
5
+  def create
6
+    
7
+    @user = User.where(email: params[:user][:email]).first
8
+    if @user != nil
9
+      if @user.invitation_accepted_at == nil && @user.invitation_sent_at != nil
10
+        @user.destroy
11
+      end
12
+    end
13
+
14
+    build_resource(sign_up_params)
15
+    resource.save
16
+    yield resource if block_given?
17
+    if resource.persisted?
18
+      @user = User.where(email: params[:user][:email]).first
19
+      # Mixpanel Tracking Analytics
20
+      @analytics.track_user_registration(@user)
21
+      # Send signup email (worker)
22
+      Resque.enqueue(SendSignupMessage, @user.id)
23
+      if resource.active_for_authentication?
24
+        set_flash_message :notice, :signed_up if is_flashing_format?
25
+        sign_up(resource_name, resource)
26
+        respond_with resource, location: after_sign_up_path_for(resource)
27
+      else
28
+        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
29
+        expire_data_after_sign_in!
30
+        respond_with resource, location: after_inactive_sign_up_path_for(resource)
31
+      end
32
+    else
33
+      clean_up_passwords resource
34
+      set_minimum_password_length
35
+      respond_with resource
36
+    end
37
+  end
38
+  
5 39
   def update
6 40
      @user = User.find(current_user.id)
7 41
 
@@ -28,7 +62,7 @@ class Users::RegistrationsController < Devise::RegistrationsController
28 62
    end
29 63
 
30 64
   def after_sign_up_path_for(resource)
31
-    root_path
65
+    dashboard_path
32 66
   end
33 67
   
34 68
   private

+ 16 - 6
app/controllers/users/sessions_controller.rb

@@ -2,13 +2,23 @@ class Users::SessionsController < Devise::SessionsController
2 2
   
3 3
   layout 'cover'
4 4
   
5
-  # def new
6
-  #   super
7
-  # end
5
+  # GET /resource/sign_in  
6
+  def new
7
+    self.resource = resource_class.new(sign_in_params)
8
+    clean_up_passwords(resource)
9
+    yield resource if block_given?
10
+    respond_with(resource, serialize_options(resource))
11
+  end
8 12
 
9
-  # def create
10
-  #   super
11
-  # end
13
+  # POST /resource/sign_in
14
+  def create
15
+    self.resource = warden.authenticate!(auth_options)
16
+    set_flash_message(:notice, :signed_in) if is_flashing_format?
17
+    sign_in(resource_name, resource)
18
+    yield resource if block_given?
19
+    @analytics.track_user_sign_in(current_user)
20
+    respond_with resource, location: after_sign_in_path_for(resource)
21
+  end
12 22
 
13 23
   
14 24
 end

+ 10 - 0
app/mailers/user_mailer.rb

@@ -13,6 +13,16 @@ class UserMailer < ActionMailer::Base
13 13
            :from_name => 'Avalanche Network'
14 14
     end
15 15
   end
16
+  
17
+  def signup_message_simple(user)
18
+    I18n.with_locale(user.language) do
19
+      config = Info.first
20
+      mail :to        => user.email,
21
+           :subject   => (t 'sign_up_email.email_subject'),
22
+           :from      => 'mission_control@avalanche.network',
23
+           :from_name => 'Avalanche Network'
24
+    end
25
+  end
16 26
 
17 27
   def contact_message(contact_message, to_address)
18 28
     @msg = contact_message

+ 7 - 0
app/models/analytics.rb

@@ -20,6 +20,13 @@ class Analytics
20 20
     end
21 21
   end 
22 22
   
23
+  def track_invitation_accepted(user)
24
+    if Rails.env.production?
25
+      identify(user)
26
+      @tracker.track(user.full_name, 'Invite accepted')
27
+    end
28
+  end 
29
+  
23 30
   def track_user_sign_in(user)
24 31
     if Rails.env.production?
25 32
       identify(user)

+ 0 - 5
app/models/user.rb

@@ -27,15 +27,10 @@ class User < ActiveRecord::Base
27 27
   end
28 28
   
29 29
   def after_signup_tasks
30
-    # Send signup email (worker)
31
-    Resque.enqueue(SendSignupMessage, self.id)
32 30
     # Add user to subscription list
33 31
     if Subscription.find_by_email(self.email) == nil
34 32
       Subscription.create(first_name: self.first_name, last_name: self.last_name, email: self.email)
35 33
     end
36
-    # Mixpanel Tracking Analytics
37
-    @analytics = Analytics.new(self.full_name)
38
-    @analytics.track_user_registration(self)
39 34
     # Save user current language
40 35
     self.update(language: I18n.locale.to_s)
41 36
   end

+ 301 - 0
app/views/user_mailer/signup_message_simple.html.erb

@@ -0,0 +1,301 @@
1
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
+<html xmlns="http://www.w3.org/1999/xhtml">
3
+	<head>
4
+        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5
+        <title>*|MC:SUBJECT|*</title>
6
+        <style type="text/css" data-roadie-ignore>
7
+			/* ---- CLIENT-SPECIFIC STYLES ---- */
8
+			
9
+			 /* Force Outlook to provide a "view in browser" message */
10
+			#outlook a{padding:0;}
11
+			 /* Force Hotmail to display emails at full width */
12
+			.ReadMsgBody{width:100%;} .ExternalClass{width:100%;}
13
+			/* Force Hotmail to display normal line spacing */
14
+			.ExternalClass, .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td, .ExternalClass div {line-height: 100%;} 
15
+			/* Prevent WebKit and Windows mobile changing default text sizes */
16
+			body, table, td, p, a, li, blockquote{-webkit-text-size-adjust:100%; -ms-text-size-adjust:100%;} 
17
+			/* Remove spacing between tables in Outlook 2007 and up */
18
+			table, td{mso-table-lspace:0pt; mso-table-rspace:0pt;} 
19
+			/* Allow smoother rendering of resized image in Internet Explorer */
20
+			img{-ms-interpolation-mode:bicubic;} 
21
+
22
+			/* ---- RESET STYLES ---- */
23
+			body{margin:0; padding:0;}
24
+			img{border:0; height:auto; line-height:100%; outline:none; text-decoration:none;}
25
+			table{border-collapse:collapse !important;}
26
+			body, .bodyTable, .bodyCell{height:100% !important; margin:0; padding:0; width:100% !important;}
27
+			
28
+			/* ---- TEMPLATE STYLES ---- */
29
+			.container, #page-header, #page-content, #page-content2, #page-footer { width: 600px; }
30
+			
31
+			
32
+			
33
+			.page-footer a {
34
+				color: #202020;
35
+				text-decoration: underline;
36
+			}
37
+			.btn:hover { background: rgba(0, 0, 0, 0.2); }
38
+			.btn.btn-success:hover { background-color: rgba(146, 188, 100, 0.25); }
39
+
40
+			/* ---- MOBILE STYLES ---- */
41
+			
42
+			@media only screen and (max-width: 480px){
43
+				
44
+				/* ---- CLIENT-SPECIFIC MOBILE STYLES ---- */
45
+				/* Prevent Webkit platforms from changing default text sizes */
46
+				body, table, td, p, a, li, blockquote{-webkit-text-size-adjust:none !important;} 
47
+				/* Prevent iOS Mail from adding padding to the body */
48
+			    	body{width:100% !important; min-width:100% !important;} 
49
+	
50
+				/* ---- MOBILE RESET STYLES ---- */
51
+				.bodyCell{padding:0px !important;}
52
+
53
+				/* ---- MOBILE STYLES ---- */
54
+		
55
+				@media only screen and (max-width: 480px){
56
+					
57
+					/* ---- MOBILE TEMPLATE STYLES ---- */
58
+					.page-header, #page-header, #page-content, #page-content2, #page-footer { width: 100%; padding-left: 25px; padding-right: 25px; margin-left: 0px; margin-right: 0px;}
59
+					.hide-mobile { display:none !important; }
60
+				}
61
+			}
62
+		</style>
63
+		
64
+		<style>
65
+		
66
+		/* ---- TEMPLATE STYLES ---- */
67
+
68
+		body, .bodyTable{
69
+			/*@editable*/ background-color:#FFFFFF;
70
+			padding: 0px; margin: 0px;
71
+		}
72
+		
73
+		h1 {
74
+			/* mission description 2: */
75
+			font-family: Avenir-Light;
76
+			font-size: 28px;
77
+			color: #000000;
78
+			line-height: 38px;
79
+			
80
+		}
81
+		
82
+		#page-header {padding-bottom: 23px;}
83
+		
84
+		.bodyTable, .bodyCell { margin: 0px; padding: 0px;}
85
+		
86
+		#body-success { background-color: #92BC64; width: 100%; margin-left: 0px; margin-right: 0px;}
87
+		#body-danger { background-color: #ED5A51; width: 100%; margin-left: 0px; margin-right: 0px;}
88
+		#body-default { background-color: #F3F3F3; width: 100%; margin-left: 0px; margin-right: 0px;}
89
+		
90
+		.cetered { text-align: center; }
91
+		
92
+		#content-main { padding-left: 10px; padding-right: 10px; }
93
+		
94
+		.page-welcome {
95
+			text-align: center;
96
+			padding-top: 30px;
97
+			padding-bottom: 60px;
98
+		}
99
+		
100
+		.page-how-to {
101
+			text-align: center;
102
+			padding-top: 30px;
103
+			padding-bottom: 10px;
104
+		}
105
+		
106
+		.page-welcome h1 {
107
+			font-family: Helvetica;
108
+			font-size: 32px;
109
+			color: #000000;
110
+			line-height: 38px;
111
+			text-align: center;
112
+			font-weight: 300;
113
+			margin-top: 45px;
114
+			margin-bottom: 32px;
115
+		}
116
+		
117
+		.page-welcome h2, .page-how-to h2 {
118
+			font-family: Helvetica;
119
+			font-size: 24px;
120
+			color: #000000;
121
+			line-height: 34px;
122
+			text-align: center;
123
+			font-weight: 300;
124
+			margin-top: 45px;
125
+			margin-bottom: 32px;
126
+		}
127
+		
128
+		.page-header h3 {
129
+			font-family: Helvetica-Light;
130
+			font-size: 28px;
131
+			color: #000000;
132
+			line-height: 38px;
133
+			font-weight: 400;
134
+			margin-top: 0px;
135
+			
136
+		}
137
+		
138
+		.page-welcome p {
139
+			font-family: Helvetica-Light;
140
+			font-size: 18px;
141
+			color: #1D1D26;
142
+			line-height: 25px;
143
+			margin-top: 0px;
144
+			text-align: center;
145
+		}
146
+		
147
+		.page-footer {
148
+			text-align: center;
149
+			margin-top: 30px;
150
+			margin-bottom: 25px;
151
+			font-family: Avenir-LightOblique;
152
+			font-size: 14px;
153
+			color: #000000;
154
+			line-height: 19px;
155
+		}
156
+		
157
+		.page-footer a {
158
+			color: #202020;
159
+			text-decoration: none;
160
+		}
161
+		
162
+		hr { border: 1px solid black; margin-bottom: 30px;}
163
+		
164
+		.btn {
165
+			display: inline-block;
166
+			padding: 4px 12px;
167
+			margin-bottom: 0;
168
+			text-align: center;
169
+			vertical-align: middle;
170
+			cursor: pointer;
171
+			background-color: transparent;
172
+			border: 2px solid #202020;
173
+			border-bottom-color: #202020;
174
+			border-radius: 4px;
175
+			text-shadow: none;
176
+			font-family: Avenir-Book;
177
+			font-style: normal;
178
+			color: #333333;
179
+			text-transform: none;
180
+			text-decoration:none;
181
+		}
182
+		
183
+		.btn.btn-success {
184
+			color: #92BC64;
185
+			border-color: #92BC64;
186
+		}
187
+		
188
+		.btn-container {
189
+			margin-bottom: 45px;
190
+			margin-top: 35px;
191
+			text-align: center;
192
+		}
193
+		
194
+		.step-box {
195
+			border: 2px solid #D4D4DE;
196
+			border-radius: 4px;
197
+			width: 300px;
198
+			height: 54px;
199
+			margin-left: auto;
200
+			margin-right: auto;
201
+			margin-bottom: 20px;
202
+		}
203
+		
204
+		.step-box .number {
205
+			font-family: Helvetica-Bold;
206
+			font-size: 40px;
207
+			color: #000000;
208
+			line-height: 53px;
209
+			width: 50px;
210
+			padding-top: 5px;
211
+
212
+		}
213
+		
214
+		.step-box .text { 
215
+			width: 250px; 
216
+			padding-top: 20px;
217
+			text-align: left;
218
+			padding-left: 5px;
219
+			font-family: Helvetica-Bold;
220
+			font-size: 14px;
221
+			color: #000000;
222
+			line-height: 19px;
223
+		}
224
+		
225
+		.copyright { 
226
+			padding-top: 15px;
227
+		}
228
+		
229
+		</style>
230
+		
231
+    </head>
232
+    <body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">
233
+        	<table align="center" border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" class="bodyTable">
234
+            	<tr>
235
+                	<td align="center" valign="top" class="bodyCell">
236
+                    	
237
+                      	<!-- BEGIN HEADER // -->
238
+                          <table border="0" cellpadding="0" cellspacing="0" width="100%" id="page-header" style="margin-bottom: 22px; margin-top: 4px">
239
+                              <tr>
240
+                                  <td valign="top" class="cetered">
241
+                                  		<img src="http://avalanche2.s3.amazonaws.com/email_files/base/avalanche2_logomark.png" style="width:174px; height: 124px;" id="logo" />
242
+                                  </td>
243
+                              </tr>
244
+                          </table>
245
+                          <!-- // END HEADER -->
246
+					
247
+                    </td>
248
+                </tr>
249
+            </table>
250
+            <table align="center" border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" class="bodyTable" id="body-default">
251
+              	<tr>
252
+                  	<td align="center" valign="top" class="bodyCell">
253
+                    	
254
+					  <!-- BEGIN BODY // -->
255
+	                      <table border="0" cellpadding="0" cellspacing="0" class="container" id="page-content">
256
+	                          <tr>
257
+	                              <td valign="top" class="centered" id="content-main">
258
+									<%= content_tag(:div, class: 'page-welcome centered') do %>
259
+										<%= content_tag(:h1, ( t 'sign_up_email.welcome')) %>
260
+										<hr>
261
+										<%= content_tag(:p, (t 'sign_up_email.welcome_text'), class: 'tagline') %>
262
+									
263
+									<% end %>
264
+									
265
+	                              </td>
266
+	                          </tr>
267
+	                      </table>
268
+	                      <!-- // END BODY -->
269
+					
270
+                      </td>
271
+                  </tr>
272
+              </table>
273
+              <table align="center" border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" class="bodyTable">
274
+              	<tr>
275
+                  	<td align="center" valign="top" class="bodyCell">
276
+                    	
277
+                       
278
+					
279
+                        	<!-- BEGIN FOOTER // -->
280
+	                      <table border="0" cellpadding="0" cellspacing="0" class="container" id="page-footer">
281
+	                          <tr>
282
+	                              <td valign="top" class="centered">
283
+								<%= content_tag(:div, class: 'page-footer') do %>
284
+									<%= link_to((t 'agent.dashboard'), url_for(controller: 'agents', action: 'dashboard', only_path: false)) %>
285
+									 | 
286
+									<%= link_to((t 'missions.missions'),  url_for(controller: 'missions', action: 'index', only_path: false)) %>
287
+									 | 
288
+									<%= link_to((t 'agent.account'),  url_for(controller: 'devise/registrations', action: 'edit', only_path: false)) %>
289
+									<br>
290
+									<span class="copyright">&copy; Avalanche Network</span>
291
+								<% end %>
292
+   	                              </td>
293
+   	                          </tr>
294
+   	                      </table>
295
+                         <!-- // END FOOTER -->
296
+					
297
+                      </td>
298
+                  </tr>
299
+              </table>
300
+    </body>
301
+</html>

+ 11 - 0
app/views/user_mailer/signup_message_simple.text.erb

@@ -0,0 +1,11 @@
1
+-----------------
2
+
3
+<%= t 'sign_up_email.welcome' %>
4
+
5
+<%= t 'sign_up_email.welcome_text' %>
6
+
7
+<%= t 'sign_up_email.start' %>
8
+<%= url_for(controller: 'agents', action: 'dashboard', only_path: false) %>
9
+
10
+-----------------
11
+Avalanche Network

+ 1 - 1
app/workers/send_signup_message.rb

@@ -8,7 +8,7 @@ class SendSignupMessage
8 8
     
9 9
     # Send Signup Email
10 10
     UserMailer.signup_message(user).deliver
11
-    
11
+
12 12
   end 
13 13
   
14 14
 end

+ 14 - 0
app/workers/send_signup_message_simple.rb

@@ -0,0 +1,14 @@
1
+class SendSignupMessageSimple
2
+  @queue = :send_signup_message_queue 
3
+  
4
+  def self.perform(user_id) 
5
+    
6
+    # Get User
7
+    user = User.find_by_id(user_id)
8
+    
9
+    # Send Signup Email
10
+    UserMailer.signup_message_simple(user).deliver
11
+
12
+  end 
13
+  
14
+end

+ 1 - 1
config/routes.rb

@@ -98,7 +98,7 @@ Avalanche2::Application.routes.draw do
98 98
     post 'login' => 'devise/sessions#create', :as => :user_session
99 99
     delete 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session
100 100
     get   'signup' => 'devise/registrations#new',    :as => :new_user_registration
101
-    post  'signup' => 'devise/registrations#create', :as => :user_registration
101
+    post  'signup' => 'users/registrations#create', :as => :user_registration
102 102
     put  'signup' => 'users/registrations#update', :as => :user_registration_update
103 103
 
104 104
     scope '/account' do

BIN
dump.rdb