Guest User

Untitled

a guest
Feb 18th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. class User < ActiveRecord::Base
  2. # validates_with UserValidator
  3. # Relationship for Role model
  4. has_and_belongs_to_many :roles
  5. has_many :posts
  6. has_many :comments
  7.  
  8. # Include default devise modules. Others available are:
  9. # :token_authenticatable, :lockable, :timeoutable and :activatable
  10. devise :database_authenticatable, :registerable, :confirmable,
  11. :recoverable, :rememberable, :trackable, :validatable, :omniauthable
  12.  
  13. # Setup accessible (or protected) attributes for your model
  14. attr_accessible :email, :password, :password_confirmation,
  15. :first_name, :last_name, :display_name, :hometown, :school_name, :school_city,
  16. :major_minor, :grad_date, :height, :weight, :cup_size, :eye_color, :age,
  17. :birth_date, :myspace_url, :facebook_url, :twitter_username
  18.  
  19. validates :email, :presence => true, :uniqueness => true
  20.  
  21. validate_on_update :validate_additional_attributes
  22.  
  23. def validate_additional_attributes
  24. errors.add(:first_name, "can't be blank") if first_name.blank?
  25. errors.add(:last_name, "can't be blank") if last_name.blank?
  26. errors.add(:display_name, "can't be blank") if display_name.blank?
  27.  
  28. if self.role?("hopeful") || self.role?("twelve")
  29. errors.add(:hometown, "can't be blank") if hometown.blank?
  30. errors.add(:school_name, "can't be blank") if school_name.blank?
  31. errors.add(:school_city, "can't be blank") if school_city.blank?
  32. errors.add(:major_minor, "can't be blank") if major_minor.blank?
  33. errors.add(:grade_date, "can't be blank") if grad_date.blank?
  34. errors.add(:height, "can't be blank") if height.blank?
  35. # errors.add(:weight, "can't be blank") if weight.blank?
  36. errors.add(:weight, "#{weight}must #{weight.to_s}be an #{weight.to_s.class}") unless weight.to_s =~ /\d+/
  37. errors.add(:cup_size, "can't be blank") if cup_size.blank?
  38. errors.add(:eye_color, "can't be blank") if eye_color.blank?
  39. # errors.add(:age, "can't be blank") if age.blank?
  40. errors.add(:age, "must be an integer") unless age.to_s =~ /\d+/
  41. errors.add(:birth_date, "can't be blank") if birth_date.blank?
  42. end
  43. end
  44.  
  45. # Accessor function for checking a specific role in a user's list of roles
  46. def role?(role)
  47. return !!self.roles.find_by_name(role.to_s)
  48. end
  49.  
  50. # Omniauth Callback Controller uses this to check for a pre-existing e-mail in Tempe12 user table
  51. def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
  52. data = access_token['extra']['user_hash']
  53. if user = User.find_by_email(data["email"])
  54. user
  55. else # Create a user with a stub password.
  56. User.create!(:email => data["email"], :password => Devise.friendly_token[0,20])
  57. end
  58. end
  59.  
  60. # Called when a first time Tempe12 user wants to sign-in with Facebook.
  61. # The user is redirected to the registration page, but first, the information from their Facebook
  62. # account is pre-loaded into the registration form. Nice, right?!
  63. def self.new_with_session(params, session)
  64. super.tap do |user|
  65. if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["user_hash"]
  66. user.email = data["email"]
  67. end
  68. end
  69. end
  70.  
  71. def name
  72. self.first_name + " " + self.last_name rescue "No Name"
  73. end
  74.  
  75. def created_string
  76. created_at.strftime('%m/%e at %l:%M %p').gsub("12:00 PM", "Noon") unless created_at.nil?
  77. end
  78.  
  79. def last_sign_in_string
  80. last_sign_in_at.strftime('%m/%e at %l:%M %p').gsub("12:00 PM", "Noon") unless last_sign_in_at.nil?
  81. end
  82.  
  83. def confirmed?
  84. confirmed_at ? "Confirmed" : "Unconfirmed"
  85. end
  86. end
Add Comment
Please, Sign In to add comment