Sha256: 0d9ddf62ed22d4591997c06c3006e1973fad9c7dd92f8eaba8a049389882efd2

Contents?: true

Size: 1.33 KB

Versions: 51

Compression:

Stored size: 1.33 KB

Contents

module Piggybak 
  class PaymentMethod < ActiveRecord::Base
    has_many :payment_method_values, :dependent => :destroy
    alias :metadata :payment_method_values

    accepts_nested_attributes_for :payment_method_values, :allow_destroy => true

    validates_presence_of :klass
    validates_presence_of :description

    def klass_enum 
      Piggybak.config.payment_calculators
    end

    validates_each :payment_method_values do |record, attr, value|
      if record.klass
        payment_method = record.klass.constantize
        metadata_keys = value.collect { |v| v.key }.sort
        if payment_method::KEYS.sort != metadata_keys
          if payment_method::KEYS.empty?
            record.errors.add attr, "You don't need any metadata for this method."
          else
            record.errors.add attr, "You must define key values for #{payment_method::KEYS.join(', ')} for this payment method."
          end
        end
      end
    end
    validates_each :active do |record, attr, value|
      if value && PaymentMethod.find_all_by_active(true).select { |p| p != record }.size > 0
        record.errors.add attr, "You may only have one active payment method."
      end
    end

    def key_values
      self.metadata.inject({}) { |h, k| h[k.key.to_sym] = k.value; h }
    end

    def admin_label
      "#{self.description}"
    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
piggybak-0.5.5 app/models/piggybak/payment_method.rb
piggybak-0.5.4 app/models/piggybak/payment_method.rb
piggybak-0.5.3 app/models/piggybak/payment_method.rb
piggybak-0.5.2 app/models/piggybak/payment_method.rb
piggybak-0.5.1 app/models/piggybak/payment_method.rb
piggybak-0.5.0 app/models/piggybak/payment_method.rb
piggybak-0.4.39 app/models/piggybak/payment_method.rb
piggybak-0.4.38 app/models/piggybak/payment_method.rb
piggybak-0.4.37 app/models/piggybak/payment_method.rb
piggybak-0.4.36 app/models/piggybak/payment_method.rb
piggybak-0.4.35 app/models/piggybak/payment_method.rb
piggybak-0.4.34 app/models/piggybak/payment_method.rb
piggybak-0.4.33 app/models/piggybak/payment_method.rb
piggybak-0.4.32 app/models/piggybak/payment_method.rb
piggybak-0.4.31 app/models/piggybak/payment_method.rb
piggybak-0.4.30 app/models/piggybak/payment_method.rb
piggybak-0.4.29 app/models/piggybak/payment_method.rb
piggybak-0.4.28 app/models/piggybak/payment_method.rb
piggybak-0.4.27 app/models/piggybak/payment_method.rb
piggybak-0.4.26 app/models/piggybak/payment_method.rb