Module: Devise::Models::Mailchimp

Extended by:
ActiveSupport::Concern
Defined in:
lib/devise_mailchimp/model.rb,
lib/devise_mailchimp/mailchimp_list_api_mapper.rb

Overview

Mailchimp is responsible for joining users to mailchimp lists when the create accounts with devise When a user is created, and join_mailing_list is set to true, they will automatically be added to one or more mailing lists returned by mailchimp_lists_to_join.

Configuration

mailing_list_name: Default mailing list for user to join.  This can be an array of strings, or just one string.
                   By default, this is "Site List".  If this will be configurable for each user, override
                   mailchimp_lists_to_join returning the list name or an array of list names for the user to
                   join.

mailing_list_opt_in_by_default: Determines if the checkbox for the user to opt-in to the mailing list should
                                be checked by default, or not.  Defaults to true.

mailchimp_api_key: The API key for accessing the mailchimp service.  To generate a new API key, go to the
                   account tab in your MailChimp account and select API Keys & Authorized Apps, then add
                   a key.  This defaults to 'your_api_key'

double_opt_in: Requires that users must click a link in a confirmation email to be added to your mailing list.
               Defaults to false.

Examples:

User.find(1).add_to_mailchimp_list('Site Administrators List')
User.find(1).remove_from_mailchimp_list('Site Administrators List')

u = User.new
u.join_mailing_list = true
u.save

Defined Under Namespace

Modules: ClassMethods Classes: MailchimpListApiMapper

Instance Method Summary collapse

Instance Method Details

#add_to_mailchimp_list(list_name) ⇒ Object

Add the user to the mailchimp list with the specified name



61
62
63
64
# File 'lib/devise_mailchimp/model.rb', line 61

def add_to_mailchimp_list(list_name)
  mapper = mailchimp_list_mapper.respond_to?(:delay) ? mailchimp_list_mapper.delay : mailchimp_list_mapper
  mapper.subscribe_to_lists(list_name, self.email)
end

#commit_mailing_list_joinObject

Commit the user to the mailing list if they have selected to join



73
74
75
# File 'lib/devise_mailchimp/model.rb', line 73

def commit_mailing_list_join
  add_to_mailchimp_list(mailchimp_lists_to_join) if @join_mailing_list
end

#join_mailing_listObject



48
49
50
# File 'lib/devise_mailchimp/model.rb', line 48

def join_mailing_list
  @join_mailing_list.nil? ? self.class.mailing_list_opt_in_by_default : @join_mailing_list
end

#join_mailing_list=(join) ⇒ Object

Set this to true to have the user automatically join the mailchimp_lists_to_join



40
41
42
43
44
45
# File 'lib/devise_mailchimp/model.rb', line 40

def join_mailing_list=(join)
  join.downcase! if join.is_a?(String)
  true_values = ['yes','true',true,'1',1]
  join = true_values.include?(join)
  @join_mailing_list = join
end

#mailchimp_list_mapperObject

mapper that helps convert list names to mailchimp ids



78
79
80
# File 'lib/devise_mailchimp/model.rb', line 78

def mailchimp_list_mapper
  @@mailchimp_list_mapper ||= MailchimpListApiMapper.new(self.class.mailchimp_api_key, self.class.double_opt_in)
end

#mailchimp_lists_to_joinObject

The mailing list or lists the user will join Should return either a single string or an array of strings. By default, returns the mailing_list_name configuration option. If you want to customize the lists based on other information, override this method in your model.



56
57
58
# File 'lib/devise_mailchimp/model.rb', line 56

def mailchimp_lists_to_join
  self.class.mailing_list_name
end

#remove_from_mailchimp_list(list_name) ⇒ Object

remove the user from the mailchimp list with the specified name



67
68
69
70
# File 'lib/devise_mailchimp/model.rb', line 67

def remove_from_mailchimp_list(list_name)
  mapper = mailchimp_list_mapper.respond_to?(:delay) ? mailchimp_list_mapper.delay : mailchimp_list_mapper
  mapper.unsubscribe_from_lists(list_name, self.email)
end