Sha256: dfbd176631e9c88324589c0432bbb8abd62ae1c6e1d7343156c340611b49262b

Contents?: true

Size: 1.93 KB

Versions: 14

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.subscription[: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

14 entries across 14 versions & 1 rubygems

Version Path
effective_orders-3.2.3 app/models/concerns/acts_as_subscribable.rb
effective_orders-3.2.2 app/models/concerns/acts_as_subscribable.rb
effective_orders-3.2.1 app/models/concerns/acts_as_subscribable.rb
effective_orders-3.2.0 app/models/concerns/acts_as_subscribable.rb
effective_orders-3.1.7 app/models/concerns/acts_as_subscribable.rb
effective_orders-3.1.6 app/models/concerns/acts_as_subscribable.rb
effective_orders-3.1.4 app/models/concerns/acts_as_subscribable.rb
effective_orders-3.1.3 app/models/concerns/acts_as_subscribable.rb
effective_orders-3.1.0 app/models/concerns/acts_as_subscribable.rb
effective_orders-3.0.4 app/models/concerns/acts_as_subscribable.rb
effective_orders-3.0.3 app/models/concerns/acts_as_subscribable.rb
effective_orders-3.0.2 app/models/concerns/acts_as_subscribable.rb
effective_orders-3.0.1 app/models/concerns/acts_as_subscribable.rb
effective_orders-3.0.0 app/models/concerns/acts_as_subscribable.rb