Sha256: efb3018c5165be614f4aaf79ac54067ff50832dd5abd0a8adb72933a3927ebe5

Contents?: true

Size: 1014 Bytes

Versions: 1

Compression:

Stored size: 1014 Bytes

Contents

module CatarseMonkeymail::UserConcern
  extend ActiveSupport::Concern

  included do
    before_save do
      if self.newsletter_changed? && self.newsletter
        subscribe_to_newsletter_list
      end

      if self.newsletter && self.email_changed? && !self.new_record?
        unsubscribe_from_newsletter_list self.email_was
        subscribe_to_newsletter_list
      end

      if self.newsletter_changed? && !self.newsletter
        unsubscribe_from_newsletter_list
      end
    end

    def mailchimp
      @mailchimp ||= ::CatarseMonkeymail::MailchimpApi.start
    end

    def subscribe_to_newsletter_list
      mailchimp.lists.subscribe(monkey_settings.list_id, { email: self.email }, { name: self.name }, 'html', false) rescue nil
    end

    def unsubscribe_from_newsletter_list email_arg = self.email
      mailchimp.lists.unsubscribe(monkey_settings.list_id, { email: email_arg }) rescue nil
    end

    private

    def monkey_settings
      ::CatarseMonkeymail.configuration
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
catarse_monkeymail-0.1.7 app/models/catarse_monkeymail/user_concern.rb