Permalink
Browse files

adding way to unsubscribe from email notificiations

  • Loading branch information...
1 parent 5349b33 commit 791e3eb2603cb9c324c3599bb932c65c3123ec14 @ryanb committed Jul 25, 2011
@@ -48,6 +48,12 @@ def ban
end
end
+ def unsubscribe
+ @user = User.find_by_unsubscribe_token!(params[:token])
+ @user.update_attributes!(:email_on_reply => false)
+ redirect_to root_url, :notice => "You have been unsubscribed from further email notifications."
+ end
+
private
def load_current_user
View
@@ -6,6 +6,7 @@ def feedback(message)
def comment_response(comment, user)
@comment = comment
- mail :to => user.email, :from => "noreply@railscasts.com", :subject => "Comment Response on RailsCasts"
+ @user = user
+ mail :to => @user.email, :from => "noreply@railscasts.com", :subject => "Comment Response on RailsCasts"
end
end
View
@@ -7,7 +7,7 @@ def initialize(user)
end
can :access, :info
can :create, :feedback_messages
- can [:read, :create, :login], :users
+ can [:read, :create, :login, :unsubscribe], :users
if user
can :logout, :users
View
@@ -1,6 +1,6 @@
class User < ActiveRecord::Base
- attr_accessible :name, :email, :site_url
- before_create :generate_token
+ attr_accessible :name, :email, :site_url, :email_on_reply
+ before_create { generate_token(:token) }
has_many :comments
has_paper_trail
@@ -12,17 +12,23 @@ def self.create_from_omniauth(omniauth)
user.name = omniauth["user_info"]["name"]
user.site_url = omniauth["user_info"]["urls"]["Blog"] if omniauth["user_info"]["urls"]
user.gravatar_token = omniauth["extra"]["user_hash"]["gravatar_id"] if omniauth["extra"] && omniauth["extra"]["user_hash"]
+ user.email_on_reply = true
user.save!
end
end
- def generate_token
- if token.blank?
- characters = ('a'..'z').to_a + ('A'..'Z').to_a + ('1'..'9').to_a
- begin
- self.token = Array.new(32) { characters.sample }.join
- end while self.class.exists?(:token => token)
+ def generated_unsubscribe_token
+ if unsubscribe_token.blank?
+ generate_token(:unsubscribe_token)
+ save!
end
+ unsubscribe_token
+ end
+
+ def generate_token(column)
+ begin
+ self[column] = SecureRandom.urlsafe_base64
+ end while User.exists?(column => self[column])
end
def display_name
@@ -7,3 +7,6 @@ From: <%= raw @comment.user.name %>
---
To view the full comment: <%= episode_url(@comment.episode, :view => "comments") %>
+
+To unsubscribe from future emails from RailsCasts, just click this link:
+<%= unsubscribe_url(@user.generated_unsubscribe_token) %>
@@ -4,17 +4,21 @@
<%= form_for @user do |f| %>
<%= f.error_messages %>
<p>
- <%= f.label :name %><br />
+ <%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
- <%= f.label :email %><br />
+ <%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
- <%= f.label :site_url %><br />
+ <%= f.label :site_url, "Site URL" %>
<%= f.text_field :site_url %>
</p>
+ <p>
+ <%= f.label :email_on_reply, "Receive email when a user replies to your comment?" %>
+ <%= f.check_box :email_on_reply %>
+ </p>
<p><%= f.submit "Update Profile" %></p>
<% end %>
</div>
View
@@ -9,6 +9,7 @@
match "logout" => "users#logout", :as => "logout"
match "feedback" => "feedback_messages#new", :as => "feedback"
match "episodes/archive" => redirect("/?view=list")
+ match 'unsubscribe/:token' => 'users#unsubscribe', :as => "unsubscribe"
post "versions/:id/revert" => "versions#revert", :as => "revert_version"
resources :users do
@@ -0,0 +1,11 @@
+class AddEmailOnReplyToUsers < ActiveRecord::Migration
+ def self.up
+ add_column :users, :email_on_reply, :boolean, :default => false, :null => false
+ add_column :users, :unsubscribe_token, :string
+ end
+
+ def self.down
+ remove_column :users, :unsubscribe_token
+ remove_column :users, :email_on_reply
+ end
+end
View
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110701025738) do
+ActiveRecord::Schema.define(:version => 20110725215614) do
create_table "comments", :force => true do |t|
t.integer "episode_id"
@@ -83,8 +83,10 @@
t.datetime "created_at"
t.datetime "updated_at"
t.string "github_uid"
- t.boolean "moderator", :default => false, :null => false
+ t.boolean "moderator", :default => false, :null => false
t.datetime "banned_at"
+ t.boolean "email_on_reply", :default => false, :null => false
+ t.string "unsubscribe_token"
end
create_table "versions", :force => true do |t|
View
@@ -21,6 +21,7 @@
f.sequence(:github_username) { |n| "foo#{n}" }
f.sequence(:github_uid) { |n| n }
f.sequence(:email) { |n| "foo#{n}@example.com" }
+ f.email_on_reply true
end
Factory.define :feedback_message do |f|
@@ -24,6 +24,7 @@
mail.from.should eq(["noreply@railscasts.com"])
mail.body.encoded.should include(comment.content)
mail.body.encoded.should include(episode_url(comment.episode, :view => "comments"))
+ mail.body.encoded.should include(unsubscribe_url(user.generated_unsubscribe_token))
end
end
end
@@ -11,6 +11,7 @@
@ability.should be_able_to(:login, :users)
@ability.should be_able_to(:show, :users)
@ability.should be_able_to(:create, :users)
+ @ability.should be_able_to(:unsubscribe, :users)
@ability.should_not be_able_to(:update, :users)
end
View
@@ -19,6 +19,15 @@
user.name.should == "Bar"
user.gravatar_token.should == "avatar"
user.site_url.should == "customsite"
+ user.email_on_reply.should be_true
user
end
+
+ it "should generate persistant unsubscribe_token" do
+ user = Factory(:user)
+ user.unsubscribe_token.should be_nil
+ token = user.generated_unsubscribe_token
+ user.reload.unsubscribe_token.should eq(token)
+ user.generated_unsubscribe_token.should eq(token)
+ end
end
@@ -16,9 +16,11 @@
page.should have_content("This is your profile.")
click_on "Edit Profile"
fill_in "Name", :with => "Leonardo"
+ uncheck "user_email_on_reply"
click_on "Update Profile"
page.should have_content("Successfully updated profile")
page.should have_content("Leonardo")
+ user.reload.email_on_reply.should be_false
end
it "logs out current user" do
@@ -55,4 +57,11 @@
bad_user.reload.should be_banned
bad_user.comments.size.should == 0
end
+
+ it "unsubscribe a user from comment replies" do
+ user = Factory(:user)
+ visit unsubscribe_path(user.generated_unsubscribe_token)
+ page.should have_content("unsubscribed")
+ user.reload.email_on_reply.should be_false
+ end
end

0 comments on commit 791e3eb

Please sign in to comment.