Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'digest/sha1'
- class User < ActiveRecord::Base
- # Person.find(:all, :include => :addresses).map{|p| p.addresses}.flatten
- attr :file_data
- attr_accessor :password_confirmation, :file_data, :content_type
- #...
- # joins
- #...
- has_many :venues,
- :foreign_key => "created_by",
- :dependent => :destroy
- has_many :created_events,
- :class_name => "Event",
- :foreign_key => "owner_id"
- has_many :events,
- :through => :event_acts,
- :dependent => :destroy
- has_many :received_band_comments,
- :class_name => "UserComment",
- :dependent => :destroy
- has_many :posted_band_comments,
- :foreign_key => "owner_id",
- :class_name => "UserComment",
- :dependent => :destroy
- has_one :profile_stat,
- :class_name => "ProfileStat",
- :dependent => :destroy
- has_many :band_invites, :dependent => :destroy
- has_many :venue_comments, :dependent => :destroy
- has_many :venue_ratings, :dependent => :destroy
- # ...
- # validation
- # ...
- validates_uniqueness_of :email, :message => "an account is already registered with this email address", :if => Proc.new {|user| !user.email.blank? }
- validates_presence_of :username
- validates_uniqueness_of :username, :if => Proc.new {|user| !user.username.blank? }
- validates_presence_of :name
- validates_presence_of :email
- validates_presence_of :password, :if => Proc.new {|user| (user.new_record? || !user.password.blank?) ==true }
- validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "looks kinda funny to me", :if => Proc.new {|user| !user.email.blank? }
- validates_confirmation_of :password, :if => Proc.new {|user| (user.new_record? || !user.password.blank?) ==true }
- validates_length_of :name, :maximum => 100, :if => Proc.new {|user| !user.name.blank? }
- validates_length_of :username, :within => 4..20, :if => Proc.new {|user| !user.username.blank? }
- validates_length_of :email, :maximum => 100, :if => Proc.new {|user| !user.email.blank? }
- validates_length_of :home_postcode, :maximum => 8
- validates_length_of :password, :within => 4..10, :if => Proc.new {|user| (user.new_record? || !user.password.blank?) ==true }
- validates_length_of :url, :maximum => 100, :if => Proc.new {|user| !user.url.blank? }
- validates_length_of :url_video, :maximum => 100, :if => Proc.new {|user| !user.url_video.blank? }
- validates_length_of :url_myspace, :maximum =>100, :if => Proc.new {|user| !user.url_myspace.blank? }
- validates_length_of :url_full_track, :maximum => 100, :if => Proc.new {|user| !user.url_full_track.blank? }
- validates_format_of :content_type, :with => /^image/, :message => "--- you can only upload pictures", :if => Proc.new {|user| user.file_data && user.file_data.length>0 }
- def validate
- end
- #validates_format_of :content_type, :with => /^image/, :message => "--- you can only upload pictures", :if => Proc.new {|user| user.file_data && user.file_data.length>0 }
- # ...
- # trigger config
- # ..
- before_destroy :dont_destroy_infrid_or_jamie
- before_save :set_has_file
- after_save :process
- #after_destroy :cleanup
- before_create :before_create
- # ...
- # trigger methods
- # ...
- def dont_destroy_infrid_or_jamie
- raise "can't destroy george or eoin" if self.username =="infrid" || self.username =="jamie"
- end
- def before_create
- # generate my guid
- self.user_status = STATUS_WAITING_EMAIL_APPROVAL
- self.guid = UUID.new
- end
- def before_update
- end
- def after_create
- @hashed_password = nil
- end
- #...
- # accessors,
- #...
- def password
- @password
- end
- def password=(pwd)
- return if pwd.blank?
- @password = pwd
- create_new_salt
- self.hashed_password = User.encrypted_password(self.password, self.salt)
- end
- #...
- # data methods
- #...
- def self.authenticate(name, password)
- user = self.find_by_username(name)
- if user
- expected_password = encrypted_password(password, user.salt)
- if user.hashed_password != expected_password
- user = nil
- end
- end
- user
- end
- def self.get_user_name(u_id)
- return if !u_id
- User.connection.select_value("select username from users where id = " + u_id.to_s)
- end
- def isAdmin?
- self.admin==5
- end
- def get_user_type_text
- USER_TYPES.index(self.user_type)
- end
- def get_user_status_text
- USER_STATUSES.index(self.user_status)
- end
- # this method tells us if we have file data attached.. could be an mp3, could be a thumbnail
- def set_has_file
- if self.file_data && self.file_data.length>0
- self.has_thumb=1
- end
- end
- #...
- # image url methods
- #...
- def get_thumb
- if self.has_thumb==1 then
- THUMBNAIL_PATH + "#{self.id}-thumb.jpg"
- else
- THUMBNAIL_PATH + "none.jpg"
- end
- end
- def thumbnail_url
- thumbnail_path.sub(/^public/, '' )
- end
- def path
- File.join(IMAGE_PATH, "#{self.id}-full.jpg")
- end
- def thumbnail_path
- File.join(THUMBNAIL_PATH, "#{self.id}-thumb.jpg")
- end
- #...
- # mp3 url methods
- #...
- def get_mp3
- if self.has_mp3==1 then
- MP3_PREVIEW_PATH + "#{self.id}-thumb.jpg"
- else
- nil
- end
- end
- def mp3_url
- mp3_path.sub(/^public/, '' )
- end
- def mp3_path
- File.join(MP3_PREVIEW_PATH, "#{self.id}-thumb.jpg")
- end
- #...
- #private
- #...
- private
- def self.encrypted_password(password, salt)
- string_to_hash = password + "h4rd3r" + salt # 'h4rd3r' makes it harder to guess
- Digest::SHA1.hexdigest(string_to_hash)
- end
- def create_new_salt
- self.salt = self.object_id.to_s + rand.to_s
- end
- def process
- if self.file_data && self.file_data.length>0 then
- create_directory
- cleanup
- #save_fullsize
- create_thumbnail
- self.file_data = nil
- end
- end
- def save_fullsize
- img = Magick::Image.from_blob(self.file_data.read )[0]
- img.write FILE_PATH + path
- end
- def create_thumbnail
- begin
- #img = Magick::Image.read(path).first
- img = Magick::Image.from_blob(self.file_data.read )[0]
- thumbnail = img.thumbnail(*THUMB_MAX_SIZE)
- thumbnail.write FILE_PATH + thumbnail_path
- rescue
- logger.error("create_thumnail", "couldn't save image")
- end
- end
- def create_directory
- FileUtils.mkdir_p DIRECTORY
- end
- def cleanup
- Dir[File.join(DIRECTORY, "#{self.id}-*")].each do |filename|
- File.unlink(filename) rescue nil end
- end
- end
Add Comment
Please, Sign In to add comment