Sha256: f6b647e07e391445293550ed2340c9b222243ecb5d776394ba50711aaaf694fe

Contents?: true

Size: 1.45 KB

Versions: 18

Compression:

Stored size: 1.45 KB

Contents

# This links the acts_as_subscribable_buyer (customer) to the acts_as_subscribable (subscribable)

module Effective
  class Subscription < ActiveRecord::Base
    self.table_name = EffectiveOrders.subscriptions_table_name.to_s

    belongs_to :customer, class_name: 'Effective::Customer', counter_cache: true
    belongs_to :subscribable, polymorphic: true

    # Attributes
    # stripe_plan_id          :string  # This will be 'bronze' or something like that
    # status                  :string
    #
    # name                    :string
    # price                   :integer, default: 0
    #
    # timestamps

    before_validation(if: -> { plan && (stripe_plan_id_changed? || new_record?) }) do
      self.name = "#{plan[:name]} #{plan[:description]}"
      self.price = plan[:amount]
    end

    validates :customer, presence: true
    validates :subscribable, presence: true

    validates :stripe_plan_id, presence: true, inclusion: { in: EffectiveOrders.stripe_plans.except('trial').keys }
    validates :status, presence: true, inclusion: { in: %w(active past_due) }

    validates :name, presence: true
    validates :price, numericality: { greater_than_or_equal_to: 0, only_integer: true }

    def to_s
      name.presence || 'New Subscription'
    end

    def plan
      EffectiveOrders.stripe_plans[stripe_plan_id]
    end

    def active?
      status == 'active'
    end

    def <=>(other)
      (name || '') <=> (other.try(:name) || '')
    end

  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
effective_orders-3.2.3 app/models/effective/subscription.rb
effective_orders-3.2.2 app/models/effective/subscription.rb
effective_orders-3.2.1 app/models/effective/subscription.rb
effective_orders-3.2.0 app/models/effective/subscription.rb
effective_orders-3.1.7 app/models/effective/subscription.rb
effective_orders-3.1.6 app/models/effective/subscription.rb
effective_orders-3.1.4 app/models/effective/subscription.rb
effective_orders-3.1.3 app/models/effective/subscription.rb
effective_orders-3.1.0 app/models/effective/subscription.rb
effective_orders-3.0.4 app/models/effective/subscription.rb
effective_orders-3.0.3 app/models/effective/subscription.rb
effective_orders-3.0.2 app/models/effective/subscription.rb
effective_orders-4.0.0beta4 app/models/effective/subscription.rb
effective_orders-4.0.0beta3 app/models/effective/subscription.rb
effective_orders-4.0.0beta2 app/models/effective/subscription.rb
effective_orders-4.0.0beta1 app/models/effective/subscription.rb
effective_orders-3.0.1 app/models/effective/subscription.rb
effective_orders-3.0.0 app/models/effective/subscription.rb