Guest User

Untitled

a guest
Nov 24th, 2018
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. class User < ActiveRecord::Base
  2. extend FriendlyId
  3. friendly_id :first_name, :use => :slugged
  4.  
  5. # Include default devise modules. Others available are:
  6. # :token_authenticatable, :confirmable,
  7. # :lockable, :timeoutable and :omniauthable
  8. devise :database_authenticatable, :registerable,
  9. :recoverable, :rememberable, :trackable, :validatable, :omniauthable
  10.  
  11. has_one :professor
  12. has_one :student
  13. has_many :educations
  14.  
  15. after_create :create_professor
  16. after_create :create_student
  17.  
  18. acts_as_messageable :table_name => "messages", # default 'messages'
  19. :required => :body, # default [:topic, :body]
  20. :class_name => "ActsAsMessageable::Message" # default "ActsAsMessageable::Message"
  21.  
  22. # Setup accessible (or protected) attributes for your model
  23. attr_accessible :email, :password, :password_confirmation, :remember_me
  24. attr_accessible :first_name, :last_name, :city, :zip_code, :avatar_url
  25.  
  26. # Validations
  27. validates :first_name, :presence => true
  28. validates :last_name, :presence => true
  29.  
  30. # Returns a concat of first name and last name
  31. def full_name
  32. "#{first_name} #{last_name}"
  33. end
  34.  
  35. def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
  36. data = access_token["extra"]["raw_info"]
  37. if user = self.find_by_email(data["email"])
  38. user
  39. else # Create a professor with a stub password.
  40. avatar_url = nil
  41. if defined?(access_token["info"]["image"])
  42. avatar_url = access_token["info"]["image"]
  43. end
  44. self.create(
  45. :email => data["email"],
  46. :password => Devise.friendly_token[0,20],
  47. :first_name => data["first_name"],
  48. :last_name => data["last_name"],
  49. :avatar_url => avatar_url,
  50. )
  51. end
  52. end
  53.  
  54. # Stores the omniauth information into the authentications table
  55. def apply_omniauth(auth)
  56. # In previous omniauth, 'user_info' was used in place of 'raw_info'
  57. #self.email = auth['extra']['raw_info']['email']
  58. # Again, saving token is optional. If you haven't created the column in authentications table, this will fail
  59. #self.authentications.build(:provider => auth['provider'], :uid => auth['uid'], :token => auth['credentials']['token'])
  60. end
  61.  
  62. # Don't use this method yet.
  63.  
  64. def self.cached_professor
  65. cache_key = "active_record:cached_user:#{self.id}"
  66. Rails.cache.fetch cache_key, :expires_in => 1.day do
  67. self.professor
  68. end
  69. end
  70.  
  71. private
  72.  
  73. def create_professor
  74. logger.debug "CREATE PROFESSOR"
  75. logger.debug self.inspect
  76. self.professor = Professor.create
  77. end
  78.  
  79. def create_student
  80. self.student = Student.create
  81. end
  82.  
  83. end
Add Comment
Please, Sign In to add comment