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
|
class RoutesController < ChouetteController
include ReferentialSupport
include PolicyChecker
defaults :resource_class => Chouette::Route
respond_to :html, :xml, :json
respond_to :kml, :only => :show
respond_to :js, :only => :show
belongs_to :referential do
belongs_to :line, :parent_class => Chouette::Line, :optional => true, :polymorphic => true
end
before_action :define_candidate_opposite_routes, only: [:new, :edit]
def index
index! do |format|
format.html { redirect_to referential_line_path(@referential, @line) }
end
end
def edit_boarding_alighting
@route = route
end
def save_boarding_alighting
@route = route
if @route.update_attributes!(route_params)
redirect_to referential_line_route_path(@referential, @line, @route)
else
render "edit_boarding_alighting"
end
end
def show
@map = RouteMap.new(route).with_helpers(self)
@route_sp = route.stop_points
if sort_sp_column && sort_sp_direction
@route_sp = @route_sp.order(sort_sp_column + ' ' + sort_sp_direction)
else
@route_sp = @route_sp.order(:position)
end
show! do
@route = @route.decorate(context: {
referential: @referential,
line: @line
})
@route_sp = StopPointDecorator.decorate(@route_sp)
end
end
def destroy
destroy! do |success, failure|
success.html { redirect_to referential_line_path(@referential,@line) }
end
end
def create
create! do |success, failure|
success.html { redirect_to referential_line_path(@referential,@line) }
failure.html { flash[:alert] = route.errors[:flash]; render :action => :new }
end
end
def duplicate
route = Chouette::Route.find(params[:id]).duplicate
flash[:notice] = t('routes.duplicate.success')
redirect_to referential_line_path(@referential, route.line)
end
protected
alias_method :route, :resource
def collection
@q = parent.routes.search(params[:q])
@routes ||=
begin
routes = @q.result(:distinct => true).order(:name)
routes = routes.paginate(:page => params[:page]) if @per_page.present?
routes
end
end
def define_candidate_opposite_routes
scope = if params[:id]
parent.routes.where(opposite_route: [nil, resource]).where('id <> ?', resource.id)
else
parent.routes.where(opposite_route: nil)
end
@forward = scope.where(wayback: :outbound)
@backward = scope.where(wayback: :inbound)
end
private
def sort_sp_column
route.stop_points.column_names.include?(params[:sort]) ? params[:sort] : 'position'
end
def sort_sp_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : 'asc'
end
def route_params
params.require(:route).permit(
:line_id,
:objectid,
:object_version,
:name,
:comment,
:opposite_route_id,
:published_name,
:number,
:direction,
:wayback,
stop_points_attributes: [:id, :_destroy, :position, :stop_area_id, :for_boarding, :for_alighting]
)
end
end
|