- # == Schema Information
- # Schema version: 20100715120610
- #
- # Table name: hotels
- #
- # id :integer not null, primary key
- # name :string(255)
- # unit :string(255)
- # streetNo :string(255)
- # street :string(255)
- # city :string(255)
- # postcode :string(255)
- # country :string(255)
- # created_at :datetime
- # updated_at :datetime
- #
- class Hotel < ActiveRecord::Base
- has_many :relationships
- has_many :users, :through => relationships
- attr_accessible :name, :unit, :streetno, :street, :city, :postcode, :country
- validates_presence_of :name, :city, :country
- end
- # == Schema Information
- # Schema version: 20100715120610
- #
- # Table name: users
- #
- # id :integer not null, primary key
- # name :string(255)
- # email :string(255)
- # privilege :integer
- # created_at :datetime
- # updated_at :datetime
- # encrypted_password :string(255)
- # salt :string(255)
- #
- class User < ActiveRecord::Base
- has_many :relationships
- has_many :hotel, :through => relationships
- attr_accessor :password
- attr_accessible :name, :email, :privilege, :password, :password_confirmation
- EmailRegex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
- validates_presence_of :name, :email, :privilege
- validates_length_of :name, :maximum => 50
- validates_length_of :email, :maximum => 50
- validates_format_of :email, :with => EmailRegex
- validates_uniqueness_of :email, :case_sensitive => false
- # Automatically create the virtual attribute 'password_confirmation'.
- validates_confirmation_of :password
- # Password validations.
- validates_presence_of :password
- validates_length_of :password, :within => 6..20
- before_save :encrypt_password
- # Return true if the user's password matches the submitted password.
- def has_pasword?(submitted_password)
- encrypted_password == encrypt(submitted_password)
- end
- def remember_me!
- self.remember_token = encrypt("#{salt}--#{id}--#{Time.now.utc}")
- save_without_validation
- end
- def self.authenticate(email, submitted_password)
- user = find_by_email(email)
- return nil if user.nil?
- return user if user.has_password?(submitted_password)
- end
- def following?(followed)
- #hotels_users.all()
- end
- def hotels()
- relationships.find_by_hotel_id()
- def list_hotels()
- #relationships.all()
- end
- def has_hotel(hotel)
- #relationships.find_by_hotel_id(hotel.id)
- end
- def follow!(user)
- #hotels_users.create!(:hotel_id => user.id)
- end
- private
- def encrypt_password
- unless password.nil?
- self.salt = make_salt
- self.encrypted_password = encrypt(password)
- end
- end
- def encrypt(string)
- secure_hash("#{salt}#{string}")
- end
- def make_salt
- secure_hash("#{Time.now.utc}#{password}")
- end
- def secure_hash(string)
- Digest::SHA2.hexdigest(string)
- end
- end
- class Relationship < ActiveRecord::Base
- belongs_to :user
- belongs_to :hotel
- end