Sha256: 78b665bb8be5c63cb25c908b82aa0383cf731f9e23c1eea9d494e5775c7a510e

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module Freemium
  module Coupon

    def self.included(base)
      base.class_eval do
        has_many :coupon_redemptions, :dependent => :destroy
        has_many :subscriptions, :through => :coupon_redemptions
        has_and_belongs_to_many :subscription_plans

        validates :description, presence: true
        validates :discount_percentage, presence: true, :inclusion => 0..100

        before_save :normalize_redemption_key
      end
    end

    def discount(rate)
      rate * (1 - self.discount_percentage.to_f / 100)
    end

    def expired?
      (self.redemption_expiration && Date.today > self.redemption_expiration) || (self.redemption_limit && self.coupon_redemptions.count >= self.redemption_limit)
    end

    def applies_to_plan?(subscription_plan)
      return true if self.subscription_plans.blank? # applies to all plans
      self.subscription_plans.include?(subscription_plan)
    end

    protected

    def normalize_redemption_key
      self.redemption_key.downcase! unless self.redemption_key.blank?
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
freemium-ajb-0.0.4 lib/freemium/coupon.rb