Sha256: dc2995c10ebd1c82f0d74523ec0b6fa01ace73d107c518653071ebfd09f6e7dd

Contents?: true

Size: 1.29 KB

Versions: 8

Compression:

Stored size: 1.29 KB

Contents

module Astrochimp
  class Signup < ActiveRecord::Base
    # Attributes
    attr_accessible :email, :referrer_id

    # Constants
    STATUS_NEW = 0
    STATUS_SUBSCRIBED = 1
    STATUS_INVITED = 2

    # Relations
    has_many :referrals, class_name: 'Signup'
    belongs_to :referrer, class_name: 'Signup', counter_cache: true

    # Validations
    validates :email, uniqueness: true, email: true

    # Callbacks
    before_create :add_email_to_mailchimp

    # Include referral code functionality
    include IdCode
    alias :referral_code :id_code

    # Return the referral code instead of id for urls
    alias :to_param :referral_code

    def self.find(id_or_code)
      if id_or_code.is_a? Integer
        super(id_or_code)
      else
        find_by_id_code(id_or_code)
      end
    end

    private
    def add_email_to_mailchimp
      # Handle any MailChimp errors
      begin
        h = ::Hominid::API.new(ENV['AC_MAILCHIMP_API_KEY'], { secure: true, timeout: 60 })
        h.list_subscribe(ENV['AC_MAILCHIMP_LIST_ID'], email, {}, ENV['AC_EMAIL_FORMAT'], ENV['AC_EMAIL_DOUBLE_OPTIN'])
        status = STATUS_SUBSCRIBED
      rescue => e
        errors.add(:base, I18n.t("activerecord.errors.models.signup.attributes.base.mailchimp_api_error", message: e.message))
        false
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
astrochimp-0.2.5 app/models/astrochimp/signup.rb
astrochimp-0.2.4 app/models/astrochimp/signup.rb
astrochimp-0.2.3 app/models/astrochimp/signup.rb
astrochimp-0.2.2 app/models/astrochimp/signup.rb
astrochimp-0.2.1 app/models/astrochimp/signup.rb
astrochimp-0.2.0 app/models/astrochimp/signup.rb
astrochimp-0.1.3 app/models/astrochimp/signup.rb
astrochimp-0.1.2 app/models/astrochimp/signup.rb