AJAX email subscription and other fixes

jamesperet vor 10 Jahren
Ursprung
Commit
800f0e4b9c

+ 1 - 0
Gemfile

@@ -64,6 +64,7 @@ gem 'gibbon'
64 64
 gem 'redis'
65 65
 gem 'resque', '~> 1.22.0', :require => "resque/server"
66 66
 gem 'mixpanel-ruby'
67
+gem 'valid_email', :require => 'valid_email/email_validator'
67 68
 
68 69
 group :development do
69 70
   gem "rename"

+ 4 - 2
Gemfile.lock

@@ -119,7 +119,6 @@ GEM
119 119
     formatador (0.2.5)
120 120
     friendly_id (5.0.4)
121 121
       activerecord (>= 4.0.0)
122
-    geocoder (1.2.6)
123 122
     gherkin (2.12.2)
124 123
       multi_json (~> 1.3)
125 124
     gibbon (1.1.4)
@@ -287,6 +286,9 @@ GEM
287 286
       kgio (~> 2.6)
288 287
       rack
289 288
       raindrops (~> 0.7)
289
+    valid_email (0.0.7)
290
+      activemodel
291
+      mail
290 292
     vegas (0.1.11)
291 293
       rack (>= 1.0.0)
292 294
     warden (1.2.3)
@@ -315,7 +317,6 @@ DEPENDENCIES
315 317
   fog
316 318
   font-awesome-rails
317 319
   friendly_id (~> 5.0.0)
318
-  geocoder
319 320
   gibbon
320 321
   jasny_bootstrap_extension_rails
321 322
   jbuilder (~> 1.2)
@@ -345,3 +346,4 @@ DEPENDENCIES
345 346
   twitter-bootstrap-rails
346 347
   uglifier (>= 1.3.0)
347 348
   unicorn
349
+  valid_email

BIN
app/assets/images/favicon.ico


+ 33 - 0
app/assets/javascripts/start.js

