-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unsubscribe option #5
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,24 @@ def create | |
end | ||
end | ||
|
||
def unsubscribe | ||
subscriber = Subscriber.find_by(email: params[:unsubscribe][:email]) | ||
|
||
if subscriber | ||
UnsubscribeMailSender.call(subscriber) | ||
redirect_to new_post_path, notice: 'Check your email to find your unsubscribe url!' | ||
else | ||
redirect_to new_post_path, notice: 'Email not found!' | ||
end | ||
end | ||
|
||
def cancel_subscription | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a better name here would be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, i felt the same thing about the naming |
||
subscriber = Subscriber.find_by(delete_token: params[:delete_token]) | ||
subscriber.destroy | ||
|
||
redirect_to new_post_path, notice: 'Subscription canceled!' | ||
end | ||
|
||
private | ||
|
||
def subscriber_params | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class UnsubscribeMailer < ApplicationMailer | ||
default from: '[email protected]' | ||
|
||
def unsubscribe_email(subscriber) | ||
@subscriber = subscriber | ||
|
||
mail(subject: "Your weekly unsubscribe url", to: @subscriber.email) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class UnsubscribeMailSender | ||
include Service | ||
|
||
def call(subscriber) | ||
subscriber.update_attribute(:delete_token, SecureRandom.hex) | ||
UnsubscribeMailer.unsubscribe_email(subscriber).deliver_now | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,13 @@ | |
<%= link_to new_subscriber_path do %> | ||
<h2>Not subscribed yet?</h2> | ||
<% end %> | ||
<hr> | ||
<hr> | ||
<hr> | ||
|
||
<h3>Unsubscribe : (</h2> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
could you please remove the blank space before the |
||
<%= form_for :unsubscribe, url: unsubscribe_path, method: :delete do |f| %> | ||
<%= f.text_field :email, placeholder: 'Your email' %> | ||
<%= f.submit 'Unsubscribe' %> | ||
<% end %> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' /> | ||
</head> | ||
<body style="font-family: open sans, helvetica neue, helvetica, arial; color: #414040"> | ||
<div style="text-align: center"> | ||
<div style="margin-bottom: 2em; | ||
border: 1px solid #dedbdb; | ||
width: 60%; | ||
margin-left: 20%; | ||
padding: .5em 1.5em; | ||
text-align: left;"> | ||
|
||
Unsubscribe url: | ||
<%= link_to 'Unsubscribe', cancel_subscription_url(@subscriber.delete_token) %> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= cancel_subscription_url(@subscriber.delete_token) %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,4 +92,6 @@ | |
:domain => 'heroku.com', | ||
:enable_starttls_auto => true | ||
} | ||
|
||
config.action_mailer.default_url_options = { :host => "your-weekly-production.herokuapp.com" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this? The app already works fine today. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's for the link_to, it needs the host parameter to build the url |
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddDeleteTokenToSubscriber < ActiveRecord::Migration[5.0] | ||
def change | ||
add_column :subscribers, :delete_token, :string, index: true | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please extract this action and the
cancel_subscription
to another controller? This one is turning a little cumbersome.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like an unsubscribe controller?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup