module SubscribedTo::ClassMethods

Public Instance Methods

subscribed_to(id) click to toggle source

Enable SubscribedTo in your user model. The only paramter it takes is a symbol which corresponds to a list in the mail_chimp_config.lists hash.

subscribed_to :mailing_list
# File lib/subscribed_to.rb, line 54
def subscribed_to(id)
  if SubscribedTo.active  # don't activate all the gem goodies if we're not active
    include InstanceMethods
    if SubscribedTo.service == :mail_chimp
      extend MailChimp::ClassMethods
      include MailChimp::InstanceMethods
    end

    @list_key = id.to_sym

    # We need to reference which models are enabled, and which list they belong to when processing the webhooks.
    SubscribedTo.mail_chimp_config.enabled_models[self.list_id].blank? ?
      SubscribedTo.mail_chimp_config.enabled_models[self.list_id] = [self.to_s] :
      SubscribedTo.mail_chimp_config.enabled_models[self.list_id] << self.to_s

    class_eval do
      after_create :subscribe_to_list
      after_update :update_list_member
    end
  end
end