Merged the address types

This commit is contained in:
Andres Keskküla 2014-09-25 15:27:08 +03:00
parent 7c8e7e22af
commit c4deed6a55
17 changed files with 90 additions and 103 deletions

View file

@ -14,41 +14,38 @@ class Address < ActiveRecord::Base
# validates_inclusion_of :type, in: TYPES
class << self
def validate_postal_info_types(parsed_frame)
errors, used = [], []
parsed_frame.css('postalInfo').each do |pi|
attr = pi.attributes['type'].try(:value)
errors << { code: 2003, msg: I18n.t('errors.messages.attr_missing', key: 'type') } and next unless attr
unless TYPES.include?(attr)
errors << { code: 2005, msg: I18n.t('errors.messages.invalid_type'), value: { obj: 'type', val: attr } }
next
end
errors << { code: 2005, msg: I18n.t('errors.messages.repeating_postal_info') } and next if used.include?(attr)
used << attr
end; errors
end
# def validate_postal_info_types(parsed_frame)
# errors, used = [], []
# parsed_frame.css('postalInfo').each do |pi|
# attr = pi.attributes['type'].try(:value)
# errors << { code: 2003, msg: I18n.t('errors.messages.attr_missing', key: 'type') } and next unless attr
# unless TYPES.include?(attr)
# errors << { code: 2005, msg: I18n.t('errors.messages.invalid_type'), value: { obj: 'type', val: attr } }
# next
# end
# errors << { code: 2005, msg: I18n.t('errors.messages.repeating_postal_info') } and next if used.include?(attr)
# used << attr
# end; errors
# end
def extract_attributes(ah)
address_hash = {}
[ah].flatten.each do |pi|
address_hash[local?(pi)] = addr_hash_from_params(pi)
end
ah = ah.first if ah.is_a?(Array)
address_hash[:address_attributes] = addr_hash_from_params(ah)
address_hash
end
private
def local?(postal_info)
return :local_address_attributes if postal_info[:type] == LOCAL_TYPE_SHORT
:international_address_attributes
end
# def local?(postal_info)
# return :local_address_attributes if postal_info[:type] == LOCAL_TYPE_SHORT
# :international_address_attributes
# end
def addr_hash_from_params(addr)
return {} if addr.nil?
return {} unless addr[:addr].is_a?(Hash)
{ name: addr[:name],
org_name: addr[:org],
country_id: Country.find_by(iso: addr[:addr][:cc]).try(:id),
{ country_id: Country.find_by(iso: addr[:addr][:cc]).try(:id),
city: addr[:addr][:city],
street: addr[:addr][:street][0],
street2: addr[:addr][:street][1],