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
|
class ReferentialConsolidated
attr_reader :params
def initialize vehicle_journeys, params
@vehicle_journeys = vehicle_journeys
@params = params
end
def paginated_lines
@paginated_lines ||= begin
line_ids = @vehicle_journeys.joins(route: :line).pluck('lines.id')
lines = Chouette::Line.where(id: line_ids).order(:name)
lines.paginate page: params[:page], per_page: params[:per_page] || 10
end
end
def lines
@lines ||= paginated_lines.to_a.map {|l| Line.new(self, l, @vehicle_journeys, params) }
end
def _should_highlight?
return false unless params[:q].present?
keys = params[:q].keys - ["stop_areas"]
params[:q].values_at(*keys).each do |value|
if value.is_a?(Hash)
return true if value.values.any?(&:present?)
elsif value.is_a?(Array)
return true if value.any?(&:present?)
else
if value.present?
return true
end
end
end
false
end
def should_highlight?
if @should_highlight.nil?
@should_highlight = _should_highlight?
end
@should_highlight
end
class Base
extend Forwardable
attr_reader :params
attr_reader :parent
attr_reader :ar_model
def initialize parent, ar_model, vehicle_journeys, params, opts={}
@parent = parent
@ar_model = ar_model
@all_vehicle_journeys = vehicle_journeys
@params = params
@opts = opts
end
def should_highlight?
parent.should_highlight?
end
end
class Line < Base
delegate name: :ar_model
delegate id: :ar_model
def routes
@routes ||= begin
ar_model.routes.order(:name).map {|r| Route.new(self, r, @all_vehicle_journeys, params) }
end
end
end
class Route < Base
def_delegators :ar_model, :name, :id, :time_tables, :purchase_windows, :stop_area_ids
def vehicle_journey_at_stops
@vehicle_journey_at_stops ||= begin
out = Hash.new{|h, k| h[k] = {}}
ar_model.vehicle_journey_at_stops.each do |vjas|
out[vjas.vehicle_journey_id][vjas.stop_point_id] = vjas
end
out
end
end
def vehicle_journeys
@vehicle_journeys ||= begin
ar_model.vehicle_journeys.select(:id, :published_journey_name, :route_id, :journey_pattern_id).map {|vj| VehicleJourney.new(self, vj, @all_vehicle_journeys, params, vehicle_journey_at_stops: vehicle_journey_at_stops[vj.id]) }
end
end
def highlighted_journeys
@all_vehicle_journeys.joins(:journey_pattern).where(route_id: self.id)
end
def highlighted_count
highlighted_journeys.except(:select).count
end
def highlighted?
matching_stop_areas = params[:q] && params[:q]["stop_areas"] && (params[:q]["stop_areas"].values & self.stop_area_ids.map(&:to_s)).present?
(should_highlight? || matching_stop_areas) && highlighted_journeys.exists?
end
def stop_areas
@stop_areas ||= begin
out = {}
ar_model.stop_areas.select(:id, :name, :city_name, :zip_code, :time_zone).each do |sp|
out[sp.id] = sp
end
out
end
end
def stop_points
@stop_points ||= ar_model.stop_points.map {|sp| StopPoint.new(self, sp, @all_vehicle_journeys, params, stop_area: stop_areas[sp.stop_area_id]) }
end
end
class VehicleJourney < Base
def_delegators :ar_model, :id, :published_journey_name, :journey_pattern, :time_tables, :purchase_windows, :time_table_ids, :purchase_window_ids, :route, :journey_pattern_only_objectid
def highlighted?
should_highlight? && @all_vehicle_journeys.where(id: self.id).exists?
end
def vehicle_journey_at_stops
@opts[:vehicle_journey_at_stops] || {}
end
def has_purchase_window? purchase_window
purchase_window_ids.include?(purchase_window.id)
end
def has_time_table? time_table
time_table_ids.include?(time_table.id)
end
end
class StopPoint < Base
def_delegators :ar_model, :id, :arrival_time, :departure_time, :stop_area_id
def stop_area
@opts[:stop_area]
end
def name
stop_area.name
end
def highlighted?
params[:q] && params[:q]["stop_areas"] && params[:q]["stop_areas"].values.any?{|v| v.to_s == stop_area_id.to_s}
end
end
end
|