Class: SolidusBraintree::TransactionAddress

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Validations::Callbacks, CountryMapper
Defined in:
app/models/solidus_braintree/transaction_address.rb

Constant Summary

Constants included from CountryMapper

CountryMapper::CANADA_VARIANTS, CountryMapper::COUNTRY_MAP, CountryMapper::USA_VARIANTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ TransactionAddress

Returns a new instance of TransactionAddress.



25
26
27
28
29
30
31
32
# File 'app/models/solidus_braintree/transaction_address.rb', line 25

def initialize(attributes = {})
  country_name = attributes.delete(:country_name) || ""
  if attributes[:country_code].blank?
    attributes[:country_code] = iso_from_name(country_name)
  end

  super(attributes)
end

Instance Attribute Details

#address_line_1Object

Returns the value of attribute address_line_1.



12
13
14
# File 'app/models/solidus_braintree/transaction_address.rb', line 12

def address_line_1
  @address_line_1
end

#address_line_2Object

Returns the value of attribute address_line_2.



12
13
14
# File 'app/models/solidus_braintree/transaction_address.rb', line 12

def address_line_2
  @address_line_2
end

#cityObject

Returns the value of attribute city.



12
13
14
# File 'app/models/solidus_braintree/transaction_address.rb', line 12

def city
  @city
end

#country_codeObject

Returns the value of attribute country_code.



12
13
14
# File 'app/models/solidus_braintree/transaction_address.rb', line 12

def country_code
  @country_code
end

#first_nameObject

Returns the value of attribute first_name.



12
13
14
# File 'app/models/solidus_braintree/transaction_address.rb', line 12

def first_name
  @first_name
end

#last_nameObject

Returns the value of attribute last_name.



12
13
14
# File 'app/models/solidus_braintree/transaction_address.rb', line 12

def last_name
  @last_name
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'app/models/solidus_braintree/transaction_address.rb', line 12

def name
  @name
end

#state_codeObject

Returns the value of attribute state_code.



12
13
14
# File 'app/models/solidus_braintree/transaction_address.rb', line 12

def state_code
  @state_code
end

#zipObject

Returns the value of attribute zip.



12
13
14
# File 'app/models/solidus_braintree/transaction_address.rb', line 12

def zip
  @zip
end

Instance Method Details

#should_match_state_model?Boolean

Check to see if this address should match to a state model in the database

Returns:

  • (Boolean)


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

def should_match_state_model?
  spree_country&.states_required?
end

#spree_countryObject



34
35
36
# File 'app/models/solidus_braintree/transaction_address.rb', line 34

def spree_country
  country_code && (@country ||= ::Spree::Country.find_by(iso: country_code.upcase))
end

#spree_stateObject



38
39
40
41
42
43
44
# File 'app/models/solidus_braintree/transaction_address.rb', line 38

def spree_state
  spree_country && state_code && ( @state ||= spree_country.states.find_by(
    ::Spree::State.arel_table[:name].matches(state_code).or(
      ::Spree::State.arel_table[:abbr].matches(state_code)
    )
  ))
end

#to_spree_addressObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/solidus_braintree/transaction_address.rb', line 46

def to_spree_address
  address = ::Spree::Address.new(
    city: city,
    country: spree_country,
    address1: address_line_1,
    address2: address_line_2,
    zipcode: zip
  )

  if SolidusSupport.combined_first_and_last_name_in_address?
    address.name = begin
      if first_name.nil?
        name
      else
        [first_name, last_name].join(" ")
      end
    end
  else
    ::Spree::Deprecation.warn("first_name and last_name are deprecated. Use name instead.", caller)
    if first_name.nil?
      first, last = SolidusBraintree::Address.split_name(name)
      address.firstname = first
      address.lastname = last || "(left blank)"
    else
      address.firstname = first_name
      address.lastname = last_name || "(left blank)"
    end
  end

  if spree_state
    address.state = spree_state
  else
    address.state_name = state_code
  end
  address
end