Sha256: 617309f81d7945970b825da1865229a0bd07eb126eff05f82c331745e807fed3

Contents?: true

Size: 2 KB

Versions: 3

Compression:

Stored size: 2 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,
                          class_name: 'StripeLocal::Invoice'

    has_many   :charges,  inverse_of: :customer,
                          class_name: 'StripeLocal::Charge'

    has_one    :subscription, inverse_of: :customer,
                              class_name: 'StripeLocal::Subscription'

    has_one    :plan, through: :subscription,
                   inverse_of: :members,
                       source: :plan,
                   class_name: 'StripeLocal::Card'

    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 :metadata     then h[:metadata] = MultiJson.dump( v.to_hash ) 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

    def metadata
      MultiJson.load read_attribute( :metadata ), symbolize_keys: true
    end

  end
#=!=#
# ==Database Schema
#
# string   :id
# integer  :account_balance
# string   :default_card
# boolean  :delinquent
# string   :description
# string   :email
# text     :metadata
# integer  :model_id
#=ยก=#
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
stripe_local-0.2.5 app/models/stripe_local/customer.rb
stripe_local-0.2.4 app/models/stripe_local/customer.rb
stripe_local-0.2.3 app/models/stripe_local/customer.rb