Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stevepolitodesign/unsubscribe
Automatically unsubscribe from emails in Rails.
https://github.com/stevepolitodesign/unsubscribe
actionmailer rails ruby-on-rails
Last synced: 3 months ago
JSON representation
Automatically unsubscribe from emails in Rails.
- Host: GitHub
- URL: https://github.com/stevepolitodesign/unsubscribe
- Owner: stevepolitodesign
- License: mit
- Created: 2021-09-17T09:25:50.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-10T01:10:56.000Z (over 1 year ago)
- Last Synced: 2024-09-30T07:22:11.554Z (3 months ago)
- Topics: actionmailer, rails, ruby-on-rails
- Language: Ruby
- Homepage: https://stevepolito.design/blog/auto-unsubscribe-from-email-in-rails/
- Size: 202 KB
- Stars: 66
- Watchers: 5
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# 📭 Unsubscribe
Automatically unsubscribe from emails in Rails.
![Demo](./docs/demo.gif)
## 🚀 Installation
Add this line to your application's Gemfile:
```ruby
gem 'unsubscribe'
```And then execute:
```bash
$ bundle
```Or install it yourself as:
```bash
$ gem install unsubscribe
```Then run the installation commands:
```bash
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.
```ruby
class User < ApplicationRecord
include Unsubscribe::Owner
end
```#### Available Methods
```ruby
User.first.mailer_subscriptions
# => #, #] >User.first.subscribed_to_mailer? "MarketingMailer"
# => true/falseUser.first.to_sgid_for_mailer_subscription
# => #
```### 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.```ruby
class MarketingMailer < ApplicationMailer
include Unsubscribe::Mailerunsubscribe_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.
```ruby
MarketingMailer.with(
recipient: User.first
).promotion.deliver_now
```#### Available Methods
```ruby
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`.
```html+erb
<%= 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.
```yml
# 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:
```ruby
bin/rails test
bundle exec standardrb
```## 📜 License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).