Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User < ActiveRecord::Base
- extend FriendlyId
- friendly_id :first_name, :use => :slugged
- # Include default devise modules. Others available are:
- # :token_authenticatable, :confirmable,
- # :lockable, :timeoutable and :omniauthable
- devise :database_authenticatable, :registerable,
- :recoverable, :rememberable, :trackable, :validatable, :omniauthable
- has_one :professor
- has_one :student
- has_many :educations
- after_create :create_professor
- after_create :create_student
- acts_as_messageable :table_name => "messages", # default 'messages'
- :required => :body, # default [:topic, :body]
- :class_name => "ActsAsMessageable::Message" # default "ActsAsMessageable::Message"
- # Setup accessible (or protected) attributes for your model
- attr_accessible :email, :password, :password_confirmation, :remember_me
- attr_accessible :first_name, :last_name, :city, :zip_code, :avatar_url
- # Validations
- validates :first_name, :presence => true
- validates :last_name, :presence => true
- # Returns a concat of first name and last name
- def full_name
- "#{first_name} #{last_name}"
- end
- def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
- data = access_token["extra"]["raw_info"]
- if user = self.find_by_email(data["email"])
- user
- else # Create a professor with a stub password.
- avatar_url = nil
- if defined?(access_token["info"]["image"])
- avatar_url = access_token["info"]["image"]
- end
- self.create(
- :email => data["email"],
- :password => Devise.friendly_token[0,20],
- :first_name => data["first_name"],
- :last_name => data["last_name"],
- :avatar_url => avatar_url,
- )
- end
- end
- # Stores the omniauth information into the authentications table
- def apply_omniauth(auth)
- # In previous omniauth, 'user_info' was used in place of 'raw_info'
- #self.email = auth['extra']['raw_info']['email']
- # Again, saving token is optional. If you haven't created the column in authentications table, this will fail
- #self.authentications.build(:provider => auth['provider'], :uid => auth['uid'], :token => auth['credentials']['token'])
- end
- # Don't use this method yet.
- def self.cached_professor
- cache_key = "active_record:cached_user:#{self.id}"
- Rails.cache.fetch cache_key, :expires_in => 1.day do
- self.professor
- end
- end
- private
- def create_professor
- logger.debug "CREATE PROFESSOR"
- logger.debug self.inspect
- self.professor = Professor.create
- end
- def create_student
- self.student = Student.create
- end
- end
Add Comment
Please, Sign In to add comment