Created
January 21, 2009 12:28
-
-
Save tbbooher/49962 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest/sha1' | |
class User < ActiveRecord::Base | |
attr_accessor :password_confirmation | |
# ||||||||||||||||||||||||||||||||||||||||||| | |
# ASSOCIATIONS | |
# ||||||||||||||||||||||||||||||||||||||||||| | |
has_many :franchises, :dependent => :destroy | |
has_many :measurements, :dependent => :destroy | |
#has_many :boot_camps, :through => :registrations | |
has_many :orders, :dependent => :destroy | |
has_and_belongs_to_many :roles | |
has_many :registrations, :through => :orders, | |
:uniq => true, :dependent => :destroy | |
# :include => :boot_camp | |
has_many :meeting_users, :dependent => :destroy | |
has_many :exertions, :through => :meeting_users, :dependent => :destroy | |
# ||||||||||||||||||||||||||||||||||||||||||| | |
# AGGREGATIONS | |
# ||||||||||||||||||||||||||||||||||||||||||| | |
# composed_of :height, | |
# :mapping => [%w{height_feet feet}*12 + %w{height_inches inches}] | |
# ||||||||||||||||||||||||||||||||||||||||||| | |
# VALIDATIONS | |
# ||||||||||||||||||||||||||||||||||||||||||| | |
validates_presence_of :user_name, :first_name, :last_name, :date_of_birth, :occupation, | |
:street_address1, :city, :us_state, :primary_phone, :email_address | |
validates_length_of :city, :street_address1, :in => 2..255 | |
# TODO we need to figure out how to work with the password | |
# validates_length_of :password, :in => 5..50 | |
# validates_length_of :city, :in => 2..255 | |
validates_numericality_of :weight, :height_feet, :height_inches | |
validates_format_of :primary_phone, :with => /[0-9\-\.]+/i | |
validates_format_of :email_address, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i #/^.*@.*\..*$/i | |
validates_multiparameter_assignments :message => " is not entered correctly." | |
validates_uniqueness_of :user_name | |
validates_confirmation_of :password | |
GENDER = [ | |
# Displayed stored in db | |
["male","1"], | |
["female","2"] | |
] | |
VETERAN_STATUS = [ | |
# DISP # IN DB | |
['No previous camps',0], # no vet | |
['One to three camps',1], # vet | |
['Four or more camps',2] #super-vet | |
] | |
TSHIRT_SIZES = [ | |
# Displayed stored in db | |
[ "Small", "s" ], | |
[ "Medium", "m"], | |
[ "Large", "l" ], | |
[ "Extra Large", "xl"] | |
] | |
def validate | |
errors.add_to_base("Missing password") if hashed_password.blank? | |
end | |
def self.authenticate(user_name, password) | |
user = self.find_by_user_name(user_name) | |
if user | |
expected_password = encrypted_password(password, user.salt) | |
if user.hashed_password != expected_password | |
user = nil | |
end | |
end | |
user | |
end | |
# 'password' is a virtual attribute | |
def set_new_password(mypasswd) | |
@password = mypasswd | |
create_new_salt | |
self.hashed_password = User.encrypted_password(self.password, self.salt) | |
end | |
def password | |
@password | |
end | |
def password=(pwd) | |
@password = pwd | |
# TODO ?? O.K. | |
return if pwd.blank? #or pwd.length < 6 | |
create_new_salt | |
self.hashed_password = User.encrypted_password(self.password, self.salt) | |
end | |
def after_destroy | |
if User.count.zero? | |
raise "Can't delete last user" | |
end | |
end | |
def self.find_potential(my_meeting) | |
self.find :all, | |
:joins => {:orders => {:registrations => :time_slot}}, | |
:conditions => ['time_slots.id = ?',my_meeting.time_slot.id] | |
end | |
def full_name | |
"#{first_name} #{last_name}" | |
end | |
def find_health_problems | |
health_array = load_health_array | |
known_health_conditions = [] | |
no_history = [] | |
health_array.each do |h| | |
if self[h] | |
known_health_conditions << h | |
else | |
no_history << h | |
end | |
end | |
[known_health_conditions, no_history] | |
end | |
def self.find_all_per_meeting(meeting_id) | |
self.find(:all, | |
:joins => {:orders => {:registrations => {:time_slot => :meetings}}},:conditions => ['meetings.id = ?',meeting_id]) | |
end | |
# from savage beast | |
# | |
# TODO would like to remove! | |
include SavageBeast::UserInit | |
#implement in your user model | |
def display_name | |
full_name | |
end | |
#implement in your user model | |
def admin? | |
if self.roles.detect{|role| | |
role.rights.detect{|right| | |
right.name == "Administer Database"}} | |
out = true | |
else | |
out = false | |
end | |
return out | |
end | |
# def female? | |
# if (u.sex == 'female') | |
# out = true | |
# else | |
# out = false | |
# end | |
# out | |
# end | |
def currently_online | |
session[:user_id] == self.id | |
end | |
def home_address | |
s = "#{self.street_address1}<br />\n" | |
s += "#{self.street_address2}<br />\n" unless self.street_address2.empty? | |
s += "#{self.city}, #{self.us_state} #{self.zip}" | |
end | |
def email | |
self.email_address | |
end | |
def age | |
# datediff | |
age = (Date.today.year - self.date_of_birth.year) | |
age -= 1 if Date.today.yday < self.date_of_birth.yday | |
age | |
end | |
def dob | |
self.date_of_birth.strftime("%d %b %y") | |
end | |
def sex | |
if self.gender == 1 | |
mysex = "male" | |
else | |
mysex = "female" | |
end | |
mysex | |
end | |
def demographic | |
if company.nil? or company.length < 1 or company.blank? | |
out = "#{self.age.to_s} year old #{sex} #{occupation} with an unspecified employer" | |
else | |
out = "#{self.age.to_s} year old #{sex} #{occupation} at #{company}" | |
end | |
out | |
end | |
def full_contact | |
#mydiv = "<div style=\"float:left;display:inline;\">" | |
mydiv = "<div>" | |
fc = "#{mydiv}#{self.mailingaddress}</div>" | |
fc += "#{mydiv}#{self.primary_phone}<br />" | |
fc += "#{self.secondary_phone}<br />" unless secondary_phone.empty? | |
fc += self.email_address | |
fc += "</div>" | |
fc += "#{mydiv}#{self.demographic}</div>" | |
fc | |
end | |
def full_contact_normal | |
#mydiv = "<div style=\"float:left;display:inline;\">" | |
fc = "#{self.mailingaddress_normal}\n" | |
fc += "#{self.primary_phone}\n" | |
fc += "#{self.secondary_phone}\n" unless secondary_phone.empty? | |
fc += "#{self.email_address}\n" | |
fc | |
end | |
def mailingaddress | |
ma = "#{self.street_address1}<br />" | |
ma += "#{self.street_address2}<br />" unless self.street_address2.empty? | |
ma += "#{self.city}, #{self.us_state} #{self.zip}" | |
ma | |
end | |
def mailingaddress_normal | |
ma = "#{self.street_address1}\n" | |
ma += "#{self.street_address2}\n" unless self.street_address2.empty? | |
ma += "#{self.city}, #{self.us_state} #{self.zip}\n" | |
ma | |
end | |
def time_slots | |
TimeSlot.find :all, :joins => {:meetings => {:meeting_users => :user}}, | |
:conditions => ['user_id = ?',self.id] | |
end | |
def register_for_boot_camps(cart,p) | |
cart.items.each do |item| | |
#u = User.find(:first) | |
UserBootCamp.create(:boot_camp_id => item.bootcamp.id, | |
:user_id => self.id, | |
:payment_method => p); | |
end | |
end | |
def usr_state_full_name | |
Location::US_STATES.detect{|fn,sn| sn == self.us_state}.first | |
end | |
private | |
def create_new_salt | |
self.salt = self.object_id.to_s + rand.to_s | |
end | |
def load_health_array | |
%w{history_of_heart_problems | |
cigarette_cigar_or_pipe_smoking | |
increased_blood_pressure | |
increased_total_blood_cholesterol | |
diabetes_mellitus | |
heart_problems_chest_pain_or_stroke | |
breathing_or_lung_problems | |
muscle_joint_or_back_disorder | |
hernia | |
chronic_illness | |
obesity | |
recent_surgery | |
pregnancy | |
difficulty_with_physical_exercise | |
advice_from_physician_not_to_exercise} | |
end | |
def self.encrypted_password(password, salt) | |
string_to_hash = password + "zimiasmixmas" + salt | |
Digest::SHA1.hexdigest(string_to_hash) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment