Sha256: 13613d7bb469006430d17f81ac624e738bf0fbf8b67fe0cbaced9fbcf9351cf9

Contents?: true

Size: 839 Bytes

Versions: 2

Compression:

Stored size: 839 Bytes

Contents

module Spree
  class Subscription < Spree::Base
    include RoleSubscriber
    include RestrictiveDestroyer
    include ApiHandler

    acts_as_restrictive_destroyer column: :unsubscribed_at
    attr_accessor :card_token

    belongs_to :plan
    belongs_to :user
    has_many :events, class_name: 'Spree::SubscriptionEvent'

    validates :plan_id, :email, :user_id, presence: true
    validates :plan_id, uniqueness: { scope: [:user_id, :unsubscribed_at] }
    validates :user_id, uniqueness: { scope: :unsubscribed_at }

    delegate_belongs_to :plan, :api_plan_id
    before_validation :set_email, on: :create

    validate :verify_plan, on: :create

    private

    def set_email
      self.email = user.try(:email)
    end

    def verify_plan
      errors.add :plan_id, "is not active." unless plan.try(:visible?)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spree_account_recurring-1.3.0 app/models/spree/subscription.rb
spree_account_recurring-1.2.0 app/models/spree/subscription.rb