Sha256: 1ea93aad1674f54222b74a3239c586179089266f4530a4f697a0865d1a453b6f

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require "aasm"

module Chaskiq
  class Subscriber < ActiveRecord::Base

    belongs_to :list
    has_many :metrics , as: :trackable
    has_one :campaign , through: :list , class_name: "Chaskiq::Campaign"

    validates :email , presence: true
    validates :name , presence: true

    include AASM

    aasm :column => :state do # default column: aasm_state
      state :passive, :initial => true
      state :subscribed, :after_enter => :notify_subscription
      state :unsubscribed, :after_enter => :notify_unsubscription

      event :suscribe do
        transitions :from => [:passive, :unsubscribed], :to => :subscribed
      end

      event :unsuscribe do
        transitions :from => [:subscribed, :passive], :to => :unsubscribed
      end
    end

    def notify_unsubscription
      puts "Pending"
    end

    def notify_subscription
      puts "Pending"
    end

    %w[click open bounce spam].each do |action|
      define_method("track_#{action}") do |opts|
        m = self.metrics.new
        m.assign_attributes(opts)
        m.action = action
        m.save
      end
    end

    def encoded_id
      URLcrypt.encode(self.email)
    end

    def decoded_id
      URLcrypt.decode(self.email)
    end


    def style_class
      case self.state
      when "passive"
        "plain"
      when "subscribed"
        "information"
      when "unsusbscribed"
        "warning"
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chaskiq-0.0.3 app/models/chaskiq/subscriber.rb
chaskiq-0.0.2 app/models/chaskiq/subscriber.rb