Sha256: d0175baf92e6c0b6c70769a22ad3e4323819453222609fdba36b37392c3ba64c

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

module DiscoApp::Concerns::Subscription
  extend ActiveSupport::Concern

  included do

    belongs_to :shop
    belongs_to :plan
    belongs_to :plan_code

    has_many :one_time_charges, class_name: 'DiscoApp::ApplicationCharge', dependent: :destroy
    has_many :recurring_charges, class_name: 'DiscoApp::RecurringApplicationCharge', dependent: :destroy

    enum status: [:trial, :active, :cancelled]
    enum subscription_type: [:recurring, :one_time]

    scope :current, -> { where status: [statuses[:trial], statuses[:active]] }

  end

  # Only require an active charge if the amount to be charged is > 0.
  def requires_active_charge?
    amount > 0
  end

  # Convenience method to check if this subscription has an active charge.
  def active_charge?
    active_charge.present?
  end

  # Convenience method to get the active charge for this subscription.
  def active_charge
    charges.active.first
  end

  # Return the appropriate set of charges for this subscription's type.
  def charges
    recurring? ? recurring_charges : one_time_charges
  end

  def charge_class
    recurring? ? DiscoApp::RecurringApplicationCharge : DiscoApp::ApplicationCharge
  end

  def shopify_charge_class
    recurring? ? ShopifyAPI::RecurringApplicationCharge : ShopifyAPI::ApplicationCharge
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
disco_app-0.8.8 app/models/disco_app/concerns/subscription.rb
disco_app-0.8.9 app/models/disco_app/concerns/subscription.rb