Permalink
Browse files

added unsubscribe link to templates, and corresponding subscribers co…

…ntroller with edit and update actions
  • Loading branch information...
1 parent c994420 commit 737b3c9273abd6ed9620773b01ef65b89688989e @zawaideh zawaideh committed Apr 20, 2012
View
@@ -26,8 +26,6 @@ load 'rails/tasks/engine.rake'
Bundler::GemHelper.install_tasks
-# require 'rake/testtask'
-
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,14 @@
+module Maktoub
+ class SubscribersController < ApplicationController
+
+ def edit
+
+ end
+
+ def update
+ Maktoub.unsubscribe params[:email]
+ end
+
+ end
+end
+
@@ -5,10 +5,19 @@
<%= stylesheet_link_tag "maktoub/application" %>
<%= javascript_include_tag "maktoub/application" %>
<%= csrf_meta_tags %>
+ <style type="text/css">
+ <%= render 'layouts/maktoub/styles' %>
+ div.container {
+ width: 600px;
+ margin: auto;
+ margin-top: 50px;
+ }
+ </style>
</head>
<body>
-
-<%= yield %>
+ <div class="container">
+ <%= yield %>
+ </div>
</body>
</html>
@@ -8,7 +8,7 @@
<meta name="viewport" content="width=680px, initial-scale=0.5">
<title><%= @subject %></title>
<style type="text/css">
- <%= render 'layouts/maktoub/styles.erb' %>
+ <%= render 'layouts/maktoub/styles' %>
</style>
</head>
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">
@@ -121,14 +121,14 @@
<tr>
<td valign="top" width="350">
<div mc:edit="std_footer">
- <em>Copyright &copy; <%= Time.now.year %> Sandglaz, All rights reserved.</em>
+ <em>Copyright &copy; <%= Time.now.year %> <%= Maktoub.app_name %>, All rights reserved.</em>
<br />
<em>You are receiving this email because you opted in at our website <%= link_to Maktoub.home_url, Maktoub.home_url, :target => "_blank" %>.</em>
<br />
<%- unless @archive %>
<%# we are not including mailing addresses %>
<%#<br /><strong>Our mailing address is:</strong><br /> %>
- <%# 928 Greenwood Ave, Toronto ON, M4J4C3 Canada %>
+ <%# %>
<%- end %>
</div>
</td>
@@ -137,7 +137,7 @@
<tr>
<td colspan="2" valign="middle" id="utility">
<div>
- &nbsp;<%= link_to 'update subscription preferences', Maktoub.subscription_preferences_url, :target => "_blank" %>&nbsp;
+ &nbsp;<%= link_to 'update subscription preferences', Maktoub.subscription_preferences_url, :target => "_blank" %>&nbsp; | <%= link_to 'unsubscribe', edit_subscribers_url, :target => "_blank", :rel => "nofollow" %>
</div>
</td>
</tr>
@@ -0,0 +1,13 @@
+<h1>Unsubscribe from newsletter</h1>
+
+<%= form_tag subscribers_path, :method => :put do |f| %>
+ <p>
+ <%= label :subscribers, 'Email', :for => 'email' %>
+ <br/>
+ <%= text_field :subscribers, 'email', {:type => "email", :id => 'email'} %>
+ <br />
+ </p>
+ <p>
+ <%= button_tag 'submit', :type => 'submit' %>
+ </o>
+<% end %>
@@ -0,0 +1 @@
+<h1>Unsubscribe successful</h1>
View
@@ -3,6 +3,8 @@
get "newsletter/(:newsletter)" => "archives#show", as: "newsletter"
root to: "archives#show"
+
+ resource :subscribers, :only => [:edit, :update]
end
View
@@ -34,7 +34,6 @@ def unsubscribe(email)
subscribers.select do |s|
s.send(email_field) == email
end.each do |s|
- subscribers.delete s
s.send(unsubscribe_method) if unsubscribe_method
end
end
@@ -0,0 +1,43 @@
+require 'spec_helper'
+
+describe Maktoub::SubscribersController do
+ describe "GET 'edit'" do
+ it "should get show" do
+ get :edit, :use_route => 'maktoub'
+ assert_response :success
+ assert_template 'subscribers/edit'
+ end
+ end
+
+ describe "PUT 'update'" do
+ before(:each) do
+ @subscribers = (1..5).map do |i|
+ Subscriber.new(name: "Subscriber #{i}", email: "subscriber#{i}@example.com", newsletter: true)
+ end
+ Maktoub.subscribers_extractor do
+ @subscribers.select { |s| s.newsletter }
+ end
+ end
+ it "should be successful" do
+ put :update, :use_route => 'maktoub', :email => 'subscriber2@example.com'
+ response.should be_success
+ end
+
+ it "should remove the subscriber for the returned list" do
+ put :update, :use_route => 'maktoub', :email => 'subscriber2@example.com'
+ Maktoub.subscribers.each do |s|
+ s.email.should_not == 'subscriber2@example.com'
+ end
+ end
+
+ it "should still be successful when email doesn't exist" do
+ put :update, :use_route => 'maktoub', :email => 'blablabla@example.com'
+ response.should be_success
+ end
+
+ it "should render subscribers/unsubscribe_successful" do
+ put :update, :use_route => 'maktoub', :email => 'subscriber2@example.com'
+ response.should render_template 'subscribers/update'
+ end
+ end
+end
@@ -1,5 +1,11 @@
require "ostruct"
+class Subscriber < OpenStruct
+ def unsubscribe
+ self.newsletter = false
+ end
+end
+
Maktoub.from = "Test Sender <maktoub@example.com>"
Maktoub.twitter_url = "http://twitter.com/#!/twitter"
Maktoub.facebook_url = "http://www.facebook.com/facebook"
@@ -8,12 +14,13 @@
Maktoub.logo = "logo.jpg"
Maktoub.home_domain = "example.com"
Maktoub.app_name = "Dummy application"
+Maktoub.unsubscribe_method = 'unsubscribe'
+
+USERS = (1..5).map do |i|
+ Subscriber.new({name: "tester#{i}", email: "test#{i}@example.com", newsletter: true})
+end
Maktoub.subscribers_extractor do
- users = []
- (1..5).each do |i|
- users << OpenStruct.new({name: "tester#{i}", email: "test#{i}@example.com"})
- end
- users
+ USERS.select { |u| u.newsletter }
end
View
@@ -12,21 +12,13 @@
Maktoub.subscribers.length.should == 2
end
- it "unsubscribe_method undefined" do
+ it "unsubscribe_method" do
users = (1..3).map do |i| OpenStruct.new({ :name => "hello #{i}", :email => "hello#{i}@example.com"}) end
Maktoub.subscribers_extractor { users }
+ Maktoub.unsubscribe_method = :dummy_unsubscribe
Maktoub.subscribers.length.should == 3
+ OpenStruct.any_instance.should_receive(:dummy_unsubscribe)
Maktoub.unsubscribe "hello2@example.com"
- Maktoub.subscribers.length.should == 2
- end
-
- it "unsubscribe_method set" do
- users = (1..3).map do |i| OpenStruct.new({ :name => "hello #{i}", :email => "hello#{i}@example.com"}) end
- Maktoub.subscribers_extractor { users }
- Maktoub.unsubscribe_method = :dummy_key
- Maktoub.subscribers.length.should == 3
- Maktoub.unsubscribe "hello2@example.com"
- Maktoub.subscribers.length.should == 2
end
it "email_field default value" do

0 comments on commit 737b3c9

Please sign in to comment.