Class: Address

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/address.rb

Overview

Schema Information

Table name: addresses

id                :integer(4)      not null, primary key
address_type_id   :integer(4)
first_name        :string(255)
last_name         :string(255)
addressable_type  :string(255)     not null
addressable_id    :integer(4)      not null
address1          :string(255)     not null
address2          :string(255)
city              :string(255)     not null
state_id          :integer(4)
state_name        :string(255)
zip_code          :string(255)     not null
phone_id          :integer(4)
alternative_phone :string(255)
default           :boolean(1)      default(FALSE)
billing_default   :boolean(1)      default(FALSE)
active            :boolean(1)      default(TRUE)
created_at        :datetime
updated_at        :datetime

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) replace_address_id

if you are updating an address set this field.



55
56
57
# File 'app/models/address.rb', line 55

def replace_address_id
  @replace_address_id
end

Instance Method Details

- (Object) address_attributes

hash of all the address db attributes except created_at, updated_at, id

@ return [Hash] address db attributes except created_at, updated_at, id

Parameters:

  • none


84
85
86
# File 'app/models/address.rb', line 84

def address_attributes
  attributes.delete_if {|key, value| ["id", 'updated_at', 'created_at'].any?{|k| k == key }}
end

- (String) address_lines(join_chars = ', ')

Use this method to represent the full address as an array compacted

Parameters:

  • default (Optional String)

    is ', '

Returns:

  • (String)

    address1 and address2 joined together with the string you pass in



135
136
137
# File 'app/models/address.rb', line 135

def address_lines(join_chars = ', ')
  [address1, address2].delete_if{|add| add.blank?}.join(join_chars)
end

- (Object) cc_params

hash of all the address attributes to be passed to a creditcard processor

@ return [Hash] address attributes for a creditcard processor

Parameters:

  • none


92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/address.rb', line 92

def cc_params
  { :name     => name,
    :address1 => address1,
    :address2 => address2,
    :city     => city,
    :state    => state.abbreviation,
    :country  => state.country_id == Country::USA_ID ? 'US' : 'CAN',
    :zip      => zip_code#,
    #:phone    => phone
  }
end

- (String) city_state_name

Use this method to represent the "city, state.abbreviation"

Parameters:

  • (none)

Returns:

  • (String)

    "city, state.abbreviation"



153
154
155
# File 'app/models/address.rb', line 153

def city_state_name
  [city, state_abbr_name].join(', ')
end

- (String) city_state_zip

Use this method to represent the "city, state.abbreviation zip_code"

Parameters:

  • (none)

Returns:

  • (String)

    "city, state.abbreviation zip_code"



169
170
171
# File 'app/models/address.rb', line 169

def city_state_zip
  [city_state_name, zip_code].join(' ')
end

- (Array) full_address_array

Use this method to represent the full address as an array compacted

Parameters:

  • (none)

Returns:

  • (Array)

    Array has ("name", "address1", "address2"(unless nil), "city state zip")



127
128
129
# File 'app/models/address.rb', line 127

def full_address_array
  [name, address1, address2, city_state_zip].compact
end

- (Object) inactive!

Will inactivate and save the address

@ return [Boolean] true or error(error will only happen if there is a bad record in the db)

Parameters:

  • none


75
76
77
78
# File 'app/models/address.rb', line 75

def inactive!
  self.active = false
  save!
end

- (Object) name

First and last name of the person on the address

@ return [String] first and last name on the address with a space between

Examples:

first_name == 'John', last_name == 'Doe'

address.name  => 'John Doe'

Parameters:

  • none


67
68
69
# File 'app/models/address.rb', line 67

def name
  [first_name, last_name].compact.join(' ')
end

- (Object) shipping_method_ids

Method used to determine the shipping methods ids available for this address



105
106
107
108
109
110
111
# File 'app/models/address.rb', line 105

def shipping_method_ids
  if state_id && state.shipping_zone
    state.shipping_zone.shipping_method_ids
  else
    country.shipping_zone_id ? country.shipping_zone.shipping_method_ids : []
  end
end

- (Object) shipping_zone_id

Method used to determine the shipping_zone_id for this address

Specifically used to determine the order_item.shipping_rate_options


115
116
117
118
119
120
121
# File 'app/models/address.rb', line 115

def shipping_zone_id
  if state_id
    state.shipping_zone_id
  elsif country_id
    country.shipping_zone_id
  end
end

- (String) state_abbr_name

Use this method to represent the state abbreviation

it is possible the state is nil. in that case the abbreviation will be stored in
the state_name column in the DB

Parameters:

  • (none)

Returns:

  • (String)

    state abbreviation



145
146
147
# File 'app/models/address.rb', line 145

def state_abbr_name
  state ? state.abbreviation : state_name
end

- (String) state_country_name

Use this method to represent the "city, state.abbreviation"

Parameters:

  • (none)

Returns:

  • (String)

    "city, state.abbreviation"



161
162
163
# File 'app/models/address.rb', line 161

def state_country_name
  [state_abbr_name, country.try(:name)].compact.join(', ')
end