I built a gem that makes it easy to automatically unsubscribe from emails in Rails.

Source Code

https://github.com/stevepolitodesign/unsubscribe

Demo

Docs

? Unsubscribe

Automatically unsubscribe from emails in Rails.

? Installation

Add this line to your application’s Gemfile:


This content originally appeared on DEV Community and was authored by Steve Polito

Source Code

https://github.com/stevepolitodesign/unsubscribe

Demo

Demo

Docs

? Unsubscribe

Automatically unsubscribe from emails in Rails.

? Installation

Add this line to your application's Gemfile:

gem 'unsubscribe'

And then execute:

$ bundle

Or install it yourself as:

$ gem install unsubscribe

Then run the installation commands:

rails g unsubscribe:install
rails unsubscribe:install:migrations
rails db:migrate

? Usage

Unsubscribe::Owner

  • Add include Unsubscribe::Owner to a Model. The Model must have an email column.
class User < ApplicationRecord
  include Unsubscribe::Owner
end
Available Methods
User.first.mailer_subscriptions
# => #<ActiveRecord::Associations::CollectionProxy [#<Unsubscribe::MailerSubscription>, #<Unsubscribe::MailerSubscription>] >

User.first.subscribed_to_mailer? "MarketingMailer"
# => true/false

User.first.to_sgid_for_mailer_subscription
# => #<SignedGlobalID:123 ...>

Unsubscribe::Mailer

  • Add include Unsubscribe::Mailer to a Mailer.
  • Optionally call unsubscribe_settings to set a name and description. This will be used in the unsubscribe page.
  • Set mail to: to @recipient.email. The @recipient is an instance of whatever Class include Unsubscribe::Owner was added to.
class MarketingMailer < ApplicationMailer  
  include Unsubscribe::Mailer

  unsubscribe_settings name: "Marketing Emails", description: "Updates on promotions and sales."

  def promotion
    mail to: @recipient.email
  end  
end
  • Call the Mailer with a recipient parameter.
  MarketingMailer.with(
    recipient: User.first
  ).promotion.deliver_now

Available Methods

Unsubscribe::MailerSubscription.first.action
# => "Unsubscribe from"/"Subscribe to"

Unsubscribe::MailerSubscription.first.call_to_action
# => "Unsubscribe from Marketing Emails"/"Subscribe to Marketing Emails"

Unsubscribe::MailerSubscription.first.description
# => "Updates on promotions and sales."

Unsubscribe::MailerSubscription.first.name
# => "Marketing Emails"

Unsubscribe Link

  • Add the @unsubscribe_url link to the Mailer.
<%= link_to "Unsubscribe", @unsubscribe_url %>

⚙️ Customize Templates

Run rails g unsubscribe:views if you want to modify the existing templates.

? I18n

The language used for Unsubscribe::MailerSubscription#action can be translated.

# config/locales/en.yml
en:
  unsubscribe:
    action:
      subscribe: "Subscribe to"
      unsubscribe: "Unsubscribe from"

? Contributing

If you'd like to open a PR please make sure the following things pass:

bin/rails test
bundle exec standardrb

? License

The gem is available as open source under the terms of the MIT License.


This content originally appeared on DEV Community and was authored by Steve Polito


Print Share Comment Cite Upload Translate Updates
APA

Steve Polito | Sciencx (2021-09-28T00:31:18+00:00) I built a gem that makes it easy to automatically unsubscribe from emails in Rails.. Retrieved from https://www.scien.cx/2021/09/28/i-built-a-gem-that-makes-it-easy-to-automatically-unsubscribe-from-emails-in-rails/

MLA
" » I built a gem that makes it easy to automatically unsubscribe from emails in Rails.." Steve Polito | Sciencx - Tuesday September 28, 2021, https://www.scien.cx/2021/09/28/i-built-a-gem-that-makes-it-easy-to-automatically-unsubscribe-from-emails-in-rails/
HARVARD
Steve Polito | Sciencx Tuesday September 28, 2021 » I built a gem that makes it easy to automatically unsubscribe from emails in Rails.., viewed ,<https://www.scien.cx/2021/09/28/i-built-a-gem-that-makes-it-easy-to-automatically-unsubscribe-from-emails-in-rails/>
VANCOUVER
Steve Polito | Sciencx - » I built a gem that makes it easy to automatically unsubscribe from emails in Rails.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/28/i-built-a-gem-that-makes-it-easy-to-automatically-unsubscribe-from-emails-in-rails/
CHICAGO
" » I built a gem that makes it easy to automatically unsubscribe from emails in Rails.." Steve Polito | Sciencx - Accessed . https://www.scien.cx/2021/09/28/i-built-a-gem-that-makes-it-easy-to-automatically-unsubscribe-from-emails-in-rails/
IEEE
" » I built a gem that makes it easy to automatically unsubscribe from emails in Rails.." Steve Polito | Sciencx [Online]. Available: https://www.scien.cx/2021/09/28/i-built-a-gem-that-makes-it-easy-to-automatically-unsubscribe-from-emails-in-rails/. [Accessed: ]
rf:citation
» I built a gem that makes it easy to automatically unsubscribe from emails in Rails. | Steve Polito | Sciencx | https://www.scien.cx/2021/09/28/i-built-a-gem-that-makes-it-easy-to-automatically-unsubscribe-from-emails-in-rails/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.