lib/braintree_rails/customer.rb in braintree-rails-0.4.5 vs lib/braintree_rails/customer.rb in braintree-rails-1.0.0
- old
+ new
@@ -1,37 +1,32 @@
module BraintreeRails
- class Customer < SimpleDelegator
+ class Customer
include Model
- define_attributes(:id, :first_name, :last_name, :email, :company, :website, :phone, :fax, :created_at, :updated_at)
- validates :id, :format => {:with => /^[-_a-z0-9]*$/i}, :length => {:maximum => 36}, :exclusion => {:in => %w(all new)}
- validates :first_name, :last_name, :company, :website, :phone, :fax, :length => {:maximum => 255}
+ define_attributes(
+ :create => [:company, :custom_fields, :email, :fax, :first_name, :id, :last_name, :options, :phone, :website],
+ :update => [:company, :custom_fields, :email, :fax, :first_name, :last_name, :options, :phone, :website],
+ :readonly => [:created_at, :updated_at],
+ :as_association => [:id, :company, :email, :fax, :first_name, :last_name, :phone, :website]
+ )
- attr_reader :addresses, :credit_cards
+ define_associations(:addresses, :transactions, :credit_cards)
- def initialize(customer = {})
- customer = ensure_model(customer)
- set_addresses(customer)
- set_credit_cards(customer)
- super(customer)
+ def ensure_model(model)
+ if Braintree::Transaction::CustomerDetails === model
+ assign_attributes(extract_values(model))
+ self.persisted = model.id.present?
+ model
+ else
+ super
+ end
end
def full_name
"#{first_name} #{last_name}".strip
end
- def transactions
- new_record? ? [] : @transactions ||= Transactions.new(self)
- end
-
- private
- def set_addresses(customer)
- addresses = customer.respond_to?(:addresses) ? customer.addresses : []
- @addresses = Addresses.new(self, addresses)
- end
-
- def set_credit_cards(customer)
- credit_cards = customer.respond_to?(:credit_cards) ? customer.credit_cards : []
- @credit_cards = CreditCards.new(self, credit_cards)
+ def default_credit_card
+ credit_cards.find(&:default?)
end
end
end