2011-08-16 20:05:58 +02:00
|
|
|
class Contact < ActiveRecord::Base
|
|
|
|
|
|
|
|
validates_length_of :nick, :within => 5..15
|
|
|
|
validates_length_of :first_name,:last_name, :within => 3..20
|
|
|
|
validates_length_of :email, :within => 5..50
|
|
|
|
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
|
2011-09-16 22:08:30 +02:00
|
|
|
validates_length_of :info, :maximum => 100
|
2011-09-16 19:44:29 +02:00
|
|
|
validate :check_unique_nick, :on => :create
|
2011-09-16 22:08:30 +02:00
|
|
|
default_scope :order => 'nick ASC'
|
2011-08-16 20:05:58 +02:00
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
def self.getPageForUser(user,page,sort_field,sort_dir)
|
|
|
|
|
|
|
|
if sort_field
|
|
|
|
if Contact.attribute_method?(sort_field) == true
|
|
|
|
order = sort_field
|
|
|
|
sort_dir == 'desc' ? order += ' desc' : sort_dir
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Contact.paginate :page => page , :per_page => $defaults["contacts_per_page"], :conditions=> ['user_id = ?', user.id],:order => order
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_name
|
|
|
|
first_name + ' ' + last_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_unique_nick
|
|
|
|
if !Contact.where('upper(nick) = ? and user_id = ?',nick.upcase,user_id).size.zero?
|
|
|
|
errors.add(:nick, :not_unique)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-16 19:44:29 +02:00
|
|
|
def export
|
|
|
|
fields = []
|
|
|
|
fields << nick.presence || ""
|
|
|
|
fields << first_name || ""
|
|
|
|
fields << last_name || ""
|
|
|
|
fields << email || ""
|
|
|
|
fields << info || ""
|
|
|
|
fields.join(';')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.import(user,line)
|
|
|
|
fields = line.split(/;/)
|
2011-09-16 22:08:30 +02:00
|
|
|
contact = user.contacts.build( :nick => fields[0].strip,
|
|
|
|
:first_name => fields[1].strip,
|
|
|
|
:last_name => fields[2].strip,
|
|
|
|
:email => fields[3].strip,
|
|
|
|
:info => fields[4].strip)
|
2011-09-16 19:44:29 +02:00
|
|
|
contact.save!
|
|
|
|
end
|
2011-08-16 20:05:58 +02:00
|
|
|
end
|