-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathtrip.rb
44 lines (40 loc) · 1.13 KB
/
trip.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
# == Schema Information
#
# Table name: trips
#
# id :bigint not null, primary key
# from_id :integer
# to_id :integer
# start_time :string
# duration_minutes :integer
# price_cents :integer
# bus_id :integer
#
class Trip < ApplicationRecord
HHMM_REGEXP = /([0-1][0-9]|[2][0-3]):[0-5][0-9]/
belongs_to :from, class_name: 'City'
belongs_to :to, class_name: 'City'
belongs_to :bus
validates :from, presence: true
validates :to, presence: true
validates :bus, presence: true
validates :start_time, format: { with: HHMM_REGEXP, message: 'Invalid time' }
validates :duration_minutes, presence: true
validates :duration_minutes, numericality: { greater_than: 0 }
validates :price_cents, presence: true
validates :price_cents, numericality: { greater_than: 0 }
def to_h
{
from: from.name,
to: to.name,
start_time: start_time,
duration_minutes: duration_minutes,
price_cents: price_cents,
bus: {
number: bus.number,
model: bus.model,
services: bus.services.map(&:name),
},
}
end
end