SubscribedTo

Abstraction layer for managing mailing list subscription information for your user model. It makes the basic mailing list management tasks super simple.

The gem creates callbacks on your model for after_create and after_update.

The after_create callback registers the user for the mailing list if subscribed_to_list is true (ie: the user chooses checks the “Join the mailing list” box on the registration form).

The after_update callback will update the user subscription or member details. It will either:

Requirements

Supported Services

MailChimp

via the Hominid gem - github.com/terra-firma/hominid

API v1.3 - apidocs.mailchimp.com/1.3

Adds support for MailChimp webhooks

Constant Contact

Planned

Installation

Add the gem to your Gemfile

gem "subscribed_to"

Install the gem using bundler

bundle install

Run the generator. You must specify the name of your user model. Other options:

--skip-migration

Do not generate the migration file to add the required column

--service=SERVICE

Specify which mailing list service you subscribe to (currently only supports “mail_chimp”)

rails generate subscribed_to:install MODEL

If you run the generator with --skip-migration, then you must be sure to to include the subscribed_to_list and mail_chimp_id columns in some other migration.

t.boolean :subscribed_to_list, :default => false
t.integer :mail_chimp_id

Be sure to run the migration before you continue.

Initializer Configuration

After the initializer has been generated, you must set your mail service configuration options. The initializer is located at: config/initializers/subscribe_to.rb

Mail Chimp

Activate the gem. Can be disabled for development, staging, etc environtments. Activated only in production environment by default.

config.active = true

Set your API key

mail_chimp_config.api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us1"

Set up your mailing lists to subscribe users to. List names can be any valid symbol.

:id

id of list - get this from the “Settings” page of your list

:merge_vars

hash of merge vars on list. The key is a string which corresponds to a merge var on your mailing list. The value is a symbol corresponding to the attribute/method on the user model related to the specified merge var. It MUST at least include “EMAIL” (do not use “MERGE0”).

mail_chimp_config.lists = {
  :mailing_list => {
    :id => "xxxxxxxx",
    :merge_vars => {"FNAME" => :first_name, "LNAME" => :last_name, "EMAIL" => :email}}}

Set your secret key for the webhook URL.

Keep this key a secret. It's the only line of defense of preventing unwanted POST requests to the URL.

mail_chimp_config.secret_key = "my_secret_key_that_i_will_change"

Webhooks

The gem sets up support for MailChimp webhooks. Read more here: apidocs.mailchimp.com/webhooks/

First, you need to setup webhooks in your MailChimp account.

1) On the list tools page, click the “WebHooks” link.

2) Enter the webhook URL as:

http://mywebapp.com/subscribed_to/mail_chimp?key=<secret_key_defined_in_config>

3) Enable updates for events:

4) Send updates when a change was made by…

5) Click “Update”

User Model Configuration

You must enable SubscribedTo in your user model with subscribed_to.

The only paramter it takes is a symbol which corresponds to a list in the mail_chimp_config.lists hash.

subscribed_to :mailing_list

You may also need to specify subscribed_to_list as an accessible attribute.

attr_accessible :subscribed_to_list

View Configuration

To get things working with the view, just include a checkbox input on your user registration/edit forms.

= f.check_box :subscribed_to_list
= f.label :subscribed_to_list, "Join the mailing list"

Contributing to subscribed_to

Copyright © 2011 Eric Salczynski. See LICENSE.txt for further details.