Guest User

Untitled

a guest
Jun 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. class Contact < ActiveRecord::Base
  2. belongs_to :member
  3. belongs_to :completed_form
  4. belongs_to :recent_form
  5. belongs_to :phone_type
  6.  
  7. # VALIDATIONS
  8. def before_validation_on_create
  9. self.phone = phone.gsub(/[^0-9]/, "")
  10. end
  11.  
  12. after_create :map_contact_to_member
  13.  
  14. validates_presence_of :city, :message => "^please enter a City"
  15. validates_length_of :state, :is => 2, :wrong_length => "^please use the 2 character abbreviation for State"
  16. validates_length_of :zip_code, :is => 5, :wrong_length => "^Zip Code should be {{count}} digits"
  17. validates_numericality_of :zip_code, :only_integer => true, :message => "^Zip Code should be composed only of numbers"
  18. validates_length_of :phone, :is => 10, :allow_blank => true, :wrong_length => "^Phone Number needs to be {{count}} digits"
  19. validates_presence_of :phone_type, :if => :phone?, :message => "^if providing a Phone Number please select a Phone Type"
  20.  
  21. #Custom Methods
  22.  
  23. protected
  24. def map_contact_to_member(id)
  25. update_attribute "member_id", '#{id}'
  26. end
  27. end
  28.  
  29. ---------------------------------------------
  30. class Member < ActiveRecord::Base
  31. #forms
  32. #allows user to access all forms completed by the member
  33. has_many :completed_forms, :as => :data_collection
  34. accepts_nested_attributes_for :completed_forms
  35. has_one :recent_form, :as => :data_collection, :class_name => "CompletedForm", :order => "updated_at DESC"
  36.  
  37. #aliases
  38. has_many :aliases
  39.  
  40. #contact associations
  41. #need this information to do traces but can also use the methods on the completed form to see individual
  42. has_many :contacts
  43. has_many :contacts, :through => :completed_forms
  44. accepts_nested_attributes_for :contacts
  45. #need to decide if this information really needs to be accessed from members or only relative to the actual forms they complete
  46. has_many :messenger_usages, :through => :completed_forms
  47. has_many :online_usages, :through => :completed_forms
  48. has_many :permitted_contacts, :through => :completed_forms
  49.  
  50. #demographic associations
  51. #allows user to access demographic information based on the set of forms it originates from
  52. has_many :demographics
  53.  
  54. has_many :ethnic_races, :through => :demographics
  55. has_many :sexes, :through => :demographics
  56. has_many :serostatuses, :through => :demographics
  57. has_many :genders, :through => :demographics
  58. has_many :gender_expressions, :through => :demographics
  59. has_many :sexual_orientations, :through => :demographics
  60.  
  61. # VALIDATIONS
  62.  
  63. validates_presence_of :first_name, :message => "^Please enter member's First Name"
  64. validates_presence_of :last_name, :message => "^Please enter member's Last Name"
  65.  
  66. def age
  67. age = Date.today.year - self.dob.year
  68. if Date.today.month < self.dob.month || (Date.today.month == self.dob.month && self.dob.day >= Date.today.day)
  69. age = age - 1
  70. end
  71. return age
  72. end
  73.  
  74. def fullName
  75. fullName = "#{self.first_name}" " " "#{self.middle_name}" " " "#{self.last_name}"
  76. end
  77. end
  78.  
  79. ---------------------------------------------
  80. class CompletedForm < ActiveRecord::Base
  81. belongs_to :data_collection, :polymorphic => true
  82. belongs_to :form_version
  83. has_one :contact
  84. has_many :messenger_usages
  85. has_many :online_usages
  86. has_many :permitted_contacts
  87. has_one :demographic
  88. accepts_nested_attributes_for :contact
  89. accepts_nested_attributes_for :demographic
  90. end
Add Comment
Please, Sign In to add comment