Guest User

Untitled

a guest
Feb 28th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. require 'digest/sha1'
  2.  
  3. class User < ActiveRecord::Base
  4.  
  5. attr_accessor :password_confirmation
  6. # |||||||||||||||||||||||||||||||||||||||||||
  7. # ASSOCIATIONS
  8. # |||||||||||||||||||||||||||||||||||||||||||
  9. has_many :franchises, :dependent => :destroy
  10. has_many :measurements, :dependent => :destroy
  11. #has_many :boot_camps, :through => :registrations
  12. has_many :orders, :dependent => :destroy
  13. has_and_belongs_to_many :roles
  14. has_many :registrations, :through => :orders,
  15. :uniq => true, :dependent => :destroy
  16. # :include => :boot_camp
  17. has_many :meeting_users, :dependent => :destroy
  18. has_many :exertions, :through => :meeting_users, :dependent => :destroy
  19. # |||||||||||||||||||||||||||||||||||||||||||
  20. # AGGREGATIONS
  21. # |||||||||||||||||||||||||||||||||||||||||||
  22. # composed_of :height,
  23. # :mapping => [%w{height_feet feet}*12 + %w{height_inches inches}]
  24. # |||||||||||||||||||||||||||||||||||||||||||
  25. # VALIDATIONS
  26. # |||||||||||||||||||||||||||||||||||||||||||
  27. validates_presence_of :user_name, :first_name, :last_name, :date_of_birth, :occupation,
  28. :street_address1, :city, :us_state, :primary_phone, :email_address
  29. validates_length_of :city, :street_address1, :in => 2..255
  30. # TODO we need to figure out how to work with the password
  31. # validates_length_of :password, :in => 5..50
  32. # validates_length_of :city, :in => 2..255
  33. validates_numericality_of :weight, :height_feet, :height_inches
  34. validates_format_of :primary_phone, :with => /[0-9\-\.]+/i
  35. validates_format_of :email_address, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i #/^.*@.*\..*$/i
  36. validates_multiparameter_assignments :message => " is not entered correctly."
  37.  
  38. validates_uniqueness_of :user_name
  39. validates_confirmation_of :password
  40.  
  41. GENDER = [
  42. # Displayed stored in db
  43. ["male","1"],
  44. ["female","2"]
  45. ]
  46.  
  47. VETERAN_STATUS = [
  48. # DISP # IN DB
  49. ['No previous camps',0], # no vet
  50. ['One to three camps',1], # vet
  51. ['Four or more camps',2] #super-vet
  52. ]
  53.  
  54. TSHIRT_SIZES = [
  55. # Displayed stored in db
  56. [ "Small", "s" ],
  57. [ "Medium", "m"],
  58. [ "Large", "l" ],
  59. [ "Extra Large", "xl"]
  60. ]
  61.  
  62. def validate
  63. errors.add_to_base("Missing password") if hashed_password.blank?
  64. end
  65.  
  66. def self.authenticate(user_name, password)
  67. user = self.find_by_user_name(user_name)
  68. if user
  69. expected_password = encrypted_password(password, user.salt)
  70. if user.hashed_password != expected_password
  71. user = nil
  72. end
  73. end
  74. user
  75. end
  76.  
  77. # 'password' is a virtual attribute
  78. def set_new_password(mypasswd)
  79. @password = mypasswd
  80. create_new_salt
  81. self.hashed_password = User.encrypted_password(self.password, self.salt)
  82. end
  83.  
  84. def password
  85. @password
  86. end
  87.  
  88. def password=(pwd)
  89. @password = pwd
  90. # TODO ?? O.K.
  91. return if pwd.blank? #or pwd.length < 6
  92. create_new_salt
  93. self.hashed_password = User.encrypted_password(self.password, self.salt)
  94. end
  95.  
  96. def after_destroy
  97. if User.count.zero?
  98. raise "Can't delete last user"
  99. end
  100. end
  101.  
  102. def self.find_potential(my_meeting)
  103. self.find :all,
  104. :joins => {:orders => {:registrations => :time_slot}},
  105. :conditions => ['time_slots.id = ?',my_meeting.time_slot.id]
  106. end
  107.  
  108. def full_name
  109. "#{first_name} #{last_name}"
  110. end
  111.  
  112. def find_health_problems
  113. health_array = load_health_array
  114. known_health_conditions = []
  115. no_history = []
  116. health_array.each do |h|
  117. if self[h]
  118. known_health_conditions << h
  119. else
  120. no_history << h
  121. end
  122. end
  123. [known_health_conditions, no_history]
  124. end
  125.  
  126. def self.find_all_per_meeting(meeting_id)
  127. self.find(:all,
  128. :joins => {:orders => {:registrations => {:time_slot => :meetings}}},:conditions => ['meetings.id = ?',meeting_id])
  129. end
  130.  
  131. # from savage beast
  132. #
  133. # TODO would like to remove!
  134. include SavageBeast::UserInit
  135. #implement in your user model
  136. def display_name
  137. full_name
  138. end
  139.  
  140. #implement in your user model
  141. def admin?
  142. if self.roles.detect{|role|
  143. role.rights.detect{|right|
  144. right.name == "Administer Database"}}
  145. out = true
  146. else
  147. out = false
  148. end
  149. return out
  150. end
  151.  
  152. # def female?
  153. # if (u.sex == 'female')
  154. # out = true
  155. # else
  156. # out = false
  157. # end
  158. # out
  159. # end
  160.  
  161. def currently_online
  162. session[:user_id] == self.id
  163. end
  164.  
  165. def home_address
  166. s = "#{self.street_address1}<br />\n"
  167. s += "#{self.street_address2}<br />\n" unless self.street_address2.empty?
  168. s += "#{self.city}, #{self.us_state} #{self.zip}"
  169. end
  170.  
  171. def email
  172. self.email_address
  173. end
  174.  
  175. def age
  176. # datediff
  177. age = (Date.today.year - self.date_of_birth.year)
  178. age -= 1 if Date.today.yday < self.date_of_birth.yday
  179. age
  180. end
  181.  
  182. def dob
  183. self.date_of_birth.strftime("%d %b %y")
  184. end
  185.  
  186. def sex
  187. if self.gender == 1
  188. mysex = "male"
  189. else
  190. mysex = "female"
  191. end
  192. mysex
  193. end
  194.  
  195. def demographic
  196. if company.nil? or company.length < 1 or company.blank?
  197. out = "#{self.age.to_s} year old #{sex} #{occupation} with an unspecified employer"
  198. else
  199. out = "#{self.age.to_s} year old #{sex} #{occupation} at #{company}"
  200. end
  201. out
  202. end
  203.  
  204. def full_contact
  205. #mydiv = "<div style=\"float:left;display:inline;\">"
  206. mydiv = "<div>"
  207. fc = "#{mydiv}#{self.mailingaddress}</div>"
  208. fc += "#{mydiv}#{self.primary_phone}<br />"
  209. fc += "#{self.secondary_phone}<br />" unless secondary_phone.empty?
  210. fc += self.email_address
  211. fc += "</div>"
  212. fc += "#{mydiv}#{self.demographic}</div>"
  213. fc
  214. end
  215.  
  216. def full_contact_normal
  217. #mydiv = "<div style=\"float:left;display:inline;\">"
  218. fc = "#{self.mailingaddress_normal}\n"
  219. fc += "#{self.primary_phone}\n"
  220. fc += "#{self.secondary_phone}\n" unless secondary_phone.empty?
  221. fc += "#{self.email_address}\n"
  222. fc
  223. end
  224.  
  225. def mailingaddress
  226. ma = "#{self.street_address1}<br />"
  227. ma += "#{self.street_address2}<br />" unless self.street_address2.empty?
  228. ma += "#{self.city}, #{self.us_state} #{self.zip}"
  229. ma
  230. end
  231.  
  232. def mailingaddress_normal
  233. ma = "#{self.street_address1}\n"
  234. ma += "#{self.street_address2}\n" unless self.street_address2.empty?
  235. ma += "#{self.city}, #{self.us_state} #{self.zip}\n"
  236. ma
  237. end
  238.  
  239. def time_slots
  240. TimeSlot.find :all, :joins => {:meetings => {:meeting_users => :user}},
  241. :conditions => ['user_id = ?',self.id]
  242. end
  243.  
  244. def register_for_boot_camps(cart,p)
  245. cart.items.each do |item|
  246. #u = User.find(:first)
  247. UserBootCamp.create(:boot_camp_id => item.bootcamp.id,
  248. :user_id => self.id,
  249. :payment_method => p);
  250. end
  251. end
  252.  
  253. def usr_state_full_name
  254. Location::US_STATES.detect{|fn,sn| sn == self.us_state}.first
  255. end
  256.  
  257. private
  258.  
  259. def create_new_salt
  260. self.salt = self.object_id.to_s + rand.to_s
  261. end
  262.  
  263. def load_health_array
  264. %w{history_of_heart_problems
  265. cigarette_cigar_or_pipe_smoking
  266. increased_blood_pressure
  267. increased_total_blood_cholesterol
  268. diabetes_mellitus
  269. heart_problems_chest_pain_or_stroke
  270. breathing_or_lung_problems
  271. muscle_joint_or_back_disorder
  272. hernia
  273. chronic_illness
  274. obesity
  275. recent_surgery
  276. pregnancy
  277. difficulty_with_physical_exercise
  278. advice_from_physician_not_to_exercise}
  279. end
  280.  
  281. def self.encrypted_password(password, salt)
  282. string_to_hash = password + "zimiasmixmas" + salt
  283. Digest::SHA1.hexdigest(string_to_hash)
  284. end
  285.  
  286. end
Add Comment
Please, Sign In to add comment