works but need some cleanup
This commit is contained in:
parent
0ec83db287
commit
e40a859b7d
752 changed files with 4866 additions and 27923 deletions
71
app/models/contact.rb
Normal file → Executable file
71
app/models/contact.rb
Normal file → Executable file
|
@ -1,34 +1,37 @@
|
|||
require 'cdfutils'
|
||||
require_association 'contact_group'
|
||||
|
||||
class Contact < ActiveRecord::Base
|
||||
|
||||
class Contact < ActiveRecord::Base
|
||||
|
||||
validate :check_fname_and_lname, :check_mail_cannot_be_changed
|
||||
validate :check_email, :on => :create
|
||||
|
||||
has_and_belongs_to_many :groups, :class_name => "ContactGroup", :join_table => "contact_contact_groups", :association_foreign_key => "contact_group_id", :foreign_key => "contact_id"
|
||||
|
||||
|
||||
# Finder methods follow
|
||||
def Contact.find_by_user(user_id)
|
||||
find(:all, :conditions => ["customer_id = ?", user_id], :order => "fname asc", :limit => 10)
|
||||
end
|
||||
|
||||
|
||||
def Contact.find_by_user_email(user_id, email)
|
||||
find(:first, :conditions => ["customer_id = #{user_id} and email = ?", email])
|
||||
end
|
||||
|
||||
|
||||
def Contact.find_by_group_user(user_id, grp_id)
|
||||
result = Array.new
|
||||
find(:all, :conditions => ["customer_id = ?", user_id], :order => "fname asc").each { |c|
|
||||
begin
|
||||
begin
|
||||
c.groups.find(grp_id)
|
||||
result << c
|
||||
result << c
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
end
|
||||
}
|
||||
result
|
||||
end
|
||||
|
||||
named_scope :for_customer, lambda{ |customer_id| {:conditions => {:customer_id => customer_id}} }
|
||||
named_scope :letter, lambda{ |letter| {:conditions => ["contacts.fname LIKE ?", "#{letter}%"]} }
|
||||
|
||||
scope :for_customer, lambda{ |customer_id| {:conditions => {:customer_id => customer_id}} }
|
||||
scope :letter, lambda{ |letter| {:conditions => ["contacts.fname LIKE ?", "#{letter}%"]} }
|
||||
|
||||
def Contact.find_by_user_letter(user_id, letter)
|
||||
find_by_sql("select * from contacts where customer_id=#{user_id} and substr(UPPER(fname),1,1) = '#{letter}' order by fname")
|
||||
end
|
||||
|
@ -36,36 +39,38 @@ class Contact < ActiveRecord::Base
|
|||
def full_name
|
||||
"#{fname} #{lname}"
|
||||
end
|
||||
|
||||
|
||||
def show_name
|
||||
"#{fname} #{lname}"
|
||||
end
|
||||
|
||||
|
||||
def full_address
|
||||
"#{fname} #{lname}<#{email}>"
|
||||
end
|
||||
|
||||
protected
|
||||
def validate
|
||||
errors.add 'fname', I18n.t(:validate_fname_error) unless self.fname =~ /^.{2,20}$/i
|
||||
errors.add 'lname', I18n.t(:validate_lname_error) unless self.lname =~ /^.{2,20}$/i
|
||||
|
||||
# Contact e-mail cannot be changed
|
||||
unless self.new_record?
|
||||
|
||||
protected
|
||||
def check_fname_and_lname
|
||||
errors.add 'fname', t(:validate_fname_error) unless self.fname =~ /^.{2,20}$/i
|
||||
errors.add 'lname', t(:validate_lname_error) unless self.lname =~ /^.{2,20}$/i
|
||||
end
|
||||
|
||||
def check_mail_cannot_be_changed
|
||||
# Contact e-mail cannot be changed
|
||||
unless self.new_record?
|
||||
old_record = Contact.find(self.id)
|
||||
errors.add 'email', I18n.t(:contacto_cannot_be_changed) unless old_record.email == self.email
|
||||
errors.add 'email', t(:contact_cannot_be_changed) unless old_record.email == self.email
|
||||
end
|
||||
end
|
||||
|
||||
def check_mail
|
||||
# Contact e-mail cannot be changed, so we only need to validate it on create
|
||||
errors.add 'email', t(:validate_email_error) unless valid_email?(self.email)
|
||||
# Already existing e-mail in contacts for this user is not allowed
|
||||
if self.new_record?
|
||||
if Contact.find :first, :conditions => {:email => email, :customer_id => customer_id}
|
||||
errors.add('email', I18n.t(:email_exists))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def validate_on_create
|
||||
# Contact e-mail cannot be changed, so we only need to validate it on create
|
||||
errors.add 'email', I18n.t(:validate_email_error) unless valid_email?(self.email)
|
||||
# Already existing e-mail in contacts for this user is not allowed
|
||||
if self.new_record?
|
||||
if Contact.find :first, :conditions => {:email => email, :customer_id => customer_id}
|
||||
errors.add('email', I18n.t(:email_exists))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
43
app/models/contact_group.rb
Normal file → Executable file
43
app/models/contact_group.rb
Normal file → Executable file
|
@ -1,25 +1,30 @@
|
|||
class ContactGroup < ActiveRecord::Base
|
||||
has_and_belongs_to_many :contacts, :class_name => "Contact", :join_table => "contact_contact_groups", :association_foreign_key => "contact_id", :foreign_key => "contact_group_id"
|
||||
has_and_belongs_to_many :contacts, :class_name => "Contact", :join_table => "contact_contact_groups", :association_foreign_key => "contact_id", :foreign_key => "contact_group_id"
|
||||
|
||||
validate :check_group_name
|
||||
validate :check_group_id, :on => :create
|
||||
validate :check_group_uniqness, :on => :update
|
||||
|
||||
def ContactGroup.find_by_user(user_id)
|
||||
find_by_sql("select * from contact_groups where customer_id = #{user_id} order by name asc")
|
||||
end
|
||||
|
||||
protected
|
||||
def validate
|
||||
errors.add('name', :contactgroup_name_invalid) unless self.name =~ /^.{1,50}$/i
|
||||
def ContactGroup.find_by_user(user_id)
|
||||
find_by_sql("select * from contact_groups where customer_id = #{user_id} order by name asc")
|
||||
end
|
||||
|
||||
def validate_on_create
|
||||
if ContactGroup.find_first(["name = '#{name}' and customer_id = #{user_id}"])
|
||||
errors.add("name", _('Please enter group name (1 to 50 characters)'))
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def check_group_name
|
||||
errors.add('name', t(:contactgroup_name_invalid)) unless self.name =~ /^.{1,50}$/i
|
||||
end
|
||||
|
||||
def check_group_id
|
||||
if ContactGroup.find_first(["name = '#{name}' and customer_id = #{user_id}"])
|
||||
errors.add("name", _('Please enter group name (1 to 50 characters)'))
|
||||
end
|
||||
end
|
||||
|
||||
def validate_on_update
|
||||
if ContactGroup.find_first(["name = '#{name}' and customer_id = #{user_id} and id <> #{id}"])
|
||||
errors.add("name", _('You already have contact group with this name'))
|
||||
end
|
||||
|
||||
def check_group_uniqness
|
||||
if ContactGroup.find_first(["name = '#{name}' and customer_id = #{user_id} and id <> #{id}"])
|
||||
errors.add("name", _('You already have contact group with this name'))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
0
app/models/customer.rb
Normal file → Executable file
0
app/models/customer.rb
Normal file → Executable file
0
app/models/mail_pref.rb
Normal file → Executable file
0
app/models/mail_pref.rb
Normal file → Executable file
Loading…
Add table
Add a link
Reference in a new issue