blob: 70c76314629ff165cec3ddf040d699f2677b9ca6 (
plain)
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
|
class CleanUp
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :expected_date, :keep_lines, :keep_stops , :keep_companies
attr_accessor :keep_networks, :keep_group_of_lines
validates_presence_of :expected_date
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
def clean
# as foreign keys are presents , delete method can be used for faster performance
result = CleanUpResult.new
# find and remove time_tables
tms = Chouette::TimeTable.validity_out_from_on?(Date.parse(expected_date))
result.time_table_count = tms.size
tms.each do |tm|
tm.delete
end
# remove vehiclejourneys without timetables
Chouette::VehicleJourney.find_each(:conditions => "id not in (select distinct vehicle_journey_id from time_tables_vehicle_journeys)") do |vj|
if vj.time_tables.size == 0
result.vehicle_journey_count += 1
vj.delete
end
end
# remove journeypatterns without vehicle journeys
Chouette::JourneyPattern.find_each(:conditions => "id not in (select distinct journey_pattern_id from vehicle_journeys)") do |jp|
if jp.vehicle_journeys.size == 0
result.journey_pattern_count += 1
jp.delete
end
end
# remove routes without journeypatterns
Chouette::Route.find_each(:conditions => "id not in (select distinct route_id from journey_patterns)") do |r|
if r.journey_patterns.size == 0
result.route_count += 1
r.delete
end
end
# if asked remove lines without routes
if keep_lines == "0"
Chouette::Line.find_each(:conditions => "id not in (select distinct line_id from routes)") do |l|
if l.routes.size == 0
result.line_count += 1
l.delete
end
end
end
# if asked remove stops without children (recurse)
if keep_stops == "0"
Chouette::StopArea.find_each(:conditions => { :area_type => "BoardingPosition" }) do |bp|
if bp.stop_points.size == 0
result.stop_count += 1
bp.delete
end
end
Chouette::StopArea.find_each(:conditions => { :area_type => "Quay" }) do |q|
if q.stop_points.size == 0
result.stop_count += 1
q.delete
end
end
Chouette::StopArea.find_each(:conditions => { :area_type => "CommercialStopPoint" }) do |csp|
if csp.children.size == 0
result.stop_count += 1
csp.delete
end
end
Chouette::StopArea.find_each(:conditions => { :area_type => "StopPlace" }) do |sp|
if sp.children.size == 0
result.stop_count += 1
sp.delete
end
end
Chouette::StopArea.find_each(:conditions => { :area_type => "ITL" }) do |itl|
if itl.routing_stops.size == 0
result.stop_count += 1
itl.delete
end
end
end
# if asked remove companies without lines or vehicle journeys
if keep_companies == "0"
Chouette::Company.find_each do |c|
if c.lines.size == 0
result.company_count += 1
c.delete
end
end
end
# if asked remove networks without lines
if keep_networks == "0"
Chouette::Network.find_each do |n|
if n.lines.size == 0
result.network_count += 1
n.delete
end
end
end
# if asked remove group_of_lines without lines
if keep_group_of_lines == "0"
Chouette::GroupOfLine.find_each do |n|
if n.lines.size == 0
result.group_of_line_count += 1
n.delete
end
end
end
result
end
end
|