Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 3.13 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # == Schema Information
  2. # Schema version: 20100715120610
  3. #
  4. # Table name: hotels
  5. #
  6. #  id         :integer         not null, primary key
  7. #  name       :string(255)
  8. #  unit       :string(255)
  9. #  streetNo   :string(255)
  10. #  street     :string(255)
  11. #  city       :string(255)
  12. #  postcode   :string(255)
  13. #  country    :string(255)
  14. #  created_at :datetime
  15. #  updated_at :datetime
  16. #
  17.  
  18. class Hotel < ActiveRecord::Base
  19.  
  20.   has_many :relationships
  21.   has_many :users, :through => relationships
  22.  
  23.   attr_accessible :name, :unit, :streetno, :street, :city, :postcode, :country
  24.  
  25.   validates_presence_of :name, :city, :country
  26.  
  27. end
  28.  
  29.  
  30.  
  31. # == Schema Information
  32. # Schema version: 20100715120610
  33. #
  34. # Table name: users
  35. #
  36. #  id                 :integer         not null, primary key
  37. #  name               :string(255)
  38. #  email              :string(255)
  39. #  privilege          :integer
  40. #  created_at         :datetime
  41. #  updated_at         :datetime
  42. #  encrypted_password :string(255)
  43. #  salt               :string(255)
  44. #
  45.  
  46. class User < ActiveRecord::Base
  47.  
  48.   has_many :relationships
  49.   has_many :hotel, :through => relationships
  50.  
  51.   attr_accessor :password
  52.   attr_accessible :name, :email, :privilege, :password, :password_confirmation
  53.  
  54.   EmailRegex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  55.  
  56.   validates_presence_of :name, :email, :privilege
  57.   validates_length_of   :name, :maximum => 50
  58.   validates_length_of   :email, :maximum => 50
  59.   validates_format_of   :email, :with => EmailRegex
  60.   validates_uniqueness_of :email, :case_sensitive => false
  61.  
  62.   # Automatically create the virtual attribute 'password_confirmation'.
  63.   validates_confirmation_of :password
  64.  
  65.   # Password validations.
  66.   validates_presence_of :password
  67.   validates_length_of   :password, :within => 6..20
  68.  
  69.   before_save :encrypt_password
  70.  
  71.   # Return true if the user's password matches the submitted password.
  72.   def has_pasword?(submitted_password)
  73.     encrypted_password == encrypt(submitted_password)
  74.   end
  75.  
  76.   def remember_me!
  77.     self.remember_token = encrypt("#{salt}--#{id}--#{Time.now.utc}")
  78.     save_without_validation
  79.   end
  80.  
  81.   def self.authenticate(email, submitted_password)
  82.     user = find_by_email(email)
  83.     return nil  if user.nil?
  84.     return user if user.has_password?(submitted_password)
  85.   end
  86.  
  87.   def following?(followed)
  88.     #hotels_users.all()
  89.   end
  90.  
  91.   def hotels()
  92.     relationships.find_by_hotel_id()
  93.  
  94.   def list_hotels()
  95.     #relationships.all()
  96.   end
  97.  
  98.   def has_hotel(hotel)
  99.     #relationships.find_by_hotel_id(hotel.id)
  100.   end
  101.  
  102.   def follow!(user)
  103.     #hotels_users.create!(:hotel_id => user.id)
  104.   end
  105.    
  106.   private
  107.  
  108.     def encrypt_password
  109.       unless password.nil?
  110.         self.salt = make_salt
  111.         self.encrypted_password = encrypt(password)
  112.       end
  113.     end
  114.  
  115.     def encrypt(string)
  116.       secure_hash("#{salt}#{string}")
  117.     end
  118.  
  119.     def make_salt
  120.       secure_hash("#{Time.now.utc}#{password}")
  121.     end
  122.  
  123.     def secure_hash(string)
  124.       Digest::SHA2.hexdigest(string)
  125.     end
  126.    
  127. end
  128.  
  129.  
  130.  
  131. class Relationship < ActiveRecord::Base
  132.   belongs_to :user
  133.   belongs_to :hotel
  134. end