Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## error
- == 5 AddDefaultAdmin: migrating ===============================================
- rake aborted!
- uninitialized constant User::Locations
- ## 005_add_default_admin.rb
- class AddDefaultAdmin < ActiveRecord::Migration
- def self.up
- user = User.new
- user.login = "evan"
- user.email = "evan.alter@gmail.com"
- user.password = "test"
- user.password_confirmation = "test"
- user.default_location = Location.new(:zipcode => "78704", :name => "Default")
- user.profile = Profile.new
- user.roles << Role.find_by_name('admin')
- user.save(false)
- user.activate!
- end
- def self.down
- user = User.find_by_login('evan')
- user.default_location.destory
- user.profile.destroy
- user.roles.destory
- user.destory
- end
- end
- ## location.rb
- class Location < ActiveRecord::Base
- belongs_to :user
- acts_as_mappable
- before_validation_on_create :geocode_address
- validates_uniqueness_of :name, :message => "You already have a location with that same name", :scope => "user_id"
- #validates_presence_of :lat, :lng
- private
- def geocode_address
- field = :full_address
- if is_zipcode?
- address = self.zipcode
- field = :zipcode
- else
- return invalid_zipcode if self.full_address.nil?
- end
- geo=GeoKit::Geocoders::MultiGeocoder.geocode(address)
- if geo.success
- self.lat = geo.lat
- self.lng = geo.lng
- self.provider = geo.provider
- self.street_address ||= geo.street_address
- self.city ||= geo.city
- self.state ||= geo.state
- self.country_code ||= geo.country_code
- self.zipcode ||= geo.zip
- self.full_address = geo.full_address
- else
- errors.add(field, "could not be found")
- end
- geo.success
- end
- def is_zipcode?
- self.zipcode =~ /^[0-9]{5}$/
- end
- def invalid_zipcode
- errors.add(:zipcode, "is invalid")
- false
- end
- end
- ## user.rb
- require 'digest/sha1'
- class User < ActiveRecord::Base
- #acts_as_ferret :fields => [:first_name, :last_name]
- #Relationships
- has_and_belongs_to_many :roles
- has_many :locations
- has_one :default_location, :class_name => 'Locations'
- has_one :profile
- #TODO create online_method so users can view friends online
- has_many_friends #:online_method
- #For values that must be used multiple places
- VALID_PROFILE_STATUSES = %w(public, logged_in_users, friends_only)
- # Virtual attribute for the unencrypted password
- attr_accessor :password
- validates_presence_of :login, :email
- validates_presence_of :password
- validates_presence_of :password_confirmation
- validates_length_of :password, :within => 4..40, :if => :password_present?
- validates_confirmation_of :password, :if => :password_present?
- validates_length_of :login, :within => 3..40, :if => :login_present?
- validates_length_of :email, :within => 3..100, :if => :email_present?
- validates_uniqueness_of :login, :email, :case_sensitive => false
- validates_inclusion_of :profile_status, :in => VALID_PROFILE_STATUSES
- validates_each :birthdate do |record, attr, value|
- record.errors.add(attr,"You're not old enough.") if value > Date.new((Date.today.year - 21),(Date.today.month),(Date.today.day))
- end
- before_save :encrypt_password
- # prevents a user from submitting a crafted form that bypasses activation
- # anything else you want your user to change should be added here.
- attr_accessible :login, :email, :password, :password_confirmation, :birthdate, :profile, :default_location
- # Not needed since attr_accessible prevents anything what is provided to be accessed
- #attr_protected :roles
- acts_as_state_machine :initial => :pending
- state :passive
- state :pending, :enter => :make_activation_code
- state :active, :enter => :do_activate
- state :suspended
- state :deleted, :enter => :do_delete
- event :register do
- transitions :from => :passive, :to => :pending, :guard => Proc.new {|u| !(u.crypted_password.blank? && u.password.blank?) }
- end
- event :activate do
- transitions :from => :pending, :to => :active
- end
- event :suspend do
- transitions :from => [:passive, :pending, :active], :to => :suspended
- end
- event :delete do
- transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
- end
- event :unsuspend do
- transitions :from => :suspended, :to => :active, :guard => Proc.new {|u| !u.activated_at.blank? }
- transitions :from => :suspended, :to => :pending, :guard => Proc.new {|u| !u.activation_code.blank? }
- transitions :from => :suspended, :to => :passive
- end
- # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
- def self.authenticate(login, password)
- u = find_in_state :first, :active, :conditions => {:login => login} # need to get the salt
- u && u.authenticated?(password) ? u : nil
- end
- # Encrypts some data with the salt.
- def self.encrypt(password, salt)
- Digest::SHA1.hexdigest("--#{salt}--#{password}--")
- end
- # Encrypts the password with the user salt
- def encrypt(password)
- self.class.encrypt(password, salt)
- end
- def authenticated?(password)
- crypted_password == encrypt(password)
- end
- def remember_token?
- remember_token_expires_at && Time.now.utc < remember_token_expires_at
- end
- # These create and unset the fields required for remembering users between browser closes
- def remember_me
- remember_me_for 2.weeks
- end
- def remember_me_for(time)
- remember_me_until time.from_now.utc
- end
- def remember_me_until(time)
- self.remember_token_expires_at = time
- self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
- save(false)
- end
- def forget_me
- self.remember_token_expires_at = nil
- self.remember_token = nil
- save(false)
- end
- def forgot_password
- @forgotten_password = true
- self.make_password_reset_code
- end
- def reset_password
- # First update the password_reset_code before setting the
- # reset_password flag to avoid duplicate email notifications.
- update_attribute(:password_reset_code, nil)
- @reset_password = true
- end
- #used in user_observer
- def recently_forgot_password?
- @forgotten_password
- end
- def recently_reset_password?
- @reset_password
- end
- def self.find_for_forget(email)
- find_in_state :first, :active, :conditions => {:email => email}
- end
- def self.find_active(id)
- find_in_state :first, :active, :conditions => {:id => id}
- end
- # Returns true if the user has just been activated.
- def recently_activated?
- @activated
- end
- # has_role? simply needs to return true or false whether a user has a role or not.
- # It may be a good idea to have "admin" roles return true always
- def has_role?(role_in_question)
- @_list ||= self.roles.collect(&:name)
- return true if @_list.include?("admin")
- (@_list.include?(role_in_question.to_s) )
- end
- #
- def show_profile_to?(user)
- if self.active?
- return true if self.profile_status == 'public'
- return false if user.nil?
- if user.is_admin? || self == user
- return true
- else
- case self.profile_status
- when 'logged_in_users'
- return true
- when 'friends_only'
- return self.is_friends_with?(user)
- end
- end
- elsif user && user.is_admin?
- return true
- else
- return false
- end
- end
- def is_admin?
- return self.has_role?('admin')
- end
- # Checks that the user is an admin but the is not the user being passed in
- # Useful for things you don't want users doing to themselves like disabling
- # TODO This may need to be refactored to use is_current_user?(user)
- def is_admin_but_not?(user)
- self.is_admin? && self != user
- end
- # This function extends has_many_friends and returns true if the user still
- # hasn't responded to the friendship request of the "friend"
- def pending_friend_for_me?(friend)
- self.pending_friends_for_me.include?(friend)
- end
- # This function returns true if the user is still waiting
- # for an answer to the friendship request
- def pending_friend_by_me?(friend)
- self.pending_friends_by_me.include?(friend)
- end
- def name
- self.login
- end
- # TODO Add ablity for Users to block each other
- protected
- # before filter
- def encrypt_password
- return if password.blank?
- self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
- self.crypted_password = encrypt(password)
- end
- def password_present?
- !password.blank?
- end
- def login_present?
- !login.blank?
- end
- def email_present?
- !email.blank?
- end
- def make_activation_code
- self.deleted_at = nil
- self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
- end
- def make_password_reset_code
- self.password_reset_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
- end
- def make_email_update_code
- self.email_update_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
- end
- def do_delete
- self.deleted_at = Time.now.utc
- end
- def do_activate
- @activated = true
- self.activated_at = Time.now.utc
- self.deleted_at = self.activation_code = nil
- end
- end
Add Comment
Please, Sign In to add comment