From 882b92555bfa06ff32c4e11410c796910bfd9915 Mon Sep 17 00:00:00 2001 From: Lucas Bibiano Date: Thu, 27 Oct 2016 18:02:17 -0200 Subject: [PATCH] Add unsubscribe option --- app/controllers/subscribers_controller.rb | 18 +++++++++++++++++ app/mailers/unsubscribe_mailer.rb | 9 +++++++++ app/services/unsubscribe_mail_sender.rb | 8 ++++++++ app/views/posts/new.html.erb | 9 +++++++++ .../unsubscribe_email.html.erb | 20 +++++++++++++++++++ .../unsubscribe_email.text.erb | 1 + config/environments/development.rb | 1 + config/environments/production.rb | 2 ++ config/routes.rb | 3 +++ ...27185334_add_delete_token_to_subscriber.rb | 5 +++++ db/schema.rb | 9 +++++---- 11 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 app/mailers/unsubscribe_mailer.rb create mode 100644 app/services/unsubscribe_mail_sender.rb create mode 100644 app/views/unsubscribe_mailer/unsubscribe_email.html.erb create mode 100644 app/views/unsubscribe_mailer/unsubscribe_email.text.erb create mode 100644 db/migrate/20161027185334_add_delete_token_to_subscriber.rb diff --git a/app/controllers/subscribers_controller.rb b/app/controllers/subscribers_controller.rb index b646a30..e8121af 100644 --- a/app/controllers/subscribers_controller.rb +++ b/app/controllers/subscribers_controller.rb @@ -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 + subscriber = Subscriber.find_by(delete_token: params[:delete_token]) + subscriber.destroy + + redirect_to new_post_path, notice: 'Subscription canceled!' + end + private def subscriber_params diff --git a/app/mailers/unsubscribe_mailer.rb b/app/mailers/unsubscribe_mailer.rb new file mode 100644 index 0000000..91f9188 --- /dev/null +++ b/app/mailers/unsubscribe_mailer.rb @@ -0,0 +1,9 @@ +class UnsubscribeMailer < ApplicationMailer + default from: 'your-weekly@test.com' + + def unsubscribe_email(subscriber) + @subscriber = subscriber + + mail(subject: "Your weekly unsubscribe url", to: @subscriber.email) + end +end diff --git a/app/services/unsubscribe_mail_sender.rb b/app/services/unsubscribe_mail_sender.rb new file mode 100644 index 0000000..4a17015 --- /dev/null +++ b/app/services/unsubscribe_mail_sender.rb @@ -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 diff --git a/app/views/posts/new.html.erb b/app/views/posts/new.html.erb index 2c9027c..a043cb7 100644 --- a/app/views/posts/new.html.erb +++ b/app/views/posts/new.html.erb @@ -7,4 +7,13 @@ <%= link_to new_subscriber_path do %>

Not subscribed yet?

<% end %> +
+
+
+ +

Unsubscribe : (

+ <%= form_for :unsubscribe, url: unsubscribe_path, method: :delete do |f| %> + <%= f.text_field :email, placeholder: 'Your email' %> + <%= f.submit 'Unsubscribe' %> + <% end %> diff --git a/app/views/unsubscribe_mailer/unsubscribe_email.html.erb b/app/views/unsubscribe_mailer/unsubscribe_email.html.erb new file mode 100644 index 0000000..c4e5081 --- /dev/null +++ b/app/views/unsubscribe_mailer/unsubscribe_email.html.erb @@ -0,0 +1,20 @@ + + + + + + +
+
+ + Unsubscribe url: + <%= link_to 'Unsubscribe', cancel_subscription_url(@subscriber.delete_token) %> +
+
+ + diff --git a/app/views/unsubscribe_mailer/unsubscribe_email.text.erb b/app/views/unsubscribe_mailer/unsubscribe_email.text.erb new file mode 100644 index 0000000..17b23ee --- /dev/null +++ b/app/views/unsubscribe_mailer/unsubscribe_email.text.erb @@ -0,0 +1 @@ +<%= cancel_subscription_url(@subscriber.delete_token) %> diff --git a/config/environments/development.rb b/config/environments/development.rb index c7a3bbb..19ab6b4 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -57,6 +57,7 @@ Bullet.rails_logger = true end + config.action_mailer.default_url_options = { :host => "http://localhost:3000" } config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'localhost', port: 1025 } diff --git a/config/environments/production.rb b/config/environments/production.rb index d8b79b3..14996cf 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -92,4 +92,6 @@ :domain => 'heroku.com', :enable_starttls_auto => true } + + config.action_mailer.default_url_options = { :host => "your-weekly-production.herokuapp.com" } end diff --git a/config/routes.rb b/config/routes.rb index aac9d9c..8a3b185 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,5 +2,8 @@ resources :posts, only: [:new, :create, :index] resources :subscribers, only: [:new, :create, :index] + delete '/subscribers', to: 'subscribers#unsubscribe', as: :unsubscribe + get '/cancel_subscription/:delete_token', to: 'subscribers#cancel_subscription', as: :cancel_subscription + root to: 'posts#new' end diff --git a/db/migrate/20161027185334_add_delete_token_to_subscriber.rb b/db/migrate/20161027185334_add_delete_token_to_subscriber.rb new file mode 100644 index 0000000..ab20e79 --- /dev/null +++ b/db/migrate/20161027185334_add_delete_token_to_subscriber.rb @@ -0,0 +1,5 @@ +class AddDeleteTokenToSubscriber < ActiveRecord::Migration[5.0] + def change + add_column :subscribers, :delete_token, :string, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index b22f9f6..ef647b2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160804191849) do +ActiveRecord::Schema.define(version: 20161027185334) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -26,9 +26,10 @@ end create_table "subscribers", force: :cascade do |t| - t.string "email", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.string "email", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "delete_token" end end