Sha256: 878c09ea0c08fc6946630d6241359bc6a05c182f789a8cecb2e1604d7cc796d7

Contents?: true

Size: 1.04 KB

Versions: 8

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Spree
  class WalletPaymentSource < Spree::Base
    belongs_to :user, class_name: Spree::UserClassHandle.new, foreign_key: 'user_id', inverse_of: :wallet_payment_sources
    belongs_to :payment_source, polymorphic: true, inverse_of: :wallet_payment_sources

    validates_presence_of :user
    validates_presence_of :payment_source
    validates :user_id, uniqueness: {
      scope: [:payment_source_type, :payment_source_id],
      message: :payment_source_already_exists
    }

    validate :check_for_payment_source_class
    validate :validate_payment_source_ownership

    private

    def check_for_payment_source_class
      if !payment_source.is_a?(Spree::PaymentSource)
        errors.add(:payment_source, :has_to_be_payment_source_class)
      end
    end

    def validate_payment_source_ownership
      return unless payment_source.present?

      if payment_source.respond_to?(:user_id) &&
         payment_source.user_id != user_id
        errors.add(:payment_source, :not_owned_by_user)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
solidus_core-2.9.6 app/models/spree/wallet_payment_source.rb
solidus_core-2.9.5 app/models/spree/wallet_payment_source.rb
solidus_core-2.9.4 app/models/spree/wallet_payment_source.rb
solidus_core-2.9.3 app/models/spree/wallet_payment_source.rb
solidus_core-2.9.2 app/models/spree/wallet_payment_source.rb
solidus_core-2.9.1 app/models/spree/wallet_payment_source.rb
solidus_core-2.9.0 app/models/spree/wallet_payment_source.rb
solidus_core-2.9.0.rc.1 app/models/spree/wallet_payment_source.rb