An Interest In:
Web News this Week
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
September 28, 2021 12:31 am GMT
Original Link: https://dev.to/stevepolitodesign/i-built-a-gem-that-makes-it-easy-to-automatically-unsubscribe-from-emails-in-rails-345o
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:
gem 'unsubscribe'
And then execute:
$ bundle
Or install it yourself as:
$ gem install unsubscribe
Then run the installation commands:
rails g unsubscribe:installrails unsubscribe:install:migrationsrails db:migrate
Usage
Unsubscribe::Owner
- Add
include Unsubscribe::Owner
to aModel
. TheModel
must have anemail
column.
class User < ApplicationRecord include Unsubscribe::Ownerend
Available Methods
User.first.mailer_subscriptions# => #<ActiveRecord::Associations::CollectionProxy [#<Unsubscribe::MailerSubscription>, #<Unsubscribe::MailerSubscription>] >User.first.subscribed_to_mailer? "MarketingMailer"# => true/falseUser.first.to_sgid_for_mailer_subscription# => #<SignedGlobalID:123 ...>
Unsubscribe::Mailer
- Add
include Unsubscribe::Mailer
to aMailer
. - Optionally call
unsubscribe_settings
to set aname
anddescription
. This will be used in the unsubscribe page. - Set
mail to:
to@recipient.email
. The@recipient
is an instance of whatever Classinclude 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 arecipient
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 theMailer
.
<%= 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.ymlen: 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 testbundle exec standardrb
License
The gem is available as open source under the terms of the MIT License.
Original Link: https://dev.to/stevepolitodesign/i-built-a-gem-that-makes-it-easy-to-automatically-unsubscribe-from-emails-in-rails-345o
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To