Skip to content
lyang edited this page Mar 18, 2013 · 3 revisions

Attributes

Create

[:company, :country_code_numeric, :customer_id, :extended_address, :first_name, :id, :last_name, :locality, :postal_code, :region, :street_address]

Update

[:company, :country_code_numeric, :extended_address, :first_name, :last_name, :locality, :postal_code, :region, :street_address]

Readonly

[:country_code_alpha2, :country_code_alpha3, :country_name, :created_at, :updated_at]

Associations

Customer

address = BraintreeRails::Address.find('customer_id', 'address_id')
address.customer # Loaded from customer_id for persisted addresses.

Default Validations

[:customer_id, :presence => true, :on => :create],
[:first_name, :last_name, :company, :street_address, :extended_address, :locality, :region, :length => {:maximum => 255}],
[:country_code_numeric, :allow_blank => true, :inclusion => { :in => Braintree::Address::CountryNames.map {|country| country[3]} }],
[:street_address, :presence => true, :if => Proc.new { Configuration.require_street_address }],
[:postal_code, :presence => true, :format => { :with => /\A[- a-z0-9]+\z/i}, :if => Proc.new { Configuration.require_postal_code }]

A little more explanations

You might noticed that the default validations only checked the :country_code_numeric attribute instead of all 4 country name related attributes.

That is because BraintreeRails added a little code that will automatically set :country_code_numeric when any of the other three are set. This way, the validation logic and the final API call will be much simpler.

Default Callbacks

(before|after)_validate
(before|around|after)_save
(before|around|after)_create
(before|around|after)_update
(before|around|after)_destroy

Convenient Methods

Address#full_name

def full_name
  "#{first_name} #{last_name}".strip
end

Supported REST APIs

Create

Find

Due to the braintree_ruby's API, unlike all other models, BraintreeRaials::Address.find(customer_id, address_id) takes an extra customer_id to find an given address.

Update

Delete

Due to the braintree_ruby's API, unlike all other models, BraintreeRaials::Address.delete(customer_id, address_id) takes an extra customer_id to delete an given address.

The instance destroy, BraintreeRails::Address#destroy does not need this extra customer_id.

Clone this wiki locally