Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User < ActiveRecord::Base
- # validates_with UserValidator
- # Relationship for Role model
- has_and_belongs_to_many :roles
- has_many :posts
- has_many :comments
- # Include default devise modules. Others available are:
- # :token_authenticatable, :lockable, :timeoutable and :activatable
- devise :database_authenticatable, :registerable, :confirmable,
- :recoverable, :rememberable, :trackable, :validatable, :omniauthable
- # Setup accessible (or protected) attributes for your model
- attr_accessible :email, :password, :password_confirmation,
- :first_name, :last_name, :display_name, :hometown, :school_name, :school_city,
- :major_minor, :grad_date, :height, :weight, :cup_size, :eye_color, :age,
- :birth_date, :myspace_url, :facebook_url, :twitter_username
- validates :email, :presence => true, :uniqueness => true
- validate_on_update :validate_additional_attributes
- def validate_additional_attributes
- errors.add(:first_name, "can't be blank") if first_name.blank?
- errors.add(:last_name, "can't be blank") if last_name.blank?
- errors.add(:display_name, "can't be blank") if display_name.blank?
- if self.role?("hopeful") || self.role?("twelve")
- errors.add(:hometown, "can't be blank") if hometown.blank?
- errors.add(:school_name, "can't be blank") if school_name.blank?
- errors.add(:school_city, "can't be blank") if school_city.blank?
- errors.add(:major_minor, "can't be blank") if major_minor.blank?
- errors.add(:grade_date, "can't be blank") if grad_date.blank?
- errors.add(:height, "can't be blank") if height.blank?
- # errors.add(:weight, "can't be blank") if weight.blank?
- errors.add(:weight, "#{weight}must #{weight.to_s}be an #{weight.to_s.class}") unless weight.to_s =~ /\d+/
- errors.add(:cup_size, "can't be blank") if cup_size.blank?
- errors.add(:eye_color, "can't be blank") if eye_color.blank?
- # errors.add(:age, "can't be blank") if age.blank?
- errors.add(:age, "must be an integer") unless age.to_s =~ /\d+/
- errors.add(:birth_date, "can't be blank") if birth_date.blank?
- end
- end
- # Accessor function for checking a specific role in a user's list of roles
- def role?(role)
- return !!self.roles.find_by_name(role.to_s)
- end
- # Omniauth Callback Controller uses this to check for a pre-existing e-mail in Tempe12 user table
- def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
- data = access_token['extra']['user_hash']
- if user = User.find_by_email(data["email"])
- user
- else # Create a user with a stub password.
- User.create!(:email => data["email"], :password => Devise.friendly_token[0,20])
- end
- end
- # Called when a first time Tempe12 user wants to sign-in with Facebook.
- # The user is redirected to the registration page, but first, the information from their Facebook
- # account is pre-loaded into the registration form. Nice, right?!
- def self.new_with_session(params, session)
- super.tap do |user|
- if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["user_hash"]
- user.email = data["email"]
- end
- end
- end
- def name
- self.first_name + " " + self.last_name rescue "No Name"
- end
- def created_string
- created_at.strftime('%m/%e at %l:%M %p').gsub("12:00 PM", "Noon") unless created_at.nil?
- end
- def last_sign_in_string
- last_sign_in_at.strftime('%m/%e at %l:%M %p').gsub("12:00 PM", "Noon") unless last_sign_in_at.nil?
- end
- def confirmed?
- confirmed_at ? "Confirmed" : "Unconfirmed"
- end
- end
Add Comment
Please, Sign In to add comment