Class: Spree::Address

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

Constant Summary collapse

ADDRESS_FIELDS =

we’re not freezing this on purpose so developers can extend and manage those attributes depending of the logic of their applications

%w(firstname lastname company address1 address2 city state zipcode country phone)
EXCLUDED_KEYS_FOR_COMPARISION =
%w(id updated_at created_at deleted_at user_id)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

belongs_to_required_by_default, page, spree_base_scopes

Methods included from Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Class Method Details

.build_defaultObject



42
43
44
# File 'app/models/spree/address.rb', line 42

def self.build_default
  new(country: Spree::Country.default)
end

.default(user = nil, kind = 'bill') ⇒ Object



46
47
48
49
50
51
52
# File 'app/models/spree/address.rb', line 46

def self.default(user = nil, kind = 'bill')
  if user && user_address = user.public_send(:"#{kind}_address")
    user_address.clone
  else
    build_default
  end
end

.required_fieldsObject



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

def self.required_fields
  Spree::Address.validators.map do |v|
    v.is_a?(ActiveModel::Validations::PresenceValidator) ? v.attributes : []
  end.flatten
end

Instance Method Details

#==(other) ⇒ Object



87
88
89
90
91
# File 'app/models/spree/address.rb', line 87

def ==(other)
  return false unless other&.respond_to?(:value_attributes)

  value_attributes == other.value_attributes
end

#active_merchant_hashObject

Generates an ActiveMerchant compatible address hash



102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/spree/address.rb', line 102

def active_merchant_hash
  {
    name: full_name,
    address1: address1,
    address2: address2,
    city: city,
    state: state_text,
    zip: zipcode,
    country: country.try(:iso),
    phone: phone
  }
end

#can_be_deleted?Boolean

Returns:

  • (Boolean)


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

def can_be_deleted?
  shipments.empty? && !Order.where('bill_address_id = ? OR ship_address_id = ?', id, id).exists?
end

#checkObject



131
132
133
134
135
# File 'app/models/spree/address.rb', line 131

def check
  attrs = attributes.except('id', 'updated_at', 'created_at')
  the_same_address = user&.addresses&.find_by(attrs)
  the_same_address || self
end

#cloneObject



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

def clone
  self.class.new(value_attributes)
end

#destroyObject



137
138
139
140
141
142
143
# File 'app/models/spree/address.rb', line 137

def destroy
  if can_be_deleted?
    super
  else
    update_column :deleted_at, Time.current
  end
end

#editable?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'app/models/spree/address.rb', line 123

def editable?
  new_record? || (shipments.empty? && !Order.complete.where('bill_address_id = ? OR ship_address_id = ?', id, id).exists?)
end

#empty?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'app/models/spree/address.rb', line 97

def empty?
  attributes.except('id', 'created_at', 'updated_at', 'country_id').all? { |_, v| v.nil? }
end

#full_nameObject



60
61
62
# File 'app/models/spree/address.rb', line 60

def full_name
  "#{firstname} #{lastname}".strip
end

#require_phone?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'app/models/spree/address.rb', line 115

def require_phone?
  Spree::Config[:address_requires_phone]
end

#require_zipcode?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/models/spree/address.rb', line 119

def require_zipcode?
  country ? country.zipcode_required? : true
end

#state_name_textObject



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

def state_name_text
  state_name.present? ? state_name : state&.name
end

#state_textObject



64
65
66
# File 'app/models/spree/address.rb', line 64

def state_text
  state.try(:abbr) || state.try(:name) || state_name
end

#to_sObject



72
73
74
75
76
77
78
79
80
81
# File 'app/models/spree/address.rb', line 72

def to_s
  [
    full_name,
    company,
    address1,
    address2,
    "#{city}, #{state_text} #{zipcode}",
    country.to_s
  ].reject(&:blank?).map { |attribute| ERB::Util.html_escape(attribute) }.join('<br/>')
end

#value_attributesObject



93
94
95
# File 'app/models/spree/address.rb', line 93

def value_attributes
  attributes.except(*EXCLUDED_KEYS_FOR_COMPARISION)
end