Sha256: ed21b0f8c4c3f5a41b0cdcdee421481fe72fc732486a15d81bcd9d83ce26eb59

Contents?: true

Size: 1.83 KB

Versions: 10

Compression:

Stored size: 1.83 KB

Contents

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

  included do

    belongs_to :shop
    belongs_to :plan
    belongs_to :plan_code, optional: true
    belongs_to :source, optional: true
    has_many :one_time_charges, class_name: 'DiscoApp::ApplicationCharge', dependent: :destroy
    has_many :recurring_charges, class_name: 'DiscoApp::RecurringApplicationCharge', dependent: :destroy

    enum status: {
      trial: 0,
      active: 1,
      cancelled: 2
    }
    enum subscription_type: {
      recurring: 0,
      one_time: 1
    }

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

    after_commit :cancel_charge

  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

  def as_json(options = {})
    super.merge(
      'active_charge' => active_charge
    )
  end

  private

    # If the amount or trial period for this subscription changes, clear any
    # active charge, as the user will need to re-authorize the charge.
    def cancel_charge
      return if (previous_changes.keys & ['amount', 'trial_period_days']).empty?
      return unless active_charge?
      active_charge.cancelled!
    end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
disco_app-0.16.1.pre.sidekiq.pre.6.pre.release app/models/disco_app/concerns/subscription.rb
disco_app-0.14.1 app/models/disco_app/concerns/subscription.rb
disco_app-0.14.2 app/models/disco_app/concerns/subscription.rb
disco_app-0.15.0 app/models/disco_app/concerns/subscription.rb
disco_app-0.16.0 app/models/disco_app/concerns/subscription.rb
disco_app-0.14.4 app/models/disco_app/concerns/subscription.rb
disco_app-0.14.3 app/models/disco_app/concerns/subscription.rb
disco_app-0.16.1 app/models/disco_app/concerns/subscription.rb
disco_app-0.15.2 app/models/disco_app/concerns/subscription.rb
disco_app-0.14.0 app/models/disco_app/concerns/subscription.rb