Module: Devise::Models::Mailjet

Extended by:
ActiveSupport::Concern
Defined in:
lib/devise_mailjet/model.rb,
lib/devise_mailjet/mailjet_worker.rb,
lib/devise_mailjet/mailjet_list_api_mapper.rb

Overview

Mailjet is responsible for joining users to mailjet 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 mailjet_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
                   mailjet_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.

Examples:

User.find(1).add_to_mailjet_list('Site Administrators List')
User.find(1).remove_from_mailjet_list('Site Administrators List')

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

Defined Under Namespace

Modules: ClassMethods Classes: MailjetListApiMapper, MailjetWorker

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.required_fields(klass) ⇒ Object



33
34
35
# File 'lib/devise_mailjet/model.rb', line 33

def self.required_fields(klass)
  [ :join_mailing_list ]
end

Instance Method Details

#add_to_mailjet_list(list_name) ⇒ Object

Add the user to the mailjet list with the specified name



56
57
58
59
60
61
62
63
64
# File 'lib/devise_mailjet/model.rb', line 56

def add_to_mailjet_list(list_name)
  if defined?(Sidekiq::Worker)
    MailjetWorker.perform_async(:subscribe, list_name, self.email, mailjet_config)
  else
    mapper = mailjet_list_mapper.respond_to?(:delay) ? mailjet_list_mapper.delay : mailjet_list_mapper
    # options = self.respond_to?(:mailjet_list_subscribe_options) ? mailjet_list_subscribe_options : {}

    mapper.subscribe_to_lists(list_name, self.email)
  end
end

#commit_mailing_list_joinObject

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



77
78
79
80
81
82
83
# File 'lib/devise_mailjet/model.rb', line 77

def commit_mailing_list_join
  if self.join_mailing_list
    add_to_mailjet_list(mailjet_lists_to_join)
  else
    remove_from_mailjet_list(mailjet_lists_to_join)
  end
end

#join_mailing_listObject



43
44
45
# File 'lib/devise_mailjet/model.rb', line 43

def join_mailing_list
  (new_record?) ? self.class.mailing_list_opt_in_by_default : read_attribute(:join_mailing_list)
end

#join_mailing_list=(join) ⇒ Object

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



38
39
40
41
# File 'lib/devise_mailjet/model.rb', line 38

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

#mailjet_list_mapperObject

mapper that helps convert list names to mailjet ids



86
87
88
# File 'lib/devise_mailjet/model.rb', line 86

def mailjet_list_mapper
  @@mailjet_list_api_mapper ||= MailjetListApiMapper.new
end

#mailjet_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.



51
52
53
# File 'lib/devise_mailjet/model.rb', line 51

def mailjet_lists_to_join
  self.class.mailing_list_name
end

#remove_from_mailjet_list(list_name) ⇒ Object

remove the user from the mailjet list with the specified name



67
68
69
70
71
72
73
74
# File 'lib/devise_mailjet/model.rb', line 67

def remove_from_mailjet_list(list_name)
  if defined?(Sidekiq::Worker)
    MailjetWorker.perform_async(:unsubscribe, list_name, self.email, mailjet_config)
  else
    mapper = mailjet_list_mapper.respond_to?(:delay) ? mailjet_list_mapper.delay : mailjet_list_mapper
    mapper.unsubscribe_from_lists(list_name, self.email)
  end
end