Sha256: ec3e54c0e3a1402ce5b0a04cf93be14d59484b06db36af0ec60e7cfd1947a176

Contents?: true

Size: 1007 Bytes

Versions: 2

Compression:

Stored size: 1007 Bytes

Contents

module Spree
  Spree.user_class.class_eval do
    has_one :referral
    has_one :referred_record
    has_one :affiliate, through: :referred_record, foreign_key: :affiliate_id
    has_one :affiliate_record, class_name: ReferredRecord

    attr_accessor :referral_code, :affiliate_code

    after_create :create_referral
    after_create :referral_affiliate_check

    def referred_by
      referred_record.try(:referral).try(:user)
    end
    
    def referred_count
      referral.referred_records.count
    end

    def referred?
      !referred_record.try(:referral).try(:user).nil?
    end

    def affiliate?
      !affiliate.nil?
    end

    private
      def referral_affiliate_check
        if !self.referral_code.nil?
          referred = Referral.find_by(code: referral_code)
        elsif !self.affiliate_code.nil?
          referred = Affiliate.find_by(path: affiliate_code)
        end
        if referred
          referred.referred_records.create(user: self)
        end
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spree_reffiliate-2.4.1 app/models/spree/user_decorator.rb
spree_reffiliate-2.3.1 app/models/spree/user_decorator.rb