Module: Devise::Referable::Referral::ClassMethods

Defined in:
lib/devise_referable/referral.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_referral(options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/devise_referable/referral.rb', line 18

def acts_as_referral(options = {})
  class_eval <<-EOV
    include Devise::Referable::Referral::InstanceMethods

    def self.referrer_types
      User.referrer_types
    end

    def self.lookup_referrer(token)
      unless token.blank?
        referrer_types.map{ |type| type.to_s.classify.constantize }.each do |klass|
          ref = klass.find_by_referrer_token(token)
          return ref unless ref.nil?
        end
      end
      return nil
    end

    def self.from_token(token)
      sender = lookup_referrer(token)
      if sender
        self.create do |ref|
          ref.referrer = sender
        end
      end
    end

    belongs_to :referrer, :polymorphic => true
    belongs_to :recipient, :class_name => 'User'
    before_create :generate_token
    
  EOV
end