forked from mongomapper/mongomapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.rb
233 lines (176 loc) · 4.13 KB
/
models.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# custom type
class WindowSize
attr_reader :width, :height
def self.to_mongo(value)
value.to_a
end
def self.from_mongo(value)
value.is_a?(self) ? value : WindowSize.new(value)
end
def initialize(*args)
@width, @height = args.flatten
end
def to_a
[width, height]
end
def ==(other)
other.is_a?(self.class) && other.width == width && other.height == height
end
end
module AccountsExtensions
def inactive
all(:last_logged_in => nil)
end
end
class Post
include MongoMapper::Document
key :title, String
key :body, String
many :comments, :as => :commentable, :class_name => 'PostComment'
timestamps!
end
class PostComment
include MongoMapper::Document
key :username, String, :default => 'Anonymous'
key :body, String
key :commentable_id, ObjectId
key :commentable_type, String
belongs_to :commentable, :polymorphic => true
timestamps!
end
class Address
include MongoMapper::EmbeddedDocument
key :address, String
key :city, String
key :state, String
key :zip, Integer
end
class Message
include MongoMapper::Document
key :body, String
key :position, Integer
key :room_id, ObjectId
belongs_to :room
end
class Enter < Message; end
class Exit < Message; end
class Chat < Message; end
class Room
include MongoMapper::Document
key :name, String
many :messages, :polymorphic => true, :order => 'position' do
def older
all(:position => {'$gt' => 5})
end
end
many :latest_messages, :class_name => 'Message', :order => 'position desc', :limit => 2
many :accounts, :polymorphic => true, :extend => AccountsExtensions
end
class Account
include MongoMapper::Document
key :room_id, ObjectId
key :last_logged_in, Time
belongs_to :room
end
class AccountUser < Account; end
class Bot < Account; end
class Answer
include MongoMapper::Document
key :body, String
end
module CollaboratorsExtensions
def top
first
end
end
class Project
include MongoMapper::Document
key :name, String
many :collaborators, :extend => CollaboratorsExtensions
many :statuses, :order => 'position' do
def open
all(:name => %w(New Assigned))
end
end
many :addresses do
def find_all_by_state(state)
# can't use select here for some reason
find_all { |a| a.state == state }
end
end
end
class Collaborator
include MongoMapper::Document
key :project_id, ObjectId
key :name, String
belongs_to :project
end
class Status
include MongoMapper::Document
scope :complete, where(:name => 'Complete')
def self.by_position(position)
where(:position => position)
end
key :project_id, ObjectId
key :target_id, ObjectId
key :target_type, String
key :name, String, :required => true
key :position, Integer
belongs_to :project
belongs_to :target, :polymorphic => true
end
class Media
include MongoMapper::EmbeddedDocument
key :file, String
key :visible, Boolean
end
class Video < Media
key :length, Integer
end
class Image < Media
key :width, Integer
key :height, Integer
end
class Music < Media
key :bitrate, String
end
class Catalog
include MongoMapper::Document
many :medias, :polymorphic => true do
def visible
# for some reason we can't use select here
find_all { |m| m.visible? }
end
end
end
module TrModels
class Transport
include MongoMapper::EmbeddedDocument
key :license_plate, String
key :purchased_on, Date
end
class Car < TrModels::Transport
include MongoMapper::EmbeddedDocument
key :model, String
key :year, Integer
end
class Bus < TrModels::Transport
include MongoMapper::EmbeddedDocument
key :max_passengers, Integer
end
class Ambulance < TrModels::Transport
include MongoMapper::EmbeddedDocument
key :icu, Boolean
end
class Fleet
include MongoMapper::Document
module TransportsExtension
def to_be_replaced
# for some reason we can't use select
find_all { |t| t.purchased_on < 2.years.ago.to_date }
end
end
many :transports, :polymorphic => true, :class_name => "TrModels::Transport", :extend => TransportsExtension
key :name, String
end
end