-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.rb
187 lines (138 loc) · 3.92 KB
/
database.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# -*- coding: utf-8 -*-
Bundler.require(:database)
require 'dm-migrations/migration_runner'
DB_PATH=File.join(Dir.pwd,"templates.sqlite3")
DataMapper::Logger.new(STDOUT, :debug) unless ENV['RACK_ENV'] == "production"
DataMapper.setup(:default, 'sqlite::memory:') if ENV['RACK_ENV']=="test"
DataMapper.setup(:default, "sqlite:///#{DB_PATH}")
DataMapper.setup(:default,
"mysql://#{DB_USER}:#{DB_PASSWORD}@#{DB_SERVER}/#{DB_DATABASE}") if
ENV["RACK_ENV"] == "production"
DataMapper::Model.raise_on_save_failure = true
class User
include DataMapper::Resource
property :id, String, :key => true, :required => true,
:default => proc { UUIDTools::UUID.random_create }
# Each user may log in using different methods:
has n, :authentications
has n, :entries
def is_owner?(uuid)
! entries.first(:uuid=>uuid,:user_id=>id).nil?
end
end
class Authentication
include DataMapper::Resource
belongs_to :user, :key=>true
# Authentication provider:
property :provider, String
# User ID allocated by that provider:
property :uid, String, :length => 240
# User name and email:
property :user_name, String, :length => 240
property :user_email, String, :length => 240, :index => true
end
class Entry
include DataMapper::Resource
property :id, Serial
property :name, String, :default=>Proc.new { UUIDTools::UUID.random_create}
property :uuid, String, :key => true,
:required => true,
:default =>proc { UUIDTools::UUID.random_create }
property :rating, Decimal, :default => 0 , :writer=> :protected
property :sum, Integer, :default => 0
property :votes, Integer, :default => 0
timestamps :at
has n, :comments
has 1, :deployable
has 1, :image
belongs_to :user
has n, :entry_tags
has n, :tags, :through => :entry_tags
def tag_list
tags.all.map { |t| t.name }.join(', ')
end
def tag_list=(list)
list.each do |tag|
new_tag = Tag.first_or_create(:name => tag)
tags << new_tag
end
end
def self.bogus
new(:deployable=>Deployable.new, :image=>Image.new)
end
def self.popular(limit = 5)
all(:rating.gt=>3, :order => [ :rating.desc ], :limit=>limit)
end
def self.last_inserts(limit = 5)
all(:order=>[ :created_at.desc ],:limit=>limit)
end
def self.search(*tags)
nil unless tags.empty?
all(:"entry_tags.tag.name"=>tags,:order=> [ :rating.desc ] )
end
def exists?
!id.nil?
end
end
class Deployable
include DataMapper::Resource
property :id, Serial
property :content, Text
belongs_to :entry
end
class Image
include DataMapper::Resource
property :id, Serial
property :content, Text
belongs_to :entry
end
class Comment
include DataMapper::Resource
property :id, Serial
property :text, Text
belongs_to :entry
end
# Adding the tagging feature - Start
class EntryTag
include DataMapper::Resource
property :tag_id, Integer, :key => true
property :entry_id, Integer, :key => true
belongs_to :entry
belongs_to :tag
end
class Tag
include DataMapper::Resource
property :id, Serial
property :name, String, :required=>true, :unique =>true, :unique_index =>true
has n, :entry_tags
has n, :entries,
:through => :entry_tags
end
# - END
# Migration to create the Trigger (Valid in MySQL and SQLite)
migration 1, :create_people_table do
up do
execute <<-EOF
delimiter #
create trigger entry_vote_after_ins_trig after insert on image_vote
for each row
begin
update entry set
votes = votes + 1,
total_score = score + new.score,
rating = total_score / votes
where
entry_id = new.entry_id;
end#
delimiter ;
EOF
end
down do
execute "DROP TRIGGER entry_vote_after_ins_trig"
end
end
# End
DataMapper.finalize
DataMapper.auto_migrate! unless DataMapper.repository(:default).adapter
.storage_exists?('authentications')
DataMapper.auto_upgrade!