Sha256: 55a2289b0ee0ab3d019a43e00253246b2841d45fbf659fdd71a704b73cf15677

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

module StripeLocal
  class Customer < ActiveRecord::Base
    include ObjectAdapter

    self.primary_key = :id

    belongs_to :model,   inverse_of: :customer,
                        foreign_key: "model_id",
                         class_name: "::#{StripeLocal::model_class}"

    has_many   :cards, inverse_of: :customer, class_name: 'StripeLocal::Card'

    has_many   :invoices, inverse_of: :customer

    has_many   :charges, inverse_of: :customer

    has_one    :subscription, inverse_of: :customer

    has_one    :plan, through: :subscription,
                   inverse_of: :members,
                       source: :plan

    class<<self

      def create params
        super normalize params
      end

      def normalize params
        params.each_with_object({}) do |(k,v),h|
          key = case k.to_sym
          when :cards        then create_each_card( v.data ) and next
          when :subscription then create_subscription( v )   and next
          when ->(x){attribute_method? x} then k.to_sym
          else next
          end
          h[key] = v
        end
      end

      def create_each_card ary
        ary.each do |card|
          StripeLocal::Card.create( card.to_hash )
        end
      end

      def create_subscription object
        StripeLocal::Subscription.create( object.to_hash ) unless object.nil?
      end
    end

#=!=>>>
#       ~<Schema>~
#   t.string   :id
#   t.integer  :account_balance
#   t.string   :default_card
#   t.boolean  :delinquent
#   t.string   :description
#   t.string   :email
#   t.text     :metadata
#   t.integer  :model_id
#   t.timestamps

#=ยก=>>>
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
stripe_local-0.1.3 app/models/stripe_local/customer.rb
stripe_local-0.1.2 app/models/stripe_local/customer.rb
stripe_local-0.1.1 app/models/stripe_local/customer.rb