Sha256: 8b1cfe39211b8786ff46b64fafb9acfb4086c213f055201fa3c59b2cc2886ec9

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

module ActsAsSubscribable
  extend ActiveSupport::Concern

  mattr_accessor :descendants

  module ActiveRecord
    def acts_as_subscribable(*options)
      @acts_as_subscribable = options || []
      include ::ActsAsSubscribable
      (ActsAsSubscribable.descendants ||= []) << self
    end
  end

  included do
    has_one :subscription, as: :subscribable, class_name: 'Effective::Subscription'
    has_one :customer, through: :subscription, class_name: 'Effective::Customer'

    validates :subscripter, associated: true

    scope :subscribed, -> { where(id: joins(:subscription)) }  # All resources with a subscription
    scope :trialing, -> { where.not(id: joins(:subscription)) } # All resources without a subscription
  end

  module ClassMethods
  end

  def subscripter
    @_effective_subscripter ||= Effective::Subscripter.new(subscribable: self, user: buyer)
  end

  def subscripter=(atts)
    subscripter.assign_attributes(atts)
  end

  def subscribed?(stripe_plan_id = nil)
    case stripe_plan_id
    when nil
      subscription.present?  # Subscribed to any subscription?
    when (EffectiveOrders.stripe_plans['trial'] || {})[:id]
      subscription.blank? || subscription.new_record? || subscription.stripe_plan_id == stripe_plan_id
    else
      subscription && subscription.persisted? && subscription.errors.blank? && subscription.stripe_plan_id == stripe_plan_id
    end
  end

  def subscription_active?
    (trialing? && !trial_expired?) || (subscribed? && subscription.active?)
  end

  def trialing?
    !subscribed?
  end

  def trial_expired?
    trialing? && Time.zone.now > trial_expires_at
  end

  def trial_expires_at
    # The rake task send_trial_expiring_emails depends on this beginning_of_day
    ((created_at || Time.zone.now) + EffectiveOrders.subscriptions[:trial_period]).beginning_of_day
  end

  def buyer
    raise 'acts_as_subscribable object requires the buyer be defined to return the User buying this item.'
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
effective_orders-4.0.0beta4 app/models/concerns/acts_as_subscribable.rb
effective_orders-4.0.0beta3 app/models/concerns/acts_as_subscribable.rb
effective_orders-4.0.0beta2 app/models/concerns/acts_as_subscribable.rb
effective_orders-4.0.0beta1 app/models/concerns/acts_as_subscribable.rb