|
| 1 | +require 'data_mapper' |
| 2 | +require 'bcrypt' |
| 3 | + |
| 4 | +if ENV['RACK_ENV'] != 'production' |
| 5 | + require 'dotenv' |
| 6 | + Dotenv.load('.env') |
| 7 | +end |
| 8 | + |
| 9 | +DataMapper.setup(:default, ENV['DATABASE_URL']) |
| 10 | + |
| 11 | +# USERS |
| 12 | +# ===================================== |
| 13 | + |
| 14 | +class User |
| 15 | + include DataMapper::Resource |
| 16 | + property :id, Serial |
| 17 | + property :first_name, String, :required => true |
| 18 | + property :last_name, String, :required => true |
| 19 | + property :email, String, { :required => true, |
| 20 | + :unique => true, |
| 21 | + :format => :email_address, |
| 22 | + :messages => { :presence => "Please enter a valid email address.", |
| 23 | + :is_unique => "It looks like that email is already taken.", |
| 24 | + :format => "Please enter a valid email address" |
| 25 | + } |
| 26 | + } |
| 27 | + property :phone_number, String, { :required => true, |
| 28 | + :unique => true, |
| 29 | + :messages => { :presence => "Please enter your phone number.", |
| 30 | + :is_unique => "It looks like that phone number is taken." |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + property :password, BCryptHash, :required => true |
| 35 | + validates_confirmation_of :password |
| 36 | + |
| 37 | + attr_accessor :password_confirmation |
| 38 | + validates_length_of :password_confirmation, :min => 6, :if => :new_user |
| 39 | + |
| 40 | + def new_user |
| 41 | + self.new? |
| 42 | + end |
| 43 | + |
| 44 | + has n, :memberships |
| 45 | + has n, :groups, through: :memberships |
| 46 | + |
| 47 | + # has n, :bets, :child_key => [:source_id] |
| 48 | + # has n, :bettors, self, :through => :bets, :via => :target |
| 49 | + |
| 50 | + has n, :created_bets, 'Bets', :child_key => [:bettor_id] |
| 51 | + has n, :joined_bets, 'Bets', :child_key => [:bettee_id] |
| 52 | + |
| 53 | + def bets |
| 54 | + self.created_bets + self.joined_bets |
| 55 | + end |
| 56 | +end |
| 57 | + |
| 58 | +# bettor = User.new |
| 59 | +# bettee = User.new |
| 60 | +# |
| 61 | +# bettor.bets # => all bets that bettor has create |
| 62 | +# bettor.bets.first.bettee |
| 63 | + |
| 64 | +# bookings |
| 65 | +# - user_id |
| 66 | +# - bet_id |
| 67 | +# - is_creator Boolean |
| 68 | + |
| 69 | +# create instance of bet and assign bettee |
| 70 | +Bet.new(bettor: User.find(3), bettee: User.find(7)) |
| 71 | + |
| 72 | + |
| 73 | +# MEMBERSHIP JOIN TABLE |
| 74 | +# ===================================== |
| 75 | + |
| 76 | +class Membership |
| 77 | + include DataMapper::Resource |
| 78 | + property :id, Serial |
| 79 | + belongs_to :user, :required => true |
| 80 | + belongs_to :group, :required => true |
| 81 | + |
| 82 | +end |
| 83 | + |
| 84 | +# GROUPS |
| 85 | +# ===================================== |
| 86 | + |
| 87 | +class Group |
| 88 | + include DataMapper::Resource |
| 89 | + property :id, Serial |
| 90 | + property :group_name, String, { :required => true, |
| 91 | + :unique => true, |
| 92 | + :messages => { :is_unique => "Group name is taken. Please choose another." } |
| 93 | + } |
| 94 | + |
| 95 | + property :password, BCryptHash, :required => true |
| 96 | + validates_confirmation_of :password |
| 97 | + |
| 98 | + attr_accessor :password_confirmation |
| 99 | + validates_length_of :password_confirmation, :min => 6, :if => :new_group |
| 100 | + |
| 101 | + |
| 102 | + def new_group |
| 103 | + self.new? |
| 104 | + end |
| 105 | + |
| 106 | + has n, :memberships |
| 107 | + has n, :users, through: :memberships |
| 108 | + |
| 109 | +end |
| 110 | + |
| 111 | +# BETS |
| 112 | +# ===================================== |
| 113 | + |
| 114 | +class Bet |
| 115 | + include DataMapper::Resource |
| 116 | + property :id, Serial |
| 117 | + property :amount, Float |
| 118 | + property :expiration, Date |
| 119 | + property :description, Text |
| 120 | + |
| 121 | + belongs_to :bettor, 'User', :key => true |
| 122 | + belongs_to :bettee, 'User', :key => true |
| 123 | +end |
| 124 | + |
| 125 | +DataMapper.finalize |
| 126 | +DataMapper.auto_upgrade! |
0 commit comments