77 lines
2.3 KiB
Ruby
77 lines
2.3 KiB
Ruby
class Contact < ActiveRecord::Base
|
|
mount_uploader :avatar, AvatarUploader
|
|
attr_accessible :avatar, :remote_avatar_url
|
|
|
|
has_many :emails, :dependent => :destroy
|
|
has_many :phones, :dependent => :destroy
|
|
|
|
belongs_to :address
|
|
accepts_nested_attributes_for :address
|
|
|
|
accepts_nested_attributes_for :emails, :reject_if => lambda { |a| a[:address].blank? }, :allow_destroy => true
|
|
accepts_nested_attributes_for :phones, :reject_if => lambda { |a| a[:nr].blank? }, :allow_destroy => true
|
|
|
|
def birth_date_string
|
|
s = birth_day.to_s || ""
|
|
s += "." unless birth_day.nil?
|
|
s += birth_month.to_s || ""
|
|
s += "." unless birth_month.nil?
|
|
s += birth_year.to_s || ""
|
|
end
|
|
|
|
def address_string
|
|
return "" if address.nil?
|
|
|
|
s = address.street || ""
|
|
s += " "
|
|
s += address.housenr || ""
|
|
unless (address.zipcode.blank? and address.place.blank?)
|
|
s += "·"
|
|
s += address.zipcode
|
|
s += " "
|
|
s += address.place
|
|
end
|
|
s
|
|
end
|
|
|
|
# very temporary
|
|
def twitter_username
|
|
nil
|
|
end
|
|
|
|
def twitter_username_link
|
|
return nil if twitter_username.nil?
|
|
'<a href="http://twitter.com/' + twitter_username + '">@' + twitter_username + "</a>"
|
|
end
|
|
|
|
# Fills all attributes with the ones given in the passed map
|
|
# (which may come as 'params' from the user.
|
|
def fill_attributes_from_map(c)
|
|
self.firstname = c[:firstname]
|
|
self.lastname = c[:lastname]
|
|
self.build_address
|
|
self.address.street = c[:address_attributes][:street]
|
|
self.address.housenr = c[:address_attributes][:housenr]
|
|
self.address.zipcode = c[:address_attributes][:zipcode]
|
|
self.address.place = c[:address_attributes][:place]
|
|
self.address.country = c[:address_attributes][:country]
|
|
|
|
# emails come as: ["0", {"address"=>"a@b.c"}]
|
|
c[:emails_attributes].map {|k,vs| vs}.each { |val|
|
|
unless val["address"].blank?
|
|
self.email = c.emails.build
|
|
self.email.desc = val["desc"]
|
|
self.email.address = val["address"]
|
|
end
|
|
}
|
|
|
|
# phones come as: {"0"=>{"nr"=>"+49123456789"}, "1"=>{"nr"=>"..."}}
|
|
c[:phones_attributes].map {|k,vs| vs}.each { |val|
|
|
unless val["nr"].blank?
|
|
self.phone = c.phones.build
|
|
self.phone.desc = val["desc"]
|
|
self.phone.nr = val["nr"]
|
|
end
|
|
}
|
|
end
|
|
end
|