@@ -18,6 +18,39 @@ function attachHandler(jQuery) {
18 18
 	
19 19
 	$('.popup').popover('toogle')
20 20
 	
21
+	$('.new_subscription').submit(function() {  
22
+	    var valuesToSubmit = $(this).serialize();
23
+	    $.ajax({
24
+	        type: "POST",
25
+	        url: $(this).attr('action'),
26
+	        data: valuesToSubmit,
27
+		   dataType: "json",
28
+		   async: true,
29
+ 		   error: function(request, textStatus, errorThrown){
30
+    		   		msg = '<div class="alert alert-error span5 subscribe-alert"><h4>' + request.status + " " + errorThrown + '</h4></div>'
31
+		   		$('.new_subscription').html(msg);     
32
+ 		   }, 
33
+		   success: function(data){
34
+			   $('.subscribe-alert').remove();
35
+			   switch(data.status) {
36
+			       case 'success':
37
+					 msg = '<div class="alert alert-success subscribe-alert"><h4>' + data.success + '</h4></div>';
38
+					 $('.new_subscription').html(msg); 
39
+			           break;
40
+			       case 'error':
41
+					 msg = '<div class="alert alert-success subscribe-alert"><h4>' + data.error + '</h4></div>';
42
+					 $('.new_subscription').html(msg); 
43
+			           break;
44
+		       	  case 'invalid':
45
+					  msg = '<div class="alert alert-block subscribe-alert"><h4>' + data.invalid + '</h4></div>';
46
+					  $('.new_subscription').prepend(msg);
47
+		           	 break;
48
+			   }      
49
+		   }
50
+	    });
51
+	    return false;
52
+	});
53
+	
21 54
 	// on scroll, 
22 55
 	$(window).on('scroll',function(){
23 56
 

+ 5 - 0
app/assets/stylesheets/start.css.less

@@ -235,6 +235,11 @@ background-color: #444;
235 235
 color: white;
236 236
 }
237 237
 
238
+.subscribe-alert {
239
+	margin-left: 0px;
240
+	width: 560px;
241
+}
242
+
238 243
 @media (max-width: 979px) {
239 244
 	h2.landing_page {
240 245
 		font-size: 26px;

+ 9 - 3
app/controllers/subscription_controller.rb

@@ -1,17 +1,23 @@
1
+require 'valid_email'
2
+
1 3
 class SubscriptionController < ApplicationController
2 4
   
3 5
   def create
4 6
     @subscription = Subscription.new(subscription_params)
7
+    @subscribe_status = 'error'
5 8
     respond_to do |format|
6
-      if Subscription.find_by_email(@subscription.email) == nil 
9
+      if Subscription.find_by_email(@subscription.email) == nil && @subscription.valid?
7 10
         if @subscription.save
11
+          @subscribe_status = 'success'
8 12
           format.html { redirect_to root_path, notice: 'Thanks for subscribing to our newsletter' }
9
-          format.json { render action: 'show', status: :created, location: @subscription }
13
+          format.json { render action: 'show', status: :created }
10 14
         else
15
+          @subscribe_status = 'error'
11 16
           format.html { redirect_to root_path, alert: 'An error ocured. Please try gain.' }
12
-          format.json { render json: @subscription.errors, status: :unprocessable_entity }
17
+          format.json { render action: 'show', status: :created, location: @subscription }
13 18
         end     
14 19
       else
20
+        @subscribe_status = 'invalid'
15 21
         format.html { redirect_to root_path, alert: 'You have already registered to our newsletter' }
16 22
         format.json { render action: 'show', status: :created, location: @subscription }
17 23
       end      

+ 2 - 0
app/models/subscription.rb

@@ -1,5 +1,7 @@
1 1
 class Subscription < ActiveRecord::Base
2 2
   
3
+  validates :email, :presence => true, :email => true
4
+  
3 5
   after_create do
4 6
     subscribe_to_mailchimp
5 7
     send_newsletter_subscription_email

+ 3 - 1
app/views/layouts/landing_page.erb

@@ -35,6 +35,8 @@
35 35
     <%= favicon_link_tag 'favicon.ico', :rel => 'shortcut icon' %>
36 36
 
37 37
     <%= javascript_include_tag "application" %>
38
+    
39
+    <%= favicon_link_tag 'favicon.ico' %>
38 40
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
39 41
 
40 42
     <script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.js'></script>
@@ -103,7 +105,7 @@
103 105
 
104 106
     <div>
105 107
 
106
-            <%= bootstrap_flash %>
108
+            
107 109
             <%= yield %>
108 110
 
109 111
 

+ 4 - 4
app/views/start/index.html.erb

@@ -239,10 +239,10 @@
239 239
 			         <p class="lead header  ">
240 240
 					    <%= (t "landing_page.subscribe_text").html_safe %>
241 241
 				    </p>
242
-				    <%= bootstrap_form_for(@subscription) do |f| %>
242
+				    <%= bootstrap_form_for(@subscription, remote: true) do |f| %>
243 243
 				      <%= f.alert_message "Please fix the errors below."%>
244 244
 				      <%= f.form_group :email, style: 'margin-top: -35px;' do %>
245
-				        	<%= f.text_field :email, hide_label: true, value: 'Email', class: 'span4 input-large', required: true, style: 'height: 30px; font-size: 24px;' %>
245
+				        	<%= f.text_field :email, hide_label: true, placeholder: 'Email', class: 'span4 input-large', required: true, style: 'height: 30px; font-size: 24px; padding-top: 10px;' %>
246 246
 				      <% end %>
247 247
 				      <%= f.submit (t 'subscription.submit'), class: 'btn btn-large', id: 'submit_subscription' %>
248 248
 				    <% end %>
@@ -279,10 +279,10 @@
279 279
     <p class="lead" >
280 280
 	    <%= (t "landing_page.subscribe_text").html_safe %>
281 281
     </p>
282
-    <%= bootstrap_form_for(@subscription) do |f| %>
282
+    <%= bootstrap_form_for(@subscription, remote: true) do |f| %>
283 283
       <%= f.alert_message "Please fix the errors below."%>
284 284
       <%= f.form_group :email, style: 'margin-top: -35px;' do %>
285
-        	<%= f.text_field :email, hide_label: true, value: 'Email', class: 'span4', required: true, style: 'height: 50px; font-size: 24px;' %>
285
+        	<%= f.text_field :email, hide_label: true, placeholder: 'Email', class: 'span4', required: true, style: 'height: 50px; font-size: 24px; padding-top: 5px;' %>
286 286
       <% end %>
287 287
       <%= f.submit (t 'subscription.submit'), class: 'btn btn-large', id: 'submit_subscription' %>
288 288
     <% end %>

+ 6 - 0
app/views/subscription/show.json.erb

@@ -0,0 +1,6 @@
1
+{  
2
+   "status": "<%= @subscribe_status %>",
3
+   "success": "<%= t 'subscription.success' %>",
4
+   "failure": "<%= t 'subscription.failure' %>",
5
+   "invalid": "<%= t 'subscription.invalid' %>"
6
+}

+ 4 - 1
config/locales/en.yml

@@ -301,4 +301,7 @@ en:
301 301
     subscribers: Subscribers
302 302
     header: Subscribe to our newsletter
303 303
     submit: Subscribe
304
-    export: Export CVS
304
+    export: Export CVS
305
+    success: 'Thanks for subscribing to our newsletter!'
306
+    failure: 'An error ocured! Please try again later.'
307
+    invalid: 'The email you entered is not valid.'

+ 4 - 1
config/locales/pt-BR.yml

@@ -305,4 +305,7 @@ pt-BR:
305 305
     subscribers: Assinantes
306 306
     header: Receba nossa newsletter
307 307
     submit: Enviar
308
-    export: Exportart CVS
308
+    export: Exportart CVS
309
+    success: 'Obrigado por se cadastrar na nossa newsletter!'
310
+    failure: 'Ocorreu um erro. Por favor tente de novo mais tarde.'
311
+    invalid: 'O endereço de email não é valido.'

+ 0 - 6
config/unicorn.rb

@@ -6,13 +6,10 @@ timeout 30
6 6
 preload_app true
7 7
  
8 8
 before_fork do |server, worker|
9
-  # Replace with MongoDB or whatever
10 9
   if defined?(ActiveRecord::Base)
11 10
     ActiveRecord::Base.connection.disconnect!
12 11
     Rails.logger.info('Disconnected from ActiveRecord')
13 12
   end
14
- 
15
-  # If you are using Redis but not Resque, change this
16 13
   if defined?(Resque)
17 14
     Resque.redis.quit
18 15
     Rails.logger.info('Disconnected from Redis')
@@ -20,13 +17,10 @@ before_fork do |server, worker|
20 17
 end
21 18
  
22 19
 after_fork do |server, worker|
23
-  # Replace with MongoDB or whatever
24 20
   if defined?(ActiveRecord::Base)
25 21
     ActiveRecord::Base.establish_connection
26 22
     Rails.logger.info('Connected to ActiveRecord')
27 23
   end
28
- 
29
-  # If you are using Redis but not Resque, change this
30 24
   if defined?(Resque)
31 25
     Resque.redis = REDIS_WORKER
32 26
     @resque_pid ||= spawn("bundle exec rake resque:work")

BIN
public/favicon.ico