Created
June 6, 2012 22:35
-
-
Save Jimgerneer/2885258 to your computer and use it in GitHub Desktop.
Models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Booking | |
include DataMapper::Resource | |
property :id, Serial | |
belongs_to :booked_user, :model => 'User', :child_key => [:user_id] | |
belongs_to :booked_opportunities, :model => 'Opportunity', :child_key => [:opportunity_id] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'will_paginate' | |
require_relative 'routes_helper' | |
class Rook < Sinatra::Base | |
get '/' do | |
@opportunities = Opportunity.paginate(:page => params[:page], :per_page => 10) | |
haml :index | |
end | |
get '/opportunity' do | |
login_required | |
haml :opportunity | |
end | |
post '/opportunity/update/:id?' do | |
end | |
post '/opportunity' do | |
opportunity_params = params[:opportunity].merge(:user => current_user) | |
@opportunity = Opportunity.create!(opportunity_params) | |
redirect "/" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Opportunity | |
include DataMapper::Resource | |
property :id, Serial | |
property :title, String | |
property :skills, String | |
property :description, String | |
belongs_to :user | |
has n, :bookings | |
has n, :booked_users, :model => 'User', :child_key => [:id], | |
:parent_key => [:opportunity_id], :through => :bookings | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
include DataMapper::Resource | |
attr_accessor :password, :password_confirmation | |
property :id, Serial, :writer => :protected, :key => true | |
property :email, String, :required => true, :length => (5..40), | |
:unique => true, :format => :email_address | |
property :username, String, :required => true, :length => (2..32), | |
:unique => true | |
property :hashed_password, String, :writer => :protected | |
property :salt, String, :required => true, :writer => :protected | |
property :created_at, DateTime | |
property :account_type, String, :required => true, :default => 'standard', | |
:writer => :protected | |
property :active, Boolean, :default => true, :writer => :protected | |
has n, :opportunities | |
has n, :bookings | |
has n, :booked_opportunities, :model => 'Opportunity', :child_key => [:id], | |
:parent_key => [:user_id], :through => :bookings | |
validates_presence_of :password_confirmation | |
validates_confirmation_of :password | |
def self.authenticate(username_or_email, pass) | |
current_user = first(:username => username_or_email)# || first(:email => username_or_email) | |
return nil if current_user.nil? || self.encrypt(pass, current_user.salt) != current_user.hashed_password | |
current_user | |
end | |
def password=(pass) | |
@password = pass | |
self.salt = (1..12).map{(rand(26)+65).chr}.join if !self.salt | |
self.hashed_password = self.class.encrypt(@password, self.salt) | |
end | |
protected | |
def self.encrypt(pass, salt) | |
Digest::SHA1.hexdigest(pass+salt) | |
end | |
end | |
# has n, :profile | |
# has n, :courses |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